-
Notifications
You must be signed in to change notification settings - Fork 7
/
.gitlab-ci.yml
288 lines (261 loc) · 7.12 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
image: docker:18.09.7
services:
- docker:18.09.7-dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2
# Temporary pipeline build artifact
BODS_IMAGE_NAME: "ito-transit-bodp-django"
CURRENT_IMAGE: ${REGISTRY_ADDRESS}:$CI_COMMIT_SHA
CURRENT_DEV_IMAGE: ${REGISTRY_ADDRESS}:${CI_COMMIT_SHA}-dev
LATEST_IMAGE: ${REGISTRY_ADDRESS}:latest
LATEST_DEV_IMAGE: ${REGISTRY_ADDRESS}:latest-dev
# Base image
BASE_IMAGE_NAME: "ito-transit-bodp-base"
BASE_IMAGE_URI: ${REGISTRY_ADDRESS}/${BASE_IMAGE_NAME}:latest
POSTGRES_HOST: postgres
POSTGRES_PORT: "5432"
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
BLACK_VERSION: 22.3.0
stages:
- build
- test
- release
# UTILITIES
# Adds timestamps to log output. Also works with Alpine
.logging: &logging |-
use_printf=`printf "%(true)T\n" -1 2>/dev/null` || use_printf=false
use_awk=`awk 'BEGIN{print(strftime("true"));}'` || use_awk=false
echo "logging: use_printf=$use_printf; use_awk=$use_awk"
if $use_printf ; then
logging_pipe=/tmp/$$.logging
mkfifo $logging_pipe
(set +x;while read -r line;do printf "[%(%Y/%m/%d %H:%M:%S)T] %s\n" -1 "$line";done) <$logging_pipe&
exec >$logging_pipe 2>&1
rm $logging_pipe
elif $use_awk ; then
logging_pipe=/tmp/$$.logging
mkfifo $logging_pipe
awk '{printf("[%s] %s\n",strftime("%Y/%m/%d %H:%M:%S"),$0);}' <$logging_pipe&
exec >$logging_pipe 2>&1
rm $logging_pipe
else
echo "Can't setup timestamped logging."
fi
echo "logging: Started."
before_script:
- *logging
- docker info
- >
apk update
&& apk add --no-cache python3 python3-dev py3-pip py3-virtualenv
&& pip3 install --upgrade pip setuptools
&& ln -sf /usr/bin/pip3 /usr/bin/pip
&& ln -sf /usr/bin/python3 /usr/bin/python
- python3 -m venv .venv
- source .venv/bin/activate
- pip install -U pip
- pip install awscli==1.24.1
- $(aws ecr get-login --no-include-email --region eu-west-1) # will pick up secret keys set in GitLab UI
# Base tasks
.dev-build:
stage: build
script:
- docker pull $LATEST_DEV_IMAGE || true
- docker pull $CURRENT_DEV_IMAGE || true
- >
docker build
--file docker/local/django/Dockerfile
--cache-from $LATEST_DEV_IMAGE
--cache-from $CURRENT_DEV_IMAGE
--build-arg BASE_IMAGE=$BASE_IMAGE_URI
--tag $CURRENT_DEV_IMAGE
.
- docker push $CURRENT_DEV_IMAGE
.prod-build:
stage: build
script:
- docker pull $LATEST_IMAGE || true
- docker pull $CURRENT_IMAGE || true
- >
docker build
--file docker/production/django/Dockerfile
--cache-from $LATEST_IMAGE
--cache-from $CURRENT_IMAGE
--build-arg BASE_IMAGE=$BASE_IMAGE_URI
--tag $CURRENT_IMAGE
.
- docker push $CURRENT_IMAGE
# MERGE REQUESTS TASKS
# Merge requests must be used to integrate code into any of the target branches: develop, release/* and master
# Base task which ignores any changes to other components
.merge-request-task:
only:
refs:
- merge_requests
changes:
- "**/*"
build-dev:
extends:
- .merge-request-task
- .dev-build
.test-dev:
stage: test
variables:
# Configures Django service
DJANGO_PARENT_HOST: bods.local
REDIS_URL: redis://redis:6379/0
CELERY_BROKER_URL: redis://redis:6379/0
DJANGO_SETTINGS_MODULE: config.settings.local
EMAIL_HOST: localhost
USE_DOCKER: "yes"
script:
- echo '127.0.0.1 www.bods.local data.bods.local publish.bods.local admin.bods.local' >> /etc/hosts
- docker pull $CURRENT_DEV_IMAGE
# Run Postgres
- >
docker run
-d --rm
--name postgres
-e POSTGRES_HOST
-e POSTGRES_PORT
-e POSTGRES_DB
-e POSTGRES_USER
-e POSTGRES_PASSWORD
mdillon/postgis:10
# run BODS
- >
docker run
--rm
--link=postgres
-e REDIS_URL
-e GOV_NOTIFY_API_KEY
-e API_SITE_URL
-e DJANGO_SETTINGS_MODULE
-e USE_DOCKER
-e DJANGO_PARENT_HOST
-e DQS_URL
-e CAVL_URL
-e CAVL_CONSUMER_URL
-e CAVL_VALIDATION_URL
-e AVL_CONSUMER_API_BASE_URL
-e AVL_PRODUCER_API_BASE_URL
-e AVL_PRODUCER_API_KEY
-e POSTGRES_HOST
-e POSTGRES_PORT
-e POSTGRES_DB
-e POSTGRES_USER
-e POSTGRES_PASSWORD
$CURRENT_DEV_IMAGE
sh -c "pytest --numprocesses=4 ."
coverage: "/TOTAL.+ ([0-9]{1,3}%)/"
test-merge-request:
extends:
- .merge-request-task
- .test-dev
lint:
extends: .merge-request-task
stage: test
image: python:3.9.7
before_script:
- *logging
- pip install black==$BLACK_VERSION
script:
- black --check --config .black.toml .
# POST-MERGE TASKS
# These tasks execute after a merge request has been tested, reviewed and merged into the target branch
# The production image is built and tag as either 'latest', 'staging' or 'stable' and released to ECR
build-and-release-base-image:
stage: build
script:
- cd docker/production/django_base
- docker pull $BASE_IMAGE_URI || true
- >
docker build
--file ./Dockerfile
--cache-from $BASE_IMAGE_URI
--tag ${BASE_IMAGE_URI} .
- docker push ${BASE_IMAGE_URI}
only:
- develop
build-latest-dev:
extends:
- .dev-build
only:
- develop
test-latest-dev:
extends:
- .test-dev
only:
- develop
build-latest-prod:
extends:
- .prod-build
only:
- tags
- /^release/.*$/
- develop
release-latest-prod:
stage: release
script:
- docker pull $CURRENT_IMAGE
- docker tag $CURRENT_IMAGE $LATEST_IMAGE
- docker push $LATEST_IMAGE
only:
- develop
release-latest-dev:
stage: release
script:
- docker pull $CURRENT_DEV_IMAGE
- docker tag $CURRENT_DEV_IMAGE $LATEST_DEV_IMAGE
- docker push $LATEST_DEV_IMAGE
only:
- develop
release-staging:
stage: release
variables:
STAGING_IMAGE: ${REGISTRY_ADDRESS}:staging
script:
- docker pull $CURRENT_IMAGE
- docker tag $CURRENT_IMAGE $STAGING_IMAGE
- docker push $STAGING_IMAGE
only:
- /^release/.*$/
release-prod:
stage: release
variables:
PROD_IMAGE: ${REGISTRY_ADDRESS}:${CI_COMMIT_REF_NAME}
STABLE_IMAGE: ${REGISTRY_ADDRESS}:stable
script:
- docker pull $CURRENT_IMAGE
- docker tag $CURRENT_IMAGE $PROD_IMAGE
- docker tag $CURRENT_IMAGE $STABLE_IMAGE
- docker push $PROD_IMAGE
- docker push $STABLE_IMAGE
only:
- tags
except:
- branches
release-prod-kpmg:
stage: release
variables:
AWS_DEFAULT_REGION: "eu-west-2"
KPMG_IMAGE_NAME: "ito_transit_bodp_django"
IMAGE_TAG: "${CI_COMMIT_TAG}"
script:
- export KPMG_IMAGE=${KPMG_REGISTRY_ADDRESS}/${KPMG_IMAGE_NAME}:${IMAGE_TAG}
- echo $KPMG_IMAGE
- docker pull $CURRENT_IMAGE
- docker tag $CURRENT_IMAGE $KPMG_IMAGE
# Explict setting of KPMG env vars as Ito's details were being used
# when they were just being set a variables.
- export AWS_ACCESS_KEY_ID=${KPMG_AWS_ACCESS_KEY_ID}
- export AWS_SECRET_ACCESS_KEY=${KPMG_AWS_SECRET_ACCESS_KEY}
- eval $(aws ecr get-login --no-include-email)
- docker push $KPMG_IMAGE
only:
- tags
except:
- branches