-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
306 lines (283 loc) · 11.4 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
image: outrigger/gitlab-ci-workspace:stable
services:
- docker:dind
variables:
# Ensure fast file system is used for docker:dind
DOCKER_DRIVER: overlay2
# PROJECT_NAME is ideally set in the CI/CD variables instead of declared here
# PROJECT_NAME: PUT_PROJECT_NAME_HERE
# KUBE_NAMESPACE is ideally set in the CI/CD variables instead of declared here
# Normally PROJECT_NAME-project
# KUBE_NAMESPACE: PUT_NAMESPACE_HERE
# CI_REGISTRY_IMAGE isn't used directly to structurally allow for projects with
# multiple images which may need to be built. However, the majority are
# expected to only ever need to build one image.
WEB_IMAGE: ${CI_REGISTRY_IMAGE}/web
# Set the release name
# This has to match how things line up in the deploy script
RELEASE_NAME: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}
# By default the environment name is set to the branch name.
# If you need this to be different, you'll need to create additional jobs
# specific to each different environment.
PROJECT_ENV: ${CI_COMMIT_REF_SLUG}
stages:
# tasks are intended for items which are only run manually and don't need
# a build to succeed in order to be able to execute. The idea is that they
# are always skipped in any automatic activities but come before a build so
# that if it fails it doesn't effect the ability to run a task
- tasks
- build
- validate
- deploy
- update
- test
- notify
before_script:
# Dump some debugging so we know what variables and values we have to work with.
- set
# Sanity check to make sure the project is configured.
- ./.gitlab-ci/check-settings.sh
# Ensure DOCKER_HOST is set in runner.
- source ./.gitlab-ci/check-docker.sh
# Define project-specific environment variables.
- source .env
# This job builds the code base pushes a docker image to the GitLab registry.
# This is only needed if we will spin up a K8S environment for the image.
build:
stage: build
script:
# Login so we can push images to the registry
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
# assemble the codebase
- docker-compose -f .gitlab-ci/build.yml run --rm base ./bin/init.sh -y
# bundle it into a docker image
- cp .gitlab-ci/Dockerfile Dockerfile
# Use previous build as cache for performance.
- docker build --cache-from ${WEB_IMAGE}:${CI_COMMIT_REF_SLUG} -t ${WEB_IMAGE}:${CI_COMMIT_SHA} .
# Tag it based on slug (branch name) too which can allow for easier commands
# of the form "deploy the latest version of BRANCH"
- docker tag ${WEB_IMAGE}:${CI_COMMIT_SHA} ${WEB_IMAGE}:${CI_COMMIT_REF_SLUG}
# Push all the plain tags and the qa variants and for cache
- docker push ${WEB_IMAGE}:${CI_COMMIT_SHA}
- docker push ${WEB_IMAGE}:${CI_COMMIT_REF_SLUG}
# - ./.gitlab-ci/flowdock-notify.sh "Build of ${CI_COMMIT_REF_NAME} complete for ${CI_COMMIT_TITLE}"
cache:
key: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}
paths:
- build
- vendor
- project/themes/*/node_modules
only:
- /^qa/
- /^test/
# Run validation (linting) and unit tests on already built codebase.
validate:
stage: validate
script:
- docker-compose -f .gitlab-ci/build.yml run --rm base ./bin/validate
- docker-compose -f .gitlab-ci/build.yml run --rm base ./bin/test unit
cache:
key: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}
paths:
- build
- vendor
- project/themes/*/node_modules
only:
- /^qa/
- /^test/
# Runs when a feature/* branch is commited/pushed.
# Doesn't create any environment, so just build, lint, and unit test.
validate_branch:
stage: validate
script:
# assemble the codebase
- docker-compose -f .gitlab-ci/build.yml run --rm base ./bin/init.sh -y
# Run linting etc.
- docker-compose -f .gitlab-ci/build.yml run --rm base ./bin/validate
# Run unit tests.
- docker-compose -f .gitlab-ci/build.yml run --rm base ./bin/test unit
cache:
# Pull the cache from the "test" branch.
key: ${PROJECT_NAME}-test
# Prevent cache from being updated/pushed.
policy: pull
paths:
- build
- vendor
- project/themes/*/node_modules
only:
- /^feature/
# Common script to deploy a built image to a Kubernetes cluster.
# When calling this script, be sure to set the "environment:" values.
.deploy_script: &deploy_script
script:
# Display versions of docker, etc. Comes from gitlab-ci-workspace container.
- /versions.sh
# Remove any previous manifests from cache.
- rm -rf .gitlab-ci/manifests
- mkdir .gitlab-ci/manifests
# Create a Docker Registry secret (if it doesn't exist) so we can pull images
# via imagePullSecrets. This depends on a deploy token having been created
# with name gitlab-deploy-token. See the note about the special naming at
# https://gitlab.com/help/user/project/deploy_tokens/index#read-container-registry-images
- kubectl get secret gitlab-registry-secret || kubectl create secret docker-registry gitlab-registry-secret --docker-server=${CI_REGISTRY} --docker-username=${CI_DEPLOY_USER} --docker-password=${CI_DEPLOY_PASSWORD} --docker-email=${GITLAB_USER_EMAIL}
# Download chart dependencies.
- helm dependency build .gitlab-ci/chart
# Do the deployment and output all the YAML resources deployed for this release.
- helm version
# Create env configMap from .env file.
# Remove the quotes around values since kubectl expects simple key=value
- cat .env | tr -d "\"" >.gitlab-ci/.env
- kubectl create configmap ${RELEASE_NAME}-${CI_ENVIRONMENT_SLUG}-env-config --from-env-file=.gitlab-ci/.env -o yaml --dry-run > .gitlab-ci/manifests/${RELEASE_NAME}-${CI_ENVIRONMENT_SLUG}-env-config.yaml
# Create additional values from environment.
- .gitlab-ci/env-values.sh > .gitlab-ci/env-values.yaml
# Generate the manifests from the chart templates.
- helm template -f .gitlab-ci/env-values.yaml --output-dir .gitlab-ci/manifests --name ${RELEASE_NAME} --set nameOverride=${CI_ENVIRONMENT_SLUG} --set image.repository=${WEB_IMAGE} --set image.tag=${CI_COMMIT_SHA} --set env.tier=${PROJECT_ENV} --set build_id="build-${CI_JOB_ID}" --set ingress.url.env=${PROJECT_ENV} --set ingress.url.project=${PROJECT_NAME} --debug .gitlab-ci/chart
# Remove un-needed mariadb-test pod.
- rm -f .gitlab-ci/manifests/chart/charts/mariadb/templates/test-runner.yaml
- rm -f .gitlab-ci/manifests/chart/charts/mariadb/templates/tests.yaml
# Send helm manifests to kubectl to create/update the pods.
- kubectl apply --recursive --filename .gitlab-ci/manifests
# Wait for pods to become available.
- .gitlab-ci/wait-until-pods-ready.sh ${KUBE_NAMESPACE}
# Add any post-deploy commands here, such as flowdock notifications
# - ./.gitlab-ci/flowdock-notify.sh "Deployment complete for ${CI_COMMIT_TITLE} to ${CI_ENVIRONMENT_URL}"
# Deploy the a branch to an environment.
# Use this job if your branch names match your environment names.
# e.g. branch "master" has environment www.PROJECT_NAME.master.kube.p2devcloud.com
# If you need different names, see the branch-specific deploy jobs below.
deploy:
stage: deploy
environment:
# Cannot use environment variables here, only gitlab variables.
# Also cannot use $PROJECT_ENV since GitLab doesn't support vars in vars.
name: ${CI_COMMIT_REF_SLUG}
url: http://www.${CI_COMMIT_REF_SLUG}.${PROJECT_NAME}.kube.p2devcloud.com
<<: *deploy_script
cache:
key: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}-manifest
paths:
- .gitlab-ci/manifests
only:
- /^qa/
- /^test/
# Deploy the an environment with a different branch name.
# Use this job if your want a different environment name for a branch.
# Change "ENV" to the name of the environment, and "BRANCH" to the branch.
# NOTE: You will also need to duplicate any other job that accesses the
# environment, such as the "update" and "test" jobs to specify the
# environment.name value for each one.
#
# deploy_ENV:
# stage: deploy
# variables:
# # Set the name of the desired environment here.
# PROJECT_ENV: ENV
# environment:
# # Cannot use environment variables here, only gitlab variables.
# name: ${PROJECT_ENV}
# url: http://www.${PROJECT_ENV}.${PROJECT_NAME}.kube.p2devcloud.com
# <<: *deploy_script
# cache:
# key: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}-manifest
# paths:
# - .gitlab-ci/manifests
# only:
# # Set the branch for the environment here.
# - /^BRANCH/
# Update the environment.
# Installs Drupal if needed, runs update hooks, config-import, clear-cache.
update:
stage: update
environment:
# Cannot use environment variables here, only gitlab variables.
# Also cannot use $PROJECT_ENV since GitLab doesn't support vars in vars.
name: ${CI_COMMIT_REF_SLUG}
url: http://www.${CI_COMMIT_REF_SLUG}.${PROJECT_NAME}.kube.p2devcloud.com
script:
# Useful debugging to see if drush is happy.
- ./.gitlab-ci/exec.sh cli ls -al
- ./.gitlab-ci/exec.sh cli drush status
- ./.gitlab-ci/exec.sh cli /var/www/bin/import -y
only:
- /^qa/
- /^test/
# Run full test suite that needs the running environment.
test:
stage: test
environment:
name: ${CI_COMMIT_REF_SLUG}
script:
- ./.gitlab-ci/exec.sh cli /var/www/bin/test
only:
- /^qa/
- /^test/
# Manual task to reinstall Drupal when needed.
install:
stage: tasks
environment:
name: ${CI_COMMIT_REF_SLUG}
script:
- ./.gitlab-ci/exec.sh cli /var/www/bin/install -y
only:
- /^qa/
- /^test/
when: manual
# Purge the Kubernetes environment manually when needed.
clean_up:
stage: tasks
environment:
# Cannot use environment variables here, only gitlab variables.
# Also cannot use $PROJECT_ENV since GitLab doesn't support vars in vars.
name: ${CI_COMMIT_REF_SLUG}
url: http://www.${CI_COMMIT_REF_SLUG}.${PROJECT_NAME}.kube.p2devcloud.com
script:
- ls -al .gitlab-ci
# Remove everything that was deployed for this environment.
- kubectl delete --recursive --filename .gitlab-ci/manifests
cache:
key: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}-manifest
paths:
- .gitlab-ci/manifests
only:
- /^qa/
- /^test/
when: manual
# Notify when pipeline succeeds.
success:
stage: notify
script:
# Notify Flowdock that pipeline finished successfully.
- ./.gitlab-ci/flowdock-notify.sh ":green_heart:Deployment successful for [${CI_COMMIT_SHORT_SHA}](${CI_PIPELINE_URL}) ${CI_COMMIT_TITLE} to ${PROJECT_ENV}"
when: on_success
only:
- /^qa/
- /^test/
# Notify when main pipeline branches fails.
failure:
stage: notify
script:
# Send failure notification to main Flow with emoji.
- ./.gitlab-ci/flowdock-notify.sh ":broken_heart:Deployment failed for [${CI_COMMIT_SHORT_SHA}](${CI_PIPELINE_URL}) ${CI_COMMIT_TITLE} to ${PROJECT_ENV}"
when: on_failure
only:
- /^qa/
- /^test/
# Notify when feature branches succeeds.
success_branch:
stage: notify
script:
# Send failure notification to flow inbox (no emoji allowed :( ).
- ./.gitlab-ci/flowdock-notify.sh inbox "Deployment PASSSED to ${PROJECT_ENV}" "for <a href="${CI_PIPELINE_URL}">${CI_COMMIT_SHORT_SHA}</a> ${CI_COMMIT_TITLE}"
when: on_success
only:
- /^feature/
# Notify when feature branches fails.
failure_branch:
stage: notify
script:
# Send failure notification to flow inbox (no emoji allowed :( ).
- ./.gitlab-ci/flowdock-notify.sh inbox "Deployment failed to ${PROJECT_ENV}" "for <a href="${CI_PIPELINE_URL}">${CI_COMMIT_SHORT_SHA}</a> ${CI_COMMIT_TITLE}"
when: on_failure
only:
- /^feature/