-
Notifications
You must be signed in to change notification settings - Fork 4
/
.gitlab-ci.yml
304 lines (278 loc) · 7.88 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
variables:
### Disable cache in Docker builds, as this has occasionally resulted in images not containing what was
### expected multiple MRs where being built/retried.
NO_CACHE: 'true'
### Replace RESOURCE with the name of the image you want to build and publish in OpenShift
### Important! In order for this template to work, the name of the gitlab repo must match
### also the variable name
RESOURCE: cern-search-rest-api
### OpenShift namespace and server values
NAMESPACE: cern-search-master
NAMESPACE_DEV: test-cern-search-master
OPENSHIFT_SERVER: https://openshift.cern.ch
OPENSHIFT_SERVER_DEV: https://openshift-dev.cern.ch
### By default, there are 6 stages that we may use:
### Feel free to adapt this to your specific case.
stages:
- build_base
- check_base
- test
- build
- tag_image
- import_image # This stage is only used when the built image is stored in the GitLab Registry
- deploy
### Build the image and store it in the registry. It is important that this step
### doesn't override the image the applications are running, as we haven't tested the image yet
.build_image:
stage: build
tags:
- docker-image-build
script: 'echo "Building Docker image..."'
### Import image into OpenShift.
.import_image:
stage: import_image
image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
script:
- oc import-image ${RESOURCE}:${TAG} --token=${TOKEN} --server=${OPENSHIFT_SERVER} -n ${NAMESPACE}
### If a new tag is pushed it needs to be referenced into the ImageStream
.tag_image:
stage: tag_image
only:
- tags
image: gitlab-registry.cern.ch/paas-tools/openshift-client:latest
script:
- oc tag --source=docker ${CI_REGISTRY_IMAGE}:${CI_REGISTRY_TAG} ${RESOURCE}:${TAG} --token=${TOKEN} --server=${OPENSHIFT_SERVER} -n ${NAMESPACE}
######################################################
################### Merge Requests ###################
#'Build Base' stage
# Builds the base docker image and tags it according to the commit message.
build_base_image:
extends: .build_image
stage: build_base
only:
refs:
- merge_requests
changes:
- Dockerfile-base
- pyproject.toml
- poetry.lock
when: manual
variables:
DOCKER_FILE: Dockerfile-base
TO: gitlab-registry.cern.ch/webservices/cern-search/cern-search-rest-api/cern-search-rest-api-base:${CI_COMMIT_SHA}
# Validate dockerfile is updated with base image tag
validate-base-image-updated:
stage: check_base
only:
refs:
- merge_requests
changes:
- Dockerfile-base
- pyproject.toml
- poetry.lock
script:
- git diff --name-only origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME
- git diff --name-only origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME | grep -q -E "(^|[^-])\bDockerfile\b([^-]|$)"
# Lint
lint:
image: python:3.8
stage: test
only:
refs:
- merge_requests
script:
- make lint
before_script:
- pip install pre-commit
# Test
test:
services:
- docker:dind
variables:
# As of GitLab 12.5, privileged runners at CERN mount a /certs/client docker volume that enables use of TLS to
# communicate with the docker daemon. This avoids a warning about the docker service possibly not starting
# successfully.
# See https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
DOCKER_TLS_CERTDIR: "/certs"
# Note that we do not need to set DOCKER_HOST when using the official docker client image: it automatically
# defaults to tcp://docker:2376 upon seeing the TLS certificate directory.
#DOCKER_HOST: tcp://docker:2376/
stage: test
only:
refs:
- merge_requests
image: tmaier/docker-compose:latest
script: make ci-test MODE=test
before_script:
- docker info
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- docker-compose --version
- apk add make
- apk add openssl
tags:
- docker-privileged-xl
# Build the image and store it in the registry.
build_test_version_manual:
extends: .build_image
only:
refs:
- merge_requests
when: manual
variables:
TO: ${CI_REGISTRY_IMAGE}:$CI_COMMIT_REF_SLUG
### Create tag in OpenShift.
tag_image_merge_request_prod:
extends: .tag_image
only:
refs:
- merge_requests
environment:
name: branch/$CI_COMMIT_REF_SLUG
when: manual
variables:
CI_REGISTRY_TAG: $CI_COMMIT_REF_SLUG
TAG: $CI_COMMIT_REF_SLUG
TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
### Create tag in OpenShift DEV.
tag_image_merge_request_dev:
extends: .tag_image
only:
refs:
- merge_requests
environment:
name: branch/$CI_COMMIT_REF_SLUG
when: manual
variables:
CI_REGISTRY_TAG: $CI_COMMIT_REF_SLUG
TAG: $CI_COMMIT_REF_SLUG
TOKEN: ${SERVICE_ACCOUNT_TOKEN_DEV}
NAMESPACE: ${NAMESPACE_DEV}
OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}
# Import image into OpenShift.
import_image_merge_request_prod:
extends: .import_image
only:
refs:
- merge_requests
environment:
name: branch/$CI_COMMIT_REF_SLUG
when: manual
variables:
TAG: $CI_COMMIT_REF_SLUG
TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
# Import image into OpenShift DEV.
import_image_merge_request_dev:
extends: .import_image
only:
refs:
- merge_requests
environment:
name: branch/$CI_COMMIT_REF_SLUG
when: manual
variables:
TAG: $CI_COMMIT_REF_SLUG
TOKEN: ${SERVICE_ACCOUNT_TOKEN_DEV}
NAMESPACE: ${NAMESPACE_DEV}
OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}
######################################################
##################### Dev branch #####################
build_dev_version:
extends: .build_image
only:
- dev
### Import image into OpenShift.
import_image_dev_branch_prod:
extends: .import_image
only:
- dev
environment:
name: staging
variables:
TAG: latest
TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
### Import image into OpenShift DEV
import_image_dev_branch_dev:
extends: .import_image
only:
- dev
environment:
name: staging
variables:
TAG: latest
TOKEN: ${SERVICE_ACCOUNT_TOKEN_DEV}
NAMESPACE: ${NAMESPACE_DEV}
OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}
######################################################
####################### Tags ########################
build_tagged_version:
extends: .build_image
only:
- tags
variables:
TO: ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}
### Create tag in OpenShift.
tag_image_prod:
extends: .tag_image
environment:
name: production
variables:
CI_REGISTRY_TAG: ${CI_COMMIT_TAG}
TAG: ${CI_COMMIT_TAG}
TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
### Create tag in OpenShift DEV.
tag_image_dev:
extends: .tag_image
environment:
name: production
variables:
CI_REGISTRY_TAG: ${CI_COMMIT_TAG}
TAG: ${CI_COMMIT_TAG}
TOKEN: ${SERVICE_ACCOUNT_TOKEN_DEV}
NAMESPACE: ${NAMESPACE_DEV}
OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}
### Import image into OpenShift.
import_tagged_image_prod:
extends: .import_image
environment:
name: production
only:
- tags
variables:
TAG: ${CI_COMMIT_TAG}
TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
### Import image into OpenShift DEV.
import_tagged_image_dev:
extends: .import_image
environment:
name: production
only:
- tags
variables:
TAG: ${CI_COMMIT_TAG}
TOKEN: ${SERVICE_ACCOUNT_TOKEN_DEV}
NAMESPACE: ${NAMESPACE_DEV}
OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}
### Deploy stable into OpenShift.
deploy_production:
extends: .tag_image
stage: deploy
environment:
name: production
when: manual
variables:
CI_REGISTRY_TAG: ${CI_COMMIT_TAG}
TAG: stable
TOKEN: ${SERVICE_ACCOUNT_TOKEN_PROD}
### Deploy stable into OpenShift DEV.
deploy_dev:
extends: .tag_image
stage: deploy
environment:
name: production
when: manual
variables:
CI_REGISTRY_TAG: ${CI_COMMIT_TAG}
TAG: stable
TOKEN: ${SERVICE_ACCOUNT_TOKEN_DEV}
NAMESPACE: ${NAMESPACE_DEV}
OPENSHIFT_SERVER: ${OPENSHIFT_SERVER_DEV}