-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.pipeline.yml
400 lines (372 loc) · 12.2 KB
/
.pipeline.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
include:
- project: "devops/gitlab/ci-templates/deploy"
file: ".hashicorp_vault.yml"
- project: "devops/gitlab/ci-templates/ruby"
ref: "sans-dind"
file:
- ".rspec.yml"
- ".rubocop.yml"
- project: "devops/gitlab/ci-templates/docker"
ref: "1.0.0"
file:
- ".amend_manifests.yml"
- ".build_and_push_docker_images.yml"
- ".copy_docker_images.yml"
- ".remove_tmp_registry.yml"
- project: "devops/gitlab/ci-templates/general"
file:
- ".install_hashicorp_vault.yml"
- ".vault_jwt_auth.yml"
- project: "devops/gitlab/ci-templates/sast"
ref: "master"
file:
- ".trivy_container_scanning.yml"
workflow:
rules:
- if: "$CI_MERGE_REQUEST_IID"
- if: "$CI_COMMIT_TAG"
- if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"
stages:
- build_and_push:tmp_base_image
- build_and_push:tmp_dev_image
- amend_tmp_base_dev_images
- test
- build_and_push:tmp_prod_image
- amend_prod_image
- sast
- copy_docker_images
- remove_tmp_registry
- deploy
# -- Start - build and push base image
build_and_push_base_app_image:
stage: build_and_push:tmp_base_image
extends:
- .build_and_push_docker_images
parallel:
matrix:
- RUNNER: build-arm
DOCKER_BUILD_PLATFORM: "linux/arm64"
- RUNNER: build
DOCKER_BUILD_PLATFORM: "linux/amd64"
before_script:
- !reference [.install_hashicorp_vault, before_script]
- !reference [.vault_jwt_auth, before_script]
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- export SIDEKIQ_PRO_CREDENTIALS="$(vault kv get -field=credentials ${VAULT_SIDEKIQ_ENDPOINT})"
variables:
DOCKER_BUILD_CONTEXT: "rails_app/"
DOCKER_BUILD_SECRETS: "--secret id=sidekiq_pro_credentials,env=SIDEKIQ_PRO_CREDENTIALS"
DOCKER_TARGET: "base"
DOCKERFILE_PATH: "rails_app/"
JWT_ROLE: "sidekiq_pro_ro"
REGISTRY_REPOSITORY: "${CI_PIPELINE_ID}"
tags:
- ${RUNNER}
# -- End - build and push base image
# -- Start - build, push temp dev image
build_and_push_dev_app_image:
stage: build_and_push:tmp_dev_image
extends:
- .build_and_push_docker_images
parallel:
matrix:
- RUNNER: build-arm
DOCKER_BUILD_PLATFORM: "linux/arm64"
- RUNNER: build
DOCKER_BUILD_PLATFORM: "linux/amd64"
before_script:
- !reference [.install_hashicorp_vault, before_script]
- !reference [.vault_jwt_auth, before_script]
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- export SIDEKIQ_PRO_CREDENTIALS="$(vault kv get -field=credentials ${VAULT_SIDEKIQ_ENDPOINT})"
variables:
DOCKER_BUILD_ARGS: "--build-arg RAILS_ENV=development"
DOCKER_BUILD_CONTEXT: "rails_app/"
DOCKER_BUILD_SECRETS: "--secret id=sidekiq_pro_credentials,env=SIDEKIQ_PRO_CREDENTIALS"
DOCKER_TARGET: "development"
DOCKERFILE_PATH: "rails_app/"
JWT_ROLE: "sidekiq_pro_ro"
REGISTRY_REPOSITORY: "${CI_PIPELINE_ID}"
needs:
- build_and_push_base_app_image
tags:
- ${RUNNER}
amend_tmp_base_dev_images:
stage: amend_tmp_base_dev_images
extends:
- .amend_manifests
needs:
- build_and_push_base_app_image
- build_and_push_dev_app_image
variables:
REGISTRY_REPOSITORY: "${CI_PIPELINE_ID}"
tags:
- build
# -- End - build, push temp dev image
# -- Start - test dev image
rspec_app_test:
stage: test
image: ${CI_REGISTRY_IMAGE}/${CI_PIPELINE_ID}:${CI_COMMIT_SHORT_SHA}-development
services:
- name: browserless/chrome:latest
alias: chrome
variables:
CONNECTION_TIMEOUT: 600000
- name: postgres:14
variables:
POSTGRES_DB: "catalog_indexing_test"
POSTGRES_PASSWORD: "rspec_test"
POSTGRES_USER: "rspec_test"
- name: browserless/chrome:latest
alias: redis
- name: bitnami/solr:9.3.0
alias: solr
entrypoint: [""]
command: [
"/bin/bash",
"-c",
"./opt/bitnami/scripts/solr/entrypoint.sh /opt/bitnami/scripts/solr/run.sh & \n
for ((i = 0; i <= 9; i += 1)); do \n
curl --silent 'http://solr:8983/api/' && break; \n
echo 'Waiting for solr api to be ready'; \n
sleep '10s'; \n
done \n
.${ZK_SCRIPT_LOCATION}/zkcli.sh -zkhost 127.0.0.1:9983 -cmd upconfig -confname ${SOLR_TEST_CONFIGNAME} -confdir ${CI_PROJECT_DIR}/rails_app/solr/conf && \n
./opt/bitnami/solr/bin/solr create_collection -c ${SOLR_TEST_COLLECTION} && \n
.${ZK_SCRIPT_LOCATION}/zkcli.sh -zkhost 127.0.0.1:9983 -cmd linkconfig -collection ${SOLR_TEST_COLLECTION} -confname ${SOLR_TEST_CONFIGNAME} \n
wait"
]
variables:
SOLR_CLOUD_BOOTSTRAP: "yes"
SOLR_ENABLE_AUTHENTICATION: "yes"
SOLR_HOST: "solr"
SOLR_JAVA_MEM: "-Xms4g -Xmx4g"
SOLR_MODE: "solrcloud"
SOLR_OPTS: "-Dsolr.disableConfigSetsCreateAuthChecks=true"
SOLR_TEST_COLLECTION: "catalog-indexing-test"
SOLR_TEST_CONFIGNAME: "catalog-indexing"
ZK_CREATE_CHROOT: "true"
ZK_SCRIPT_LOCATION: "/opt/bitnami/solr/server/scripts/cloud-scripts"
extends:
- .rspec
variables:
APP_URL: "indexing.test.com"
# CI_DEBUG_SERVICES: "true"
CHROME_URL: "http://chrome:3000"
DATABASE_NAME: "indexing_test"
DATABASE_PASSWORD: "rspec_test"
DATABASE_USER: "rspec_test"
FF_NETWORK_PER_BUILD: "true"
RAILS_ENV: "test"
RAILS_SYSTEM_TESTING_SCREENSHOT_HTML: "1"
REDIS_URL: "redis:6379"
SOLR_URL: "http://solr:8983"
script:
- >
for ((i = 0; i <= 9; i += 1)); do
curl --silent "http://solr:8983/api/" "-H" "'Content-type:application/json'" && break
echo "Waiting for solr api to be ready"
sleep "10s"
done
- cd ${CI_PROJECT_DIR}/rails_app/
- bundle add rspec_junit_formatter
- bundle exec rake db:migrate
- bundle exec rspec --format progress --format RspecJunitFormatter --out rspec.xml
needs:
- amend_tmp_base_dev_images
coverage: '/\(\d+.\d+\%\) covered/'
artifacts:
paths:
- rails_app/tmp/coverage/index.html
- rails_app/tmp/capybara
- rails_app/rspec.xml
expire_in: 1 week
reports:
junit: rails_app/rspec.xml
rules:
- if: $DISABLE_RSPEC_TEST == "true"
when: never
- exists:
- rails_app/spec/**.rb
- rails_app/spec/**/**.rb
tags:
- build
rubocop_app_test:
stage: test
image: ${CI_REGISTRY_IMAGE}/${CI_PIPELINE_ID}:${CI_COMMIT_SHORT_SHA}-development
needs:
- amend_tmp_base_dev_images
extends:
- .rubocop
before_script:
- cd ${CI_PROJECT_DIR}/rails_app/
rules:
- if: $DISABLE_RUBOCOP == "true"
when: never
- exists:
- rails_app/.rubocop.yml
tags:
- build
# -- End - test dev image
# -- Start - build and push temp prod image
build_and_push_prod_app_image:
stage: build_and_push:tmp_prod_image
extends:
- .build_and_push_docker_images
parallel:
matrix:
- RUNNER: build-arm
DOCKER_BUILD_PLATFORM: "linux/arm64"
- RUNNER: build
DOCKER_BUILD_PLATFORM: "linux/amd64"
before_script:
- !reference [.install_hashicorp_vault, before_script]
- !reference [.vault_jwt_auth, before_script]
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
- export SIDEKIQ_PRO_CREDENTIALS="$(vault kv get -field=credentials ${VAULT_SIDEKIQ_ENDPOINT})"
variables:
DOCKER_BUILD_ARGS: "--build-arg RAILS_ENV=production"
DOCKER_BUILD_CONTEXT: "rails_app/"
DOCKER_BUILD_SECRETS: "--secret id=sidekiq_pro_credentials,env=SIDEKIQ_PRO_CREDENTIALS"
DOCKER_TARGET: "production"
DOCKERFILE_PATH: "rails_app/"
JWT_ROLE: "sidekiq_pro_ro"
REGISTRY_REPOSITORY: "${CI_PIPELINE_ID}"
needs:
- rspec_app_test
- rubocop_app_test
tags:
- ${RUNNER}
amend_prod_image:
stage: amend_prod_image
extends:
- .amend_manifests
needs:
- build_and_push_prod_app_image
variables:
DOCKER_TARGET: "production"
REGISTRY_REPOSITORY: "${CI_PIPELINE_ID}"
TAG_WITH_DOCKER_TARGET: "false"
tags:
- build
# -- End - build and push temp prod image
# -- Start - SAST images
trivy_app_container_scanning:
stage: sast
extends:
- .trivy_container_scanning
before_script:
- apk add --no-cache docker
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
after_script:
- docker logout ${CI_REGISTRY}
needs:
- amend_prod_image
variables:
CI_IMAGE_NAME: ${CI_REGISTRY_IMAGE}/${CI_PIPELINE_ID}:${CI_COMMIT_SHORT_SHA}
allow_failure: true
tags:
- build
# -- End - SAST images
# -- Start - Copy app docker images
copy_app_docker_images:
stage: copy_docker_images
variables:
ORIGINAL_REGISTRY_REPOSITORY: "${CI_PIPELINE_ID}"
needs:
- trivy_app_container_scanning
extends:
- .copy_docker_images
tags:
- build
# -- End - Copy app docker images
# -- Start - remove tmp registry repository
remove_tmp_registry:
stage: remove_tmp_registry
extends:
- .remove_tmp_registry
rules:
- when: always
needs:
- copy_app_docker_images
allow_failure: true
tags:
- build
# -- End - remove tmp registry repository
# Deploy staging
deploy_staging:
stage: deploy
extends:
- .deploy
variables:
ANSIBLE_HOST_KEY_CHECKING: "false"
IMAGE_TAG: ${CI_COMMIT_SHORT_SHA}
environment:
name: staging
url: https://indexing-staging.library.upenn.edu
needs:
- copy_app_docker_images
before_script:
- ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -P ""
# Get Vault token via JWT auth locked to this project
- export VAULT_ADDR=${VAULT_URL}
- export VAULT_TOKEN="$(vault write -field=token auth/jwt/${CI_SERVER_HOST}/login role=${JWT_ROLE} jwt=${CI_JOB_JWT})"
# Generate signed key and write to a new pub key
- vault write -field=signed_key ${VAULT_SSH_CLIENT_ENDPOINT}/sign/${VAULT_SSH_CLIENT_ROLE} public_key=@${HOME}/.ssh/id_ed25519.pub valid_principals=${VALID_PRINCIPALS} > ~/.ssh/id_ed25519-cert.pub
- chmod 0400 ~/.ssh/id_ed25519-cert.pub
# Set the remote user for ansible
- export ANSIBLE_REMOTE_USER=${DEPLOY_USER}
# Set the private key for ansible
- export ANSIBLE_PRIVATE_KEY_FILE=~/.ssh/id_ed25519
# Set the vault id
- export ANSIBLE_VAULT_ID_MATCH=${DEPLOY_ENVIRONMENT}
script:
# Ensure dynamic inventory file is executable
- chmod +x ansible/inventories/${DEPLOY_ENVIRONMENT}/inventory.py
# Install ansible requirements
- ansible-galaxy install -g -f -r ansible/roles/requirements.yml
# Run ansible playbook
- ansible-playbook -e "image_tag=${IMAGE_TAG}" -i ansible/inventories/${DEPLOY_ENVIRONMENT}/inventory.py ansible/site.yml
only:
- main
tags:
- deploy
#Deploy production
deploy_production:
stage: deploy
extends:
- .deploy
variables:
ANSIBLE_HOST_KEY_CHECKING: "false"
IMAGE_TAG: ${CI_COMMIT_SHORT_SHA}
environment:
name: production
url: https://indexing.library.upenn.edu
needs:
- copy_app_docker_images
before_script:
- ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -P ""
# Get Vault token via JWT auth locked to this project
- export VAULT_ADDR=${VAULT_URL}
- export VAULT_TOKEN="$(vault write -field=token auth/jwt/${CI_SERVER_HOST}/login role=${JWT_ROLE} jwt=${CI_JOB_JWT})"
# Generate signed key and write to a new pub key
- vault write -field=signed_key ${VAULT_SSH_CLIENT_ENDPOINT}/sign/${VAULT_SSH_CLIENT_ROLE} public_key=@${HOME}/.ssh/id_ed25519.pub valid_principals=${VALID_PRINCIPALS} > ~/.ssh/id_ed25519-cert.pub
- chmod 0400 ~/.ssh/id_ed25519-cert.pub
# Set the remote user for ansible
- export ANSIBLE_REMOTE_USER=${DEPLOY_USER}
# Set the private key for ansible
- export ANSIBLE_PRIVATE_KEY_FILE=~/.ssh/id_ed25519
# Set the vault id
- export ANSIBLE_VAULT_ID_MATCH=${DEPLOY_ENVIRONMENT}
script:
# Ensure dynamic inventory file is executable
- chmod +x ansible/inventories/${DEPLOY_ENVIRONMENT}/inventory.py
# Install ansible requirements
- ansible-galaxy install -g -f -r ansible/roles/requirements.yml
# Run ansible playbook
- ansible-playbook -e "image_tag=${IMAGE_TAG}" -i ansible/inventories/${DEPLOY_ENVIRONMENT}/inventory.py ansible/site.yml
rules:
- if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+(\.[a-zA-Z0-9]+)?$/
tags:
- deploy
#-- End - deployment