-
Notifications
You must be signed in to change notification settings - Fork 148
/
.gitlab-ci.yml.bak
353 lines (331 loc) · 10.5 KB
/
.gitlab-ci.yml.bak
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
stages:
- updateSql
- build
- invalidateCachalot
- deployComponent
- deployIngress
- rollbackConfirm
- rollback
variables:
PROJECTNAME: "devops"
DOCKERHUB: "hub.leffss.com"
DOCKERHUBPROJECT: "library"
DOCKERHUBUSER: "admin"
DOCKERHUBPASS: "Liff@2019"
DINDSERVICE: "dind.kube-system"
DINDSERVICEPORT: 2375
NAMESPACE: devops
mysql-update:
stage: updateSql
image: hub.leffss.com/library/mysql:5.7
script:
- |
if [ ! -d database ];then
echo "none database path, exit 0"
exit 0
fi
[ -d /data/${PROJECTNAME} ] || mkdir /data/${PROJECTNAME}
cd database
end_sql_num=$(ls 2>/dev/null|tail -1|awk -F '-' '{print $1}')
if [[ ${end_sql_num} == "" ]];then
echo "none sql update, exit 0"
exit 0
fi
echo "end_sql_num: ${end_sql_num}"
latest_sql_num=$(ls /data/${PROJECTNAME} 2>/dev/null|tail -1|awk -F '-' '{print $1}')
if [[ ${latest_sql_num} == "" ]];then
latest_sql_num=0
fi
echo "latest_sql_num: ${latest_sql_num}"
if [[ ${end_sql_num} -lt ${latest_sql_num} ]];then
echo "none sql update, exit 1"
exit 0
fi
if [[ ${end_sql_num} -eq ${latest_sql_num} ]];then
echo "none sql update, exit 2"
exit 0
fi
while true;do
let latest_sql_num=latest_sql_num+1
if [[ ${latest_sql_num} -gt ${end_sql_num} ]];then
echo "all sql update done, break"
break
fi
need_update_sql=$(ls ${latest_sql_num}-*.sql 2>/dev/null)
for sql in ${need_update_sql};do
echo "update sql: ${sql}"
cp -arf ${sql} /data/${PROJECTNAME}
done
done
#rules:
# - if: $CI_COMMIT_TAG && $CI_BUILD_REF_NAME == "dev"
# gitlab ci 无法做到指定分支打tag时运行job,所以使用规范 COMMIT_MESSAGE 的方法发布指定版本
rules:
- if: '$CI_COMMIT_MESSAGE =~ /^deploy dev/'
- if: $CI_COMMIT_TAG
devops-build:
stage: build
retry: 2
variables:
DOCKER_HOST: tcp://${DINDSERVICE}:${DINDSERVICEPORT}/
#DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
dockerfile: "Dockerfile"
before_script:
- docker login ${DOCKERHUB} -u "${DOCKERHUBUSER}" -p "${DOCKERHUBPASS}"
script:
- env
- cp -arf ./deploy/settings.py ./devops
- docker build -f ${dockerfile} --cache-from ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:latest -t ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:${CI_COMMIT_TAG} -t ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:latest .
- docker images
- docker push ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:${CI_COMMIT_TAG}
- docker push ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:latest
after_script:
- docker logout ${DOCKERHUB}
tags:
- "docker"
only:
- tags
devops-nginx-build:
stage: build
retry: 2
variables:
DOCKER_HOST: tcp://${DINDSERVICE}:${DINDSERVICEPORT}/
#DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
dockerfile: "Dockerfile-nginx"
before_script:
- docker login ${DOCKERHUB} -u "${DOCKERHUBUSER}" -p "${DOCKERHUBPASS}"
script:
- cp -arf ./deploy/settings.py ./devops
- docker build -f ${dockerfile} --cache-from ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}-nginx:latest -t ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}-nginx:${CI_COMMIT_TAG} -t ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}-nginx:latest .
- docker images
- docker push ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}-nginx:${CI_COMMIT_TAG}
- docker push ${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}-nginx:latest
after_script:
- docker logout ${DOCKERHUB}
tags:
- "docker"
only:
- tags
devops-invalidate-cachalot:
stage: invalidateCachalot
image: hub.leffss.com/library/${PROJECTNAME}:${CI_COMMIT_TAG}
script:
- cd /devops
- sed -i "s/mysql-service/mysql-service.${NAMESPACE}/g" devops/settings.py
- - sed -i "s/redis-service/redis-service.${NAMESPACE}/g" devops/settings.py
- python3 manage.py invalidate_cachalot
only:
- tags
.deploy_component: &deploy_component |
[ -d ~/.kube ] || mkdir ~/.kube
echo "${kube_config}" > ~/.kube/config
cp -arf deploy/${component_name}-template.yaml ${component_name}-${CI_COMMIT_TAG}.yaml
sed -i "s#{{img_url}}#${img_url}#g" ${component_name}-${CI_COMMIT_TAG}.yaml
sed -i "s#{{component_name}}#${component_name}#g" ${component_name}-${CI_COMMIT_TAG}.yaml
sed -i "s#{{NAMESPACE}}#${NAMESPACE}#g" ${component_name}-${CI_COMMIT_TAG}.yaml
kubectl apply -f ${component_name}-${CI_COMMIT_TAG}.yaml --record
echo
echo
echo "============================================================="
echo " ${component_name} Rollback Indx List"
echo "============================================================="
kubectl -n ${NAMESPACE} rollout history deployment ${component_name}
celery-beat-deploy:
stage: deployComponent
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
img_url: "${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:${CI_COMMIT_TAG}"
component_name: "celery-beat"
script:
- *deploy_component
when: on_success
only:
- tags
needs: ["devops-build"]
celery-worker-deploy:
stage: deployComponent
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
img_url: "${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:${CI_COMMIT_TAG}"
component_name: "celery-worker"
script:
- *deploy_component
when: on_success
only:
- tags
needs: ["devops-build"]
daphne-deploy:
stage: deployComponent
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
img_url: "${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:${CI_COMMIT_TAG}"
component_name: "daphne"
script:
- *deploy_component
when: on_success
only:
- tags
needs: ["devops-build"]
gunicorn-deploy:
stage: deployComponent
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
img_url: "${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:${CI_COMMIT_TAG}"
component_name: "gunicorn"
script:
- *deploy_component
when: on_success
only:
- tags
needs: ["devops-build"]
sshd-deploy:
stage: deployComponent
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
img_url: "${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}:${CI_COMMIT_TAG}"
component_name: "sshd"
script:
- *deploy_component
when: on_success
only:
- tags
needs: ["devops-build"]
nginx-deploy:
stage: deployComponent
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
img_url: "${DOCKERHUB}/${DOCKERHUBPROJECT}/${PROJECTNAME}-nginx:${CI_COMMIT_TAG}"
component_name: "nginx"
script:
- *deploy_component
when: on_success
only:
- tags
needs: ["devops-nginx-build"]
.deploy_ingress: &deploy_ingress |
[ -d ~/.kube ] || mkdir ~/.kube
echo "${kube_config}" > ~/.kube/config
date
cp -arf deploy/${component_name}-template.yaml ${component_name}-${CI_COMMIT_TAG}.yaml
sed -i "s#{{component_name}}#${component_name}#g" ${component_name}-${CI_COMMIT_TAG}.yaml
sed -i "s#{{NAMESPACE}}#${NAMESPACE}#g" ${component_name}-${CI_COMMIT_TAG}.yaml
kubectl apply -f ${component_name}-${CI_COMMIT_TAG}.yaml --record
echo
echo
echo "============================================================="
echo " ${component_name} Indx List"
echo "============================================================="
kubectl -n ${NAMESPACE} get ingress ${component_name}
ingress-deploy:
stage: deployIngress
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
component_name: "devops-ingress"
script:
- *deploy_ingress
when: on_success
only:
- tags
needs: ["daphne-deploy","gunicorn-deploy","sshd-deploy","nginx-deploy"]
confirm-rollback:
stage: rollbackConfirm
script:
- echo "rollbackConfirm"
when: manual
only:
- tags
.rollback_component: &rollback_component |
[ -d ~/.kube ] || mkdir ~/.kube
echo "${kube_config}" > ~/.kube/config
last_version=$(kubectl -n ${NAMESPACE} rollout history deployment ${component_name} | sed -n '3,$'p | tail -6 | head -5 | sed '$d' | awk -F"[ =]+" '{print $1" "$5}' | tail -1 | head -1)
last_version_num=$(echo ${last_version}|awk '{print $1}')
last_version_name=$(echo ${last_version}|awk '{print $2}')
echo
echo
echo "============================================================="
echo " ${component_name} Rollback to ${last_version_name}"
echo "============================================================="
kubectl -n ${NAMESPACE} rollout undo deployment ${component_name} --to-revision=$last_version_num
kubectl -n ${NAMESPACE} rollout history deployment ${component_name}
celery-beat-rollback:
stage: rollback
# 指定 image,不指定的话会使用 runner 配置文件 /etc/gitlab-runner/config.toml 中设置的 image
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
component_name: "celery-beat"
script:
- *rollback_component
when: on_success
only:
- tags
needs: ["confirm-rollback"]
celery-worker-rollback:
stage: rollback
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
component_name: "celery-worker"
script:
- *rollback_component
when: on_success
only:
- tags
needs: ["confirm-rollback"]
daphne-rollback:
stage: rollback
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
component_name: "daphne"
script:
- *rollback_component
when: on_success
only:
- tags
needs: ["confirm-rollback"]
gunicorn-rollback:
stage: rollback
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
component_name: "gunicorn"
script:
- *rollback_component
when: on_success
only:
- tags
needs: ["confirm-rollback"]
sshd-rollback:
stage: rollback
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
component_name: "sshd"
script:
- *rollback_component
when: on_success
only:
- tags
needs: ["confirm-rollback"]
nginx-rollback:
stage: rollback
image: hub.leffss.com/library/kubectl:v1.19.9
variables:
kube_config: "${KUBE_ADMIN_CONFIG}"
component_name: "nginx"
script:
- *rollback_component
when: on_success
only:
- tags
needs: ["confirm-rollback"]