-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.gitlab-ci.yml
368 lines (340 loc) · 11.2 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
stages:
- test
- build
- build-docker
- deploy
variables:
DEV_BRANCH: "master"
STG_BRANCH: "staging"
PROD_BRANCH: "production"
test frontend-component:
stage: test
image: cypress/browsers:node17.8.0-chrome99-ff97-slim
tags:
- docker
rules:
- if: "$CI_MERGE_REQUEST_ID != null"
changes:
- frontend/**/*
- .gitlab-ci.yml
when: always
- if: "$CI_COMMIT_BRANCH == $DEV_BRANCH || $CI_COMMIT_BRANCH == $STG_BRANCH || $CI_COMMIT_BRANCH == $PROD_BRANCH"
when: always
# Always build/test for schedule / manual "Run Pipeline" via web
- if: $CI_PIPELINE_SOURCE == "schedule"
- if: $CI_PIPELINE_SOURCE == "web"
before_script:
- cd frontend
script:
- npm ci --cache .npm --prefer-offline
- npm run test:component
- npm run coverage
cache:
key:
files:
- package-lock.json
paths:
- frontend/.npm/
- frontend/node_modules/
test frontend-e2e:
stage: test
image: cypress/browsers:node16.14.2-slim-chrome103-ff102
tags:
- docker
rules:
- if: "$CI_MERGE_REQUEST_ID != null"
changes:
- frontend/**/*
- .gitlab-ci.yml
when: always
- if: "$CI_COMMIT_BRANCH == $DEV_BRANCH || $CI_COMMIT_BRANCH == $STG_BRANCH || $CI_COMMIT_BRANCH == $PROD_BRANCH"
when: always
# Always build/test for schedule / manual "Run Pipeline" via web
- if: $CI_PIPELINE_SOURCE == "schedule"
- if: $CI_PIPELINE_SOURCE == "web"
before_script:
- cd frontend
script:
- npm ci --cache .npm --prefer-offline
- npm run test:e2e
- npm run coverage
cache:
key:
files:
- package-lock.json
paths:
- frontend/.npm/
- frontend/node_modules/
# This CI-CD job will run the fluent logger tests for each commit pushed to the master branch
# If any test results in a failure, then the pipeline will fail
test fluent-logger:
stage: test
image: python:3.9.1-buster
tags:
- docker
variables:
PYTHONPATH: "$CI_PROJECT_DIR/baytree_app"
DOMAIN: "example.com"
SECRET_KEY: "test"
MYSQL_HOST: baytree_database
MYSQL_DATABASE: baytree
MYSQL_USER: theuser
MYSQL_PASSWORD: thepassword
EMAIL_HOST: ""
EMAIL_PORT: ""
EMAIL_USER: ""
EMAIL_PASSWORD: ""
VIEWS_USERNAME: ""
VIEWS_PASSWORD: ""
CELERY_BROKER_URL: ""
CELERY_BACKEND_URL: ""
before_script:
- cd shared-apps
- python setup.py sdist
- python -m pip install --user ./dist/shared-apps-0.1.tar.gz
- cd ../baytree_app
script:
- pip install -r requirements.txt
- cd baytree_app/testing
- python -m unittest FluentLoggerTests.py
after_script:
- cd baytree_app/server_logs
- cat server_application.log
- cat server_requests.log
build info:
# Always runs: allows pipeline execution to happen even if no other tasks run.
stage: build
image: alpine:latest
tags:
- docker
rules:
- if: $CI_PIPELINE_SOURCE != "push"
script:
- echo "This task give the CI/CD pipeline something to (almost always) run."
- echo " CI_PIPELINE_SOURCE = $CI_PIPELINE_SOURCE"
- echo " CI_MERGE_REQUEST_ID = $CI_MERGE_REQUEST_ID"
- echo " CI_COMMIT_BRANCH = $CI_COMMIT_BRANCH"
build frontend:
stage: build
image: cypress/browsers:node16.14.2-slim-chrome103-ff102
tags:
- docker
rules:
- if: "$CI_MERGE_REQUEST_ID != null"
changes:
- frontend/**/*
- .gitlab-ci.yml
when: always
- if: "$CI_COMMIT_BRANCH == $DEV_BRANCH || $CI_COMMIT_BRANCH == $STG_BRANCH || $CI_COMMIT_BRANCH == $PROD_BRANCH"
when: always
# Always build/test for schedule / manual "Run Pipeline" via web
- if: $CI_PIPELINE_SOURCE == "schedule"
- if: $CI_PIPELINE_SOURCE == "web"
before_script:
- cd frontend
- npm ci --cache .npm --prefer-offline
script:
- GENERATE_SOURCEMAP=false npm run build
after_script:
- cd frontend/build && tar -czvf ../../frontend_build.tar.gz .
artifacts:
expire_in: 1 week
paths:
- frontend_build.tar.gz
cache:
key:
files:
- package-lock.json
paths:
- frontend/.npm/
- frontend/node_modules/
build admin-frontend:
stage: build
image: node:14
tags:
- docker
rules:
- if: "$CI_MERGE_REQUEST_ID != null"
changes:
- admin-frontend/**/*
- .gitlab-ci.yml
when: always
- if: "$CI_COMMIT_BRANCH == $DEV_BRANCH || $CI_COMMIT_BRANCH == $STG_BRANCH || $CI_COMMIT_BRANCH == $PROD_BRANCH"
when: always
# Always build/test for schedule / manual "Run Pipeline" via web
- if: $CI_PIPELINE_SOURCE == "schedule"
- if: $CI_PIPELINE_SOURCE == "web"
before_script:
- cd admin-frontend
- npm ci
script:
- GENERATE_SOURCEMAP=false npm run export
after_script:
- ls -l admin-frontend
- cd admin-frontend/build && tar -czvf ../../admin_frontend_build.tar.gz .
artifacts:
expire_in: 1 week
paths:
- admin_frontend_build.tar.gz
build server:
stage: build
image: python:3.9.1-buster
services:
- name: mariadb:10.9.4
alias: test_mysql
- name: fluent/fluent-bit
alias: fluent-bit
tags:
- docker
rules:
- if: "$CI_MERGE_REQUEST_ID != null"
changes:
- baytree_app/**/*
- .gitlab-ci.yml
when: always
- if: "$CI_COMMIT_BRANCH == $DEV_BRANCH || $CI_COMMIT_BRANCH == $STG_BRANCH || $CI_COMMIT_BRANCH == $PROD_BRANCH"
when: always
# Always build/test for schedule / manual "Run Pipeline" via web
- if: $CI_PIPELINE_SOURCE == "schedule"
- if: $CI_PIPELINE_SOURCE == "web"
variables:
DOMAIN: "example.com"
SECRET_KEY: "test"
MYSQL_HOST: baytree_database
MYSQL_DATABASE: baytree
MYSQL_USER: theuser
MYSQL_PASSWORD: thepassword
EMAIL_HOST: ""
EMAIL_PORT: ""
EMAIL_USER: ""
EMAIL_PASSWORD: ""
VIEWS_USERNAME: ""
VIEWS_PASSWORD: ""
CELERY_BROKER_URL: ""
CELERY_BACKEND_URL: ""
before_script:
- cd shared-apps
- python setup.py sdist
- python -m pip install --user ./dist/shared-apps-0.1.tar.gz
- cd ../baytree_app
script:
- pip install -r requirements.txt
- python manage.py check
- python manage.py makemigrations --check
.deploy-script: &export_image_tag
# Set IMAGE_TAG to encode the date and SHA of the most recent commit.
- export IMAGE_TAG=v$(git show -s --format=%cs $CI_COMMIT_SHA).`git rev-parse --short=8 $CI_COMMIT_SHA`
# Display some useful info for understanding / debugging the pipeline
- echo "Docker hub user ='$HUB_USER', version='$IMAGE_TAG'"
- echo "Full tag (backend) is = $HUB_USER/$HUB_REPO_BACKEND:$IMAGE_TAG"
- echo "Full tag (rev-proxy & frontend) is = $HUB_USER/$HUB_REPO_FRONTEND:$IMAGE_TAG"
- docker images
build-docker publish-docker-images:
stage: build-docker
tags:
# Run on CI/CD server so it does not interfere with containers in active use on dev server.
- deploy-dockerhub-shell
rules:
- if: "$CI_COMMIT_BRANCH == $DEV_BRANCH"
- if: $CI_PIPELINE_SOURCE == "web"
script:
# Required environment variables defined by GitLab's settings:
# HUB_USER: Docker Hub user
# HUB_TOKEN: Docker Hub token
# HUB_REPO_BACKEND: Docker Hub project name for backend
# HUB_REPO_FRONTEND: Docker Hub project name for frontend / caddy
# Build images (tagged by docker-composition*.yml using IMAGE_TAG)
- *export_image_tag
- docker compose -f docker-compose.yml -f docker-compose.deploy.yml build
# Push images to Docker Hub
- docker images
- docker login -u $HUB_USER -p $HUB_TOKEN
- docker push $HUB_USER/$HUB_REPO_BACKEND:$IMAGE_TAG
- docker push $HUB_USER/$HUB_REPO_FRONTEND:$IMAGE_TAG
- docker logout
# Cleanup the image and any tags of that image
# Run on the CI/CD server, so it can force delete these images because it does not run any of them as containers
- docker images -a | grep -e "$HUB_REPO_BACKEND" -e "$HUB_REPO_FRONTEND" | awk '{print $3}' | xargs docker rmi --force
- docker images
.deploy-script: &deploy-from-dockerhub
- *export_image_tag
- cp /var/baytree/.env ./.env
- docker compose -f docker-compose.yml -f docker-compose.deploy.yml pull
- docker compose -f docker-compose.yml -f docker-compose.deploy.yml up -d
- docker image prune -f # remove unused images since we just downloaded new ones
- bash -c 'sleep 15'
- docker exec baytree_server python manage.py migrate
- docker exec baytree_server python manage.py loaddata defaultPreferences.json
- docker exec baytree_server python manage.py loaddata goalCategories
- docker exec baytree_server python manage.py loaddata ukHolidays
- docker images
- docker ps -a
- docker volume ls
.deploy-script: &retag-and-upload-dockerhub
# Required environment variables passed into this function:
# BRANCH_TAG: Determined by which branch we are building off: "dev", "staging", "prod"
- *export_image_tag
# Download images (docker skips if already be present)
- echo "Trying to tag images of version '$IMAGE_TAG' with new tag '$BRANCH_TAG'"
- |
echo "If you get 'Error response from daemon - manifest for ... not found - manifest unknown - manifest unknown'"
echo " it likely means the pipeline did not build and deploy a docker image for this commit."
echo " Was there a MR to this branch that did not get build for use on Dev server?"
- echo "Downloading image = $HUB_USER/$HUB_REPO_BACKEND, tag = $IMAGE_TAG'..."
- docker pull $HUB_USER/$HUB_REPO_BACKEND:$IMAGE_TAG
- docker pull $HUB_USER/$HUB_REPO_FRONTEND:$IMAGE_TAG
# Retag
- docker tag $HUB_USER/$HUB_REPO_BACKEND:$IMAGE_TAG $HUB_USER/$HUB_REPO_BACKEND:$BRANCH_TAG
- docker tag $HUB_USER/$HUB_REPO_FRONTEND:$IMAGE_TAG $HUB_USER/$HUB_REPO_FRONTEND:$BRANCH_TAG
# Upload tags
- docker images
- docker login -u $HUB_USER -p $HUB_TOKEN
- docker push $HUB_USER/$HUB_REPO_BACKEND:$BRANCH_TAG
- docker push $HUB_USER/$HUB_REPO_FRONTEND:$BRANCH_TAG
- docker logout
# Cleanup (if in use as a container this will fail, so we tolerate failures)
- docker rmi $HUB_USER/$HUB_REPO_BACKEND:$BRANCH_TAG || true
- docker rmi $HUB_USER/$HUB_REPO_FRONTEND:$BRANCH_TAG || true
- docker rmi $HUB_USER/$HUB_REPO_BACKEND:$IMAGE_TAG || true
- docker rmi $HUB_USER/$HUB_REPO_FRONTEND:$IMAGE_TAG || true
- docker images
deploy development:
stage: deploy
environment:
name: development
url: https://baytree-dev.cmpt.sfu.ca
tags:
- deploy-development
variables:
BRANCH_TAG: "dev"
rules:
- if: "$CI_COMMIT_BRANCH == $DEV_BRANCH"
script:
# After images are built, deploy to dev-server & tag with "dev"
- *deploy-from-dockerhub
- *retag-and-upload-dockerhub
deploy staging:
stage: deploy
environment:
name: staging
url: https://baytree-stg.cmpt.sfu.ca
tags:
- deploy-staging
variables:
BRANCH_TAG: "staging"
rules:
- if: "$CI_COMMIT_BRANCH == $STG_BRANCH"
script:
- *deploy-from-dockerhub
- *retag-and-upload-dockerhub
deploy production:
stage: deploy
tags:
- deploy-dockerhub-shell
variables:
BRANCH_TAG: "prod"
rules:
- if: "$CI_COMMIT_BRANCH == $PROD_BRANCH"
script:
- *retag-and-upload-dockerhub
# Production server's admin must manually log into the server and run the /root/update.sh script