-
Notifications
You must be signed in to change notification settings - Fork 1
/
.gitlab-ci.yml
executable file
·246 lines (225 loc) · 7.26 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
---
# == Notes ==
# - GitLab automatically passes artifacts from previous stages by default
# - This project requires runners with specific tags to execute specific jobs
# - This project includes variables specific to using 'Docker In Docker'
# - This project uses additional services ran alongside the job images
# - Set required secret variables at: https://gitlab.data.bas.ac.uk/web-apps/arctic-office-projects-api/settings/ci_cd
# = Secret variables
# - Variables are grouped by section in KEY: "value" format (e.g. FOO: "bar")
# Sensitive values are represented by "[Sensitive]"
#
# - AWS IAM id/secret keys for 'bas-gitlab-arctic-office-projects-api' user
# > AWS_ACCESS_KEY_ID: "[Sensetive]"
# > AWS_SECRET_ACCESS_KEY: "[Sensetive]"
#
# - Heroku
# > HEROKU_API_KEY: "[Sensitive]"
#
# - Sentry
# > SENTRY_AUTH_TOKEN: "[Sensetive]"
# > SENTRY_DSN: "[Sensetive]"
#
# - BAS API Documentation
# > API_DOCS_PIPELINE_TOKEN: "[Sensitive]"
# == Global settings ==
stages:
- build-base
- test
- lint
- build
- deploy
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_CERT_PATH: /certs/client
DOCKER_DRIVER: overlay2
DOCKER_TLS_VERIFY: 1
GITLAB_REGISTRY: docker-registry.data.bas.ac.uk
POSTGRES_DB: app_test
POSTGRES_USER: app
POSTGRES_PASSWORD: "password"
APP_NAME: arctic-office-projects-api
HEROKU_APP_STAGE_NAME: bas-arctic-projects-api-stage
HEROKU_APP_PROD_NAME: bas-arctic-projects-api-prod
SENTRY_ORG: antarctica
SENTRY_PROJECT: arctic-office-projects-api
SNYK_ORG: antarctica
SNYK_PROJECT: arctic-office-projects-api
API_DOCS_PROJECT: arctic-office-projects
API_DOCS_STAGE_BUCKET: bas-api-docs-content-testing
API_DOCS_PROD_BUCKET: bas-api-docs-content
build-base:
stage: build-base
tags:
- dnd
services:
- docker:dind
image: docker:latest
script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $GITLAB_REGISTRY
- docker build -t $CI_REGISTRY_IMAGE/arctic-office-projects-api:latest -f Dockerfile-ci .
- docker push $CI_REGISTRY_IMAGE/arctic-office-projects-api:latest
only:
- master
cache:
paths:
- .venv
image:
name: docker-registry.data.bas.ac.uk/web-apps/arctic-office-projects-api/arctic-office-projects-api:latest
entrypoint: [""]
# == Jobs ==
test-app:
stage: test
services:
- name: postgres:12.12-alpine
alias: postgres
variables:
POSTGRES_DB: runner_db
POSTGRES_USER: runner
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
before_script:
- export FLASK_APP=manage.py
- export FLASK_ENV=development
- export SQLALCHEMY_DATABASE_URI=postgresql://runner:password@postgres:5432/runner_db
- apk add --no-cache postgresql-client
- psql -h postgres -U runner -d runner_db -c "ALTER SYSTEM SET listen_addresses TO '*';"
- psql -h postgres -U runner -d runner_db -c "SELECT pg_reload_conf();"
- psql -h postgres -U runner -d runner_db < docker/init.sql
script:
- poetry install --no-interaction --no-ansi
- poetry run flask db upgrade
- poetry run pytest --cov-report=html --cov=arctic_office_projects_api tests
only:
- master
dependencies-app:
stage: test
before_script:
- "apk add --update nodejs npm"
- "npm install snyk -g"
- "poetry install"
script:
- "poetry run snyk test"
- "poetry run snyk monitor --project-name=$SNYK_PROJECT --org=$SNYK_ORG"
only:
- master
pep8-app:
stage: lint
before_script:
- "poetry install"
script:
- "poetry run flake8 . --ignore=E501 --exclude migrations,.venv"
bandit-app:
stage: lint
before_script:
- "poetry install"
script:
- "poetry run bandit -r arctic_office_project_api"
build-heroku-stage:
stage: build
tags:
- dnd
services:
- docker:dind
image:
name: docker:19.03-dind
entrypoint: [""]
script:
- "docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $GITLAB_REGISTRY"
- "docker login -u _ -p $HEROKU_API_KEY registry.heroku.com"
- "docker build -t registry.heroku.com/$HEROKU_APP_STAGE_NAME/web -f Dockerfile.heroku --build-arg APP_RELEASE=$CI_COMMIT_SHA --build-arg APP_ENV=staging ."
- "docker build -t registry.heroku.com/$HEROKU_APP_STAGE_NAME/release -f Dockerfile.heroku-release ."
- "docker push registry.heroku.com/$HEROKU_APP_STAGE_NAME/web"
- "docker push registry.heroku.com/$HEROKU_APP_STAGE_NAME/release"
only:
- master
build-heroku-prod:
stage: build
tags:
- dnd
services:
- docker:dind
image:
name: docker:19.03-dind
entrypoint: [""]
script:
- "docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $GITLAB_REGISTRY"
- "docker login -u _ -p $HEROKU_API_KEY registry.heroku.com"
- "docker build -t registry.heroku.com/$HEROKU_APP_PROD_NAME/web -f Dockerfile.heroku --build-arg APP_RELEASE=$CI_COMMIT_SHA --build-arg APP_ENV=production ."
- "docker build -t registry.heroku.com/$HEROKU_APP_PROD_NAME/release -f Dockerfile.heroku-release ."
- "docker push registry.heroku.com/$HEROKU_APP_PROD_NAME/web"
- "docker push registry.heroku.com/$HEROKU_APP_PROD_NAME/release"
only:
- tags
deploy-heroku-stage:
stage: deploy
image:
name: wingrunr21/alpine-heroku-cli
entrypoint: [""]
script:
- "heroku container:release web release --app $HEROKU_APP_STAGE_NAME"
only:
- master
environment:
name: Staging
url: https://api.bas.ac.uk/arctic-office-projects/testing/
deploy-heroku-prod:
stage: deploy
image:
name: wingrunr21/alpine-heroku-cli
entrypoint: [""]
script:
- "heroku container:release web release --app $HEROKU_APP_PROD_NAME"
only:
- tags
environment:
name: Production
url: https://api.bas.ac.uk/arctic-office-projects/v1/
deploy-docs-stage:
stage: deploy
image:
name: governmentpaas/awscli:latest
entrypoint: [""]
script:
- "aws s3 cp docs/usage.md s3://$API_DOCS_STAGE_BUCKET/services/$API_DOCS_PROJECT/testing/usage/usage.md"
- "aws s3 cp openapi.yml s3://$API_DOCS_STAGE_BUCKET/services/$API_DOCS_PROJECT/testing/openapi.yml"
- "curl -X POST -F token=$API_DOCS_PIPELINE_TOKEN -F ref=master https://gitlab.data.bas.ac.uk/api/v4/projects/530/trigger/pipeline"
only:
- master
environment:
name: Staging - Documentation
url: https://docs-testing.api.bas.ac.uk/arctic-office-projects
deploy-docs-prod:
stage: deploy
image:
name: governmentpaas/awscli:latest
entrypoint: [""]
script:
- "aws s3 cp docs/usage.md s3://$API_DOCS_PROD_BUCKET/services/$API_DOCS_PROJECT/$CI_COMMIT_TAG/usage/usage.md"
- "aws s3 cp openapi.yml s3://$API_DOCS_PROD_BUCKET/services/$API_DOCS_PROJECT/$CI_COMMIT_TAG/openapi.yml"
- "curl -X POST -F token=$API_DOCS_PIPELINE_TOKEN -F ref=master https://gitlab.data.bas.ac.uk/api/v4/projects/530/trigger/pipeline"
only:
- tags
environment:
name: Production - Documentation
url: https://docs.api.bas.ac.uk/arctic-office-projects
sentry-stage:
stage: deploy
image:
name: getsentry/sentry-cli
entrypoint: [""]
script:
- "sentry-cli releases new --finalize $CI_COMMIT_SHA"
- "sentry-cli releases deploys $CI_COMMIT_SHA new -e staging"
only:
- master
sentry-prod:
stage: deploy
image:
name: getsentry/sentry-cli
entrypoint: [""]
script:
- "sentry-cli releases new --finalize $CI_COMMIT_SHA"
- "sentry-cli releases deploys $CI_COMMIT_SHA new -e production"
only:
- tags