-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
373 lines (352 loc) · 11.5 KB
/
.gitlab-ci.yml
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
variables:
DOCKER_PRODUCTION: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
DOCKER_REVIEW: $CI_REGISTRY_IMAGE/review:$CI_COMMIT_REF_SLUG
stages:
- build base
- build
- test
- deploy
- release
- publish
- cleanup
include:
- template: Code-Quality.gitlab-ci.yml
# To use Kaniko extend the job and set the variable for DOCKER_DESTINATION and optionally DOCKER_FILE
.use-kaniko:
tags:
- kubernetes
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
before_script:
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
script:
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $DOCKER_FILE --destination $DOCKER_DESTINATION
build_base:
stage: build base
extends: .use-kaniko
variables:
DOCKER_FILE: $CI_PROJECT_DIR/docker/Dockerfile.base
rules:
- if: ($CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main")
changes:
- docker/Dockerfile.base
- requirements/base.txt
- requirements/production.txt
variables:
DOCKER_DESTINATION: $CI_REGISTRY_IMAGE/base:master
- if: ($CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main")
when: manual
allow_failure: true
variables:
DOCKER_DESTINATION: $CI_REGISTRY_IMAGE/base:master
- if: ($CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
changes:
- docker/Dockerfile.base
- requirements/base.txt
- requirements/production.txt
variables:
DOCKER_DESTINATION: $CI_REGISTRY_IMAGE/base:beta
- if: ($CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
when: manual
allow_failure: true
variables:
DOCKER_DESTINATION: $CI_REGISTRY_IMAGE/base:beta
- if: $CI_COMMIT_BRANCH
changes:
- docker/Dockerfile.base
- requirements/base.txt
- requirements/production.txt
variables:
DOCKER_DESTINATION: $CI_REGISTRY_IMAGE/base:review
- if: $CI_COMMIT_BRANCH
when: manual
allow_failure: true
variables:
DOCKER_DESTINATION: $CI_REGISTRY_IMAGE/base:review
build_production:
stage: build
extends: .use-kaniko
variables:
DOCKER_FILE: $CI_PROJECT_DIR/docker/production/django/Dockerfile
rules:
- if: ($CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main")
variables:
DOCKER_BASE_IMAGE: $CI_REGISTRY_IMAGE/base:master
DOCKER_DESTINATION: $DOCKER_PRODUCTION
- if: ($CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
variables:
DOCKER_BASE_IMAGE: $CI_REGISTRY_IMAGE/base:beta
DOCKER_DESTINATION: $DOCKER_PRODUCTION
- if: $CI_COMMIT_BRANCH
variables:
DOCKER_BASE_IMAGE: $CI_REGISTRY_IMAGE/base:review
DOCKER_DESTINATION: $DOCKER_REVIEW
script:
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $DOCKER_FILE --destination $DOCKER_DESTINATION --build-arg BASE_IMAGE=$DOCKER_BASE_IMAGE
code_quality:
rules:
- if: ($CODE_QUALITY_DISABLED || $CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG)
when: never
- if: $CI_COMMIT_BRANCH
before_script:
- echo -e "[pycodestyle]\nmax-line-length = 119" > setup.cfg
- echo -e "rule 'MD013', :line_length = 119" > .mdlrc
.test-base:
stage: test
image:
name: $DOCKER_IMAGE
pull_policy: always
needs:
- job: build_production
tags:
- kubernetes
rules:
- if: ($CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
variables:
DOCKER_IMAGE: $CI_REGISTRY_IMAGE/base:master
- if: $CI_COMMIT_BRANCH == "beta"
variables:
DOCKER_IMAGE: $CI_REGISTRY_IMAGE/base:beta
- if: $CI_COMMIT_BRANCH
variables:
DOCKER_IMAGE: $CI_REGISTRY_IMAGE/base:review
.test-base-services:
extends: .test-base
variables:
DJANGO_SECRET_KEY: $CI_COMMIT_SHA
DJANGO_ALLOWED_HOSTS: "*"
DJANGO_EMAIL_BACKEND: django.core.mail.backends.locmem.EmailBackend
DJANGO_SECURE_SSL_REDIRECT: "False"
REDIS_URL: redis://redis:6379/0
CELERY_BROKER_URL: $REDIS_URL
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_USER: anna
POSTGRES_PASSWORD: pass
POSTGRES_DB: askanna_backend
services:
- name: postgres:12
alias: postgres
- name: redis:7
alias: redis
pytest:
extends: .test-base-services
variables:
KUBERNETES_CPU_REQUEST: 2
KUBERNETES_MEMORY_REQUEST: 2Gi
before_script:
- pip install -r requirements/local.txt
script:
- python manage.py collectstatic --no-input
- pytest --cov=apps --cov-config=pyproject.toml --cov-report xml --cov-report html --cov-report term --junitxml=junit.xml
coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/'
artifacts:
expire_in: 2 weeks
name: $CI_JOB_NAME-$CI_COMMIT_REF_SLUG
paths:
- htmlcov/
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
junit: junit.xml
migrations:
extends: .test-base-services
script:
- python manage.py makemigrations --dry-run --check --no-input
lint:
extends: .test-base
before_script:
- pip install -r requirements/local.txt
script:
- ruff check .
.deploy-variables:
variables:
GIT_STRATEGY: none
ASKANNA_DEPLOY_BRANCH: main
ASKANNA_DEPLOY_PRODUCT: askanna-backend
.deploy:
stage: deploy
image: curlimages/curl
extends: .deploy-variables
tags:
- kubernetes
script:
- curl -X POST -F token=$CI_JOB_TOKEN -F ref=$ASKANNA_DEPLOY_BRANCH -F variables[product]=$ASKANNA_DEPLOY_PRODUCT -F variables[action]=deploy -F variables[env_name]=$ENV_NAME -F variables[env_host]=$ENV_HOST -F variables[backend_image]=$DOCKER_IMAGE $ASKANNA_DEPLOY_TRIGGER
environment:
name: $ENV_NAME
url: https://$ENV_HOST/v1/docs/swagger/
deploy_review:
extends: .deploy
rules:
- if: ($CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
when: never
- if: $CI_COMMIT_BRANCH
variables:
ENV_NAME: $CI_COMMIT_REF_SLUG
ENV_HOST: $CI_ENVIRONMENT_SLUG-api.askanna.eu
DOCKER_IMAGE: $DOCKER_REVIEW
needs:
- job: build_production
- job: migrations
environment:
name: $ENV_NAME
url: https://$ENV_HOST/v1/docs/swagger/
on_stop: stop_review
deploy_production:
extends: .deploy
rules:
- if: ($CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main")
when: manual
variables:
ENV_NAME: master
ENV_HOST: api.askanna.eu
DOCKER_IMAGE: $DOCKER_PRODUCTION
- if: ($CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
when: manual
variables:
ENV_NAME: beta
ENV_HOST: beta-api.askanna.eu
DOCKER_IMAGE: $DOCKER_PRODUCTION
needs:
- job: build_production
- job: pytest
- job: migrations
stop_review:
stage: cleanup
image: curlimages/curl
extends: .deploy-variables
variables:
ENV_NAME: $CI_COMMIT_REF_SLUG
rules:
- if: ($CI_COMMIT_BRANCH == "master" || $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
when: never
- if: $CI_COMMIT_BRANCH
when: manual
needs:
- job: deploy_review
environment:
name: $ENV_NAME
action: stop
allow_failure: true
tags:
- kubernetes
script:
- curl -X POST -F token=$CI_JOB_TOKEN -F ref=$ASKANNA_DEPLOY_BRANCH -F variables[product]=$ASKANNA_DEPLOY_PRODUCT -F variables[action]=undeploy -F variables[branch]=$CI_COMMIT_REF_SLUG -F variables[slug]=$CI_ENVIRONMENT_SLUG $ASKANNA_DEPLOY_TRIGGER
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
- if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/
script:
- echo "Creating a new release..."
release:
tag_name: $CI_COMMIT_TAG
description: $CI_COMMIT_TAG_MESSAGE
publish_to_gitlab:
stage: publish
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
optional: true
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
TARGET: refs/tags/$CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH
when: manual
variables:
TARGET: HEAD:$CI_COMMIT_BRANCH
before_script:
- apt-get update -y && apt-get install openssh-client git curl -y
- eval $(ssh-agent -s)
- echo "${SSH_PRIVATE_KEY_GITLAB_COM}" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- git config --global user.email "[email protected]"
- git config --global user.name "AskAnna Robot"
script:
- git remote add gitlab [email protected]:askanna/askanna-backend.git
- git push gitlab $TARGET
publish_release_to_gitlab:
stage: publish
image: registry.gitlab.com/gitlab-org/release-cli:latest
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
- job: publish_to_gitlab
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
GITLAB_PRIVATE_TOKEN: $GITLAB_COM_API_TOKEN
script:
- release-cli --server-url https://gitlab.com --project-id 46875844 create --tag-name $CI_COMMIT_TAG --description "$CI_COMMIT_TAG_MESSAGE"
publish_to_github:
stage: publish
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
optional: true
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
TARGET: refs/tags/$CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH
when: manual
variables:
TARGET: HEAD:$CI_COMMIT_BRANCH
before_script:
- apt-get update -y && apt-get install openssh-client git curl -y
- eval $(ssh-agent -s)
- echo "${SSH_PRIVATE_KEY_GITHUB_COM}" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan github.com >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- git config --global user.email "[email protected]"
- git config --global user.name "AskAnna Robot"
script:
- git remote add github [email protected]:askanna-io/askanna-backend.git
- git push github $TARGET
publish_release_to_github:
image: ubuntu:22.04
stage: publish
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
- job: publish_to_github
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
GH_TOKEN: $GITHUB_COM_API_TOKEN
before_script:
- apt-get update -y && apt-get install curl -y
- curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
- chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null
- apt-get update -y && apt-get install gh -y
script:
- gh release create $CI_COMMIT_TAG --verify-tag --title $CI_COMMIT_TAG --notes "$CI_COMMIT_TAG_MESSAGE" --repo askanna-io/askanna-backend