forked from himerus/octane-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
370 lines (346 loc) · 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
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
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 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}
# Specify the name of the main branch to use as starting cache for building
# feature branches
# MAIN_BRANCH: qa
MAIN_BRANCH: master
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
- cleanup
before_script:
# Dump environment so we know what variables and values we have to work with.
- set
# Sanity check to make sure the project is configured.
- ./.gitlab-ci/bin/check-settings.sh
# Ensure DOCKER_HOST is set in runner.
- source ./.gitlab-ci/bin/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:
# Call common deploy script. Should not need to modify.
- .gitlab-ci/bin/build.sh
# Add any post-build commands here, such as flowdock notifications
# - ./.gitlab-ci/bin/flowdock-notify.sh "Build complete for ${CI_COMMIT_TITLE} to ${CI_ENVIRONMENT_URL}"
cache:
key: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}
paths:
- docroot
- build
- vendor
- project/themes/*/node_modules
only:
- /^qa/
- /^test/
- /^develop/
- /^master/
# 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,kernel
cache:
key: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}
paths:
- docroot
- build
- vendor
- project/themes/*/node_modules
only:
- /^qa/
- /^test/
- /^develop/
- /^master/
# Runs when a feature/* branch is commited/pushed.
# Doesn't create any environment, so just build, lint, and unit test.
validate_feature:
stage: validate
script:
# assemble the codebase
- docker-compose -f .gitlab-ci/build.yml run --rm base ./bin/make -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,kernel
cache:
# Pull the cache from the main branch.
# Speeds up feature branch build so not starting from scratch.
key: ${PROJECT_NAME}-${MAIN_BRANCH}
# Prevent cache from being updated/pushed.
policy: pull
paths:
- docroot
- build
- vendor
- project/themes/*/node_modules
only:
- /^feature/
# 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
on_stop: x_destroy_env
script:
# Call common deploy script. Should not need to modify.
- .gitlab-ci/bin/deploy.sh
# Add any post-deploy commands here, such as flowdock notifications
# - ./.gitlab-ci/bin/flowdock-notify.sh "Deployment complete for ${CI_COMMIT_TITLE} to ${CI_ENVIRONMENT_URL}"
artifacts:
# Save the manifests from the deployment so it can be deleted later.
name: ${PROJECT_NAME}-${CI_COMMIT_REF_SLUG}-manifests
# Don't expire these since normal environments might be long running.
paths:
- .gitlab-ci/manifests
only:
- /^qa/
- /^test/
- /^develop/
- /^master/
# Install and test a feature branch manually.
deploy_feature:
stage: deploy
variables:
PROJECT_ENV: feature-${CI_COMMIT_SHORT_SHA}
RELEASE_NAME: ${PROJECT_NAME}-feature-${CI_COMMIT_SHORT_SHA}
environment:
# Cannot use environment variables here, only gitlab variables.
# Also cannot use $PROJECT_ENV since GitLab doesn't support vars in vars.
name: feature-${CI_COMMIT_SHORT_SHA}
url: http://www.feature-${CI_COMMIT_SHORT_SHA}.${PROJECT_NAME}.kube.p2devcloud.com
on_stop: x_destroy_feature
script:
# Call common deploy script. Should not need to modify.
# Skips the octane setup since already done in main branch cache.
- .gitlab-ci/bin/build.sh -skip
# Call common deploy script. Should not need to modify.
- .gitlab-ci/bin/deploy.sh
# Fresh install for feature branches being tested.
- ./.gitlab-ci/bin/exec.sh cli /var/www/bin/install -y
- ./.gitlab-ci/bin/exec.sh cli /var/www/bin/test
artifacts:
# Save the manifests from the deployment so it can be deleted later.
name: ${PROJECT_NAME}-feature-${CI_COMMIT_SHORT_SHA}-manifests
# Expire these since feature-branch environments are not long-lived.
expire_in: 1 week
paths:
- .gitlab-ci/manifests
cache:
# Pull the cache from the main branch.
# Speeds up feature branch build so not starting from scratch.
key: ${PROJECT_NAME}-${MAIN_BRANCH}
# Prevent cache from being updated/pushed.
policy: pull
paths:
- docroot
- build
- vendor
- project/themes/*/node_modules
only:
# Set the branch for the environment here.
- /^feature/
allow_failure: true
when: manual
# 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
# script:
# # Call common deploy script. Should not need to modify.
# - .gitlab-ci/bin/deploy.sh
# 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/bin/exec.sh cli drush status
- ./.gitlab-ci/bin/exec.sh cli /var/www/bin/import -y
only:
- /^qa/
- /^test/
- /^develop/
- /^master/
# Run full test suite that needs the running environment.
test:
stage: test
environment:
name: ${CI_COMMIT_REF_SLUG}
script:
- ./.gitlab-ci/bin/exec.sh cli /var/www/bin/test
only:
- /^qa/
- /^test/
- /^develop/
- /^master/
# Manual task to reinstall Drupal when needed.
install:
stage: tasks
environment:
name: ${CI_COMMIT_REF_SLUG}
script:
- ./.gitlab-ci/bin/exec.sh cli /var/www/bin/install -y
only:
- /^qa/
- /^test/
- /^develop/
- /^master/
when: manual
# Purge the Kubernetes environment manually when needed.
# Set up as the stop action for deploy job, so stage needs to be deploy.
x_destroy_env:
stage: cleanup
variables:
# Tells Gitlab not to bother doing a git-pull for this job.
GIT_STRATEGY: none
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
action: stop
script:
# Remove everything that was deployed for this environment.
- kubectl delete --recursive --filename .gitlab-ci/manifests
dependencies:
# Bring in the manifest artifact
- deploy
only:
- /^qa/
- /^test/
- /^develop/
- /^master/
allow_failure: true
when: manual
# Purge the feature branch Kubernetes environment manually when needed.
# Set up as the stop action for feature_test job, so stage needs to be deploy.
x_destroy_feature:
stage: cleanup
variables:
# Tells Gitlab not to bother doing a git-pull for this job.
GIT_STRATEGY: none
variables:
PROJECT_ENV: feature-${CI_COMMIT_SHORT_SHA}
RELEASE_NAME: ${PROJECT_NAME}-feature-${CI_COMMIT_SHORT_SHA}
environment:
# Cannot use environment variables here, only gitlab variables.
# Also cannot use $PROJECT_ENV since GitLab doesn't support vars in vars.
name: feature-${CI_COMMIT_SHORT_SHA}
url: http://www.feature-${CI_COMMIT_SHORT_SHA}.${PROJECT_NAME}.kube.p2devcloud.com
action: stop
script:
# Remove everything that was deployed for this environment.
- kubectl delete --recursive --filename .gitlab-ci/manifests
dependencies:
# Bring in the manifest artifact
- deploy_feature
only:
- /^feature/
allow_failure: true
when: manual
# Notify when pipeline succeeds.
success:
stage: notify
script:
# Notify Flowdock that pipeline finished successfully.
- ./.gitlab-ci/bin/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/
- /^develop/
- /^master/
# Notify when main pipeline branches fails.
failure:
stage: notify
script:
# Send failure notification to main Flow with emoji.
- ./.gitlab-ci/bin/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/
- /^develop/
- /^master/
# Notify when feature branches succeeds.
success_branch:
stage: notify
script:
# Send failure notification to flow inbox (no emoji allowed :( ).
- ./.gitlab-ci/bin/flowdock-notify.sh inbox "Deployment PASSED 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/bin/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/