-
Notifications
You must be signed in to change notification settings - Fork 0
279 lines (267 loc) · 10.8 KB
/
docker-image.yml
File metadata and controls
279 lines (267 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
name: FastAPI CI Deployment
on:
push:
branches: [ main, staging ]
paths-ignore:
- '**/README.md'
pull_request:
branches: [ main, staging ]
paths:
- '!README.md'
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
SRV_PASS: ${{ secrets.SRV_PASS }}
SRV_USER: root
SRV_PORT: 22
SRV_IP: 10.99.104.214;10.99.104.219;10.99.104.241;10.99.104.250
WORK_PATH: /srv/deploy
MOUNT_PORT: 8787
MOUNT_PATH_1: /mnt/storage
MOUNT_PATH_2: /mnt/tmp
jobs:
varmaps:
runs-on: self-hosted
container:
image: ghcr.io/allennliu/docker:18
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
outputs:
VERSION: ${{ steps.var1.outputs.version }}
VER_MATCH: ${{ steps.var2.outputs.match }}
PROJECT_NAME: ${{ steps.var3.outputs.project_name }}
REPO_AUTHOR: ${{ steps.var4.outputs.repo_author }}
CONTAINER_NAME: ${{ steps.var5.outputs.container_name }}
REGISTRY_IMAGE: ${{ steps.var6.outputs.registry_image }}
EXE_PATH: ${{ steps.var7.outputs.exe_path }}
BRANCH: ${{ steps.var8.outputs.branch }}
steps:
- uses: actions/checkout@v2
- name: Set Environ Variables
run: echo "VERSION=$(grep -E '^`Rev:\s' README.md | grep -oE '([0-9]+\.){2}[0-9]+')" >> $GITHUB_ENV
- id: var1
run: echo "::set-output name=version::${{ env.VERSION }}"
- uses: actions-ecosystem/action-regex-match@v2
id: regex-ver-match
with:
text: ${{ env.VERSION }}
regex: '\.[1-9][0-9]{0,}$'
- id: var2
run: echo "::set-output name=match::${{ steps.regex-ver-match.outputs.match }}"
- id: var3
run: echo "::set-output name=project_name::$(basename $GITHUB_REPOSITORY)"
- id: var4
run: echo "::set-output name=repo_author::$(echo $GITHUB_REPOSITORY | awk '{print tolower($0)}')"
- id: var5
run: echo "::set-output name=container_name::$(basename $GITHUB_REPOSITORY | awk '{print tolower($0)}')"
- id: var6
run: echo "::set-output name=registry_image::ghcr.io/$(echo $GITHUB_REPOSITORY | awk '{print tolower($0)}')"
- id: var7
run: echo "::set-output name=exe_path::${WORK_PATH}/$(basename $GITHUB_REPOSITORY)"
- id: var8
run: echo "::set-output name=branch::$(echo $GITHUB_REF | awk -F\/ '{print $2"_"$3}')"
build:
runs-on: self-hosted
needs: varmaps
if: contains('refs/heads/staging', github.ref)
container:
image: ghcr.io/allennliu/docker:18
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
steps:
- name: Build Image
run: |
docker build --no-cache -t ghcr.io/${{ needs.varmaps.outputs.REPO_AUTHOR }}:${{ needs.varmaps.outputs.VERSION }} .
docker images | grep -i $GITHUB_REPOSITORY || true
- name: Publish to Registry
run: |
echo $ACCESS_TOKEN | docker login ghcr.io -u $GITHUB_REPOSITORY_OWNER --password-stdin
docker push ghcr.io/${{ needs.varmaps.outputs.REPO_AUTHOR }}:${{ needs.varmaps.outputs.VERSION }}
buildx:
runs-on: self-hosted
needs: varmaps
if: contains('refs/heads/main', github.ref)
permissions:
contents: read
packages: write
container:
image: ghcr.io/allennliu/docker:19
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
steps:
- uses: actions/checkout@v2
- name: Log in to the Container registry
if: github.event_name != 'pull_request'
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ needs.varmaps.outputs.REGISTRY_IMAGE }}
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ needs.varmaps.outputs.REGISTRY_IMAGE }}:${{ needs.varmaps.outputs.VERSION }}
labels: ${{ steps.meta.outputs.labels }}
- name: Update Package Badge
uses: schneegans/dynamic-badges-action@v1.0.0
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: ab6dc65e1bc6049b57a18f32124f2ce7
filename: ${{ needs.varmaps.outputs.PROJECT_NAME }}-package.json
label: Package
message: v${{ needs.varmaps.outputs.VERSION }}
color: lightgrey
namedLogo: Docker
logoColor: blue
staging:
runs-on: self-hosted
needs: [varmaps, build, buildx]
container:
image: ghcr.io/allennliu/tool-ansible:1.0.1
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
steps:
- name: Configure Hosts
if: contains('refs/heads/main', github.ref)
run: |
sed -i "s,<SRV_USER>,${SRV_USER},g" ${PWD}/hosts
sed -i "s,<SRV_PASS>,${SRV_PASS},g" ${PWD}/hosts
sed -i "s,<SRV_PORT>,${SRV_PORT},g" ${PWD}/hosts
sed -i "s,<SRV_IP>,${SRV_IP},g" ${PWD}/hosts
sed -i -E "s,\;,\n,g" ${PWD}/hosts
- name: Configure Ansible
run: |
exe_cmd="bash start.sh -v ${{ needs.varmaps.outputs.VERSION }} -p ${{ needs.varmaps.outputs.PROJECT_NAME }} -n ${{ needs.varmaps.outputs.CONTAINER_NAME }} -i ${{ needs.varmaps.outputs.REGISTRY_IMAGE }} -P $MOUNT_PORT -M1 $MOUNT_PATH_1 -M2 $MOUNT_PATH_2"
sed -i "s,<EXE_CMD>,${exe_cmd},g" ${PWD}/variables/common.yaml
sed -i "s,<WORK_PATH>,${WORK_PATH},g" ${PWD}/variables/common.yaml
sed -i "s,<SRV_USER>,${SRV_USER},g" ${PWD}/variables/common.yaml
sed -i "s,<SRV_PASS>,${SRV_PASS},g" ${PWD}/variables/common.yaml
sed -i "s,<SRV_PORT>,${SRV_PORT},g" ${PWD}/variables/common.yaml
sed -i "s,<ACCESS_TOKEN>,${ACCESS_TOKEN},g" ${PWD}/variables/common.yaml
sed -i "s,<VERSION>,${{ needs.varmaps.outputs.VERSION }},g" ${PWD}/variables/common.yaml
sed -i "s,<EXE_PATH>,${{ needs.varmaps.outputs.EXE_PATH }},g" ${PWD}/variables/common.yaml
sed -i "s,<REGISTRY_IMAGE>,${{ needs.varmaps.outputs.REGISTRY_IMAGE }},g" ${PWD}/variables/common.yaml
sed -i "s,<CONTAINER_NAME>,${{ needs.varmaps.outputs.CONTAINER_NAME }},g" ${PWD}/variables/common.yaml
sed -i "s,<GITHUB_REPOSITORY_OWNER>,${GITHUB_REPOSITORY_OWNER},g" ${PWD}/variables/common.yaml
- name: Deploy Staging Service
if: contains('refs/heads/staging', github.ref)
run: ansible-playbook -i ${PWD}/hosts deploy-staging.yaml
# Because of the IT domain resolve policy, we couldn't
# pulling image from Github directly on Kubernetes node.
# So this is going to archive image and then pass it to
# deployment Kubernetes node. finally, loading image to
# local Docker image instantly.
- name: Pass Image to Production Server
if: contains('refs/heads/main', github.ref)
run: ansible-playbook -i ${PWD}/hosts passing-image.yaml
production:
runs-on: self-hosted
needs: [varmaps, staging]
if: contains('refs/heads/main', github.ref)
container:
image: ghcr.io/allennliu/tool-kubectl-deployment:1.0.4
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
steps:
- name: Configure Hosts
run: |
sed -i "s,<SRV_USER>,${SRV_USER},g" ${PWD}/hosts
sed -i "s,<SRV_PASS>,${SRV_PASS},g" ${PWD}/hosts
sed -i "s,<SRV_PORT>,${SRV_PORT},g" ${PWD}/hosts
sed -i "s,<SRV_IP>,${SRV_IP},g" ${PWD}/hosts
sed -i -E "s,\;,\n,g" ${PWD}/hosts
- name: Deploy Production Service
run: |
bash start.sh --k8s-run -v ${{ needs.varmaps.outputs.VERSION }} -p ${{ needs.varmaps.outputs.PROJECT_NAME }} -n ${{ needs.varmaps.outputs.CONTAINER_NAME }} \
-i ${{ needs.varmaps.outputs.REGISTRY_IMAGE }} -MP $MOUNT_PORT -M1 $MOUNT_PATH_1 -M2 $MOUNT_PATH_2
pytest:
runs-on: self-hosted
needs: [varmaps, production]
if: contains('refs/heads/main', github.ref)
container:
image: ghcr.io/allennliu/tool-kubectl-deployment:1.0.4
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
steps:
- name: Run Unit Test
run: |
export POD=$(kubectl get pods -n kube-ops | grep -oE "^${{ needs.varmaps.outputs.CONTAINER_NAME }}-(\w+\-)?\w+" | head -n1)
echo -e "\nStarting run Pytest on pod: ${POD}..\n"
kubectl exec -t $POD -n kube-ops -- bash -c 'cd tests && pytest' | tee pytest.log
kubectl exec -t $POD -n kube-ops -- bash -c 'cd tests && cat report.xml' > report.xml
echo "COVERAGE=$(grep -Eo '^TOTAL.+?([0-9]+\%)$' pytest.log | awk '{print $NF}')" >> $GITHUB_ENV
- name: Upload Pytest Report
if: always()
uses: actions/upload-artifact@v2
with:
name: pytest-results
path: report.xml
- name: Update Test Coverage Badge
if: ${{ env.COVERAGE != '' }}
uses: schneegans/dynamic-badges-action@v1.0.0
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: cdc11bececff228f66cebd39b8b588dc
filename: ${{ needs.varmaps.outputs.PROJECT_NAME }}__${{ needs.varmaps.outputs.BRANCH }}.json
label: Test Coverage
message: ${{ env.COVERAGE }}
color: green
namedLogo: pytest
release:
runs-on: self-hosted
if: |
contains('refs/heads/main', github.ref) && needs.varmaps.outputs.VER_MATCH == ''
needs: [varmaps, pytest]
container:
image: ghcr.io/allennliu/docker:18
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
steps:
- name: Publish Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ needs.varmaps.outputs.VERSION }}
release_name: Version ${{ needs.varmaps.outputs.VERSION }}
body: ${{ github.event.head_commit.message }}
draft: false
prerelease: false
- name: Update Release Badge
uses: schneegans/dynamic-badges-action@v1.0.0
with:
auth: ${{ secrets.GIST_SECRET }}
gistID: 0bf7d0bf3675b35eaa46fffc60b4ade0
filename: ${{ needs.varmaps.outputs.PROJECT_NAME }}-release.json
label: Release
message: v${{ needs.varmaps.outputs.VERSION }}
color: informational
namedLogo: fastapi
logoColor: success
clean:
runs-on: self-hosted
if: always()
needs: [build, buildx, production, pytest, release]
container:
image: ghcr.io/allennliu/docker:18
credentials:
username: ${{ github.actor }}
password: ${{ secrets.ACCESS_TOKEN }}
steps:
- uses: actions/checkout@v2
- name: Clean Environment
run: sh tools/pipeline_clean.sh