forked from jmfee-usgs/geomag-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
277 lines (252 loc) · 6.56 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
image: ${DEVOPS_REGISTRY}usgs/python:3.9-obspy
stages:
- init
- test
- integration
- deploy
variables:
APP_NAME: geomag-algorithms
DATA_HOST: "edgecwb.usgs.gov"
DATA_PORT: "2060"
DATA_TYPE: "edge"
UPSTREAM_PATH: ghsc/geomag/geomag-algorithms
# Do not run in upstream for merge requests
workflow:
rules:
- if: $CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH
## --------------------------------------------------
# Templates
## --------------------------------------------------
.adjust_image_names:
before_script:
- IMAGE_NAME=usgs/${APP_NAME}:${CI_COMMIT_REF_SLUG}
- IMAGE_NAME=${IMAGE_NAME/:master/:latest}
- INTERNAL_IMAGE_NAME=${CODE_REGISTRY_IMAGE}/${IMAGE_NAME}
- STACK_NAME=${APP_NAME}
.deploy:
extends:
- .adjust_image_names
image: "${CODE_REGISTRY}/ghsc/hazdev/container-deploy:latest"
script:
- cp -v
/container-deploy/default.config.sh
/container-deploy/default.funcs.sh
/container-deploy/deploy.sh
scripts/.
- export APP_NAME=${APP_NAME}
- export IMAGE_NAME=${IMAGE_NAME}
- export REGISTRY=${CODE_REGISTRY_IMAGE}
- export STACK_NAME=${APP_NAME}
- ./scripts/deploy.sh
stage: deploy
tags:
- deploy
- swarm
# these variables are defined in Gitlab, or set in scripts/custom.config.sh
# variables:
# DATABASE_URL
# OPENID_CLIENT_ID
# OPENID_CLIENT_SECRET
# OPENID_METADATA_URL
# SECRET_KEY
# SECRET_SALT
# ADMIN_GROUP
# REVIEWER_GROUP
.deploy-library:
image: "docker:stable-git"
script:
- PREFIX_LENGTH=${#REQUIRED_PREFIX};
- if [[ "${APP_DEPLOY_DIR:0:${PREFIX_LENGTH}}" != "${REQUIRED_PREFIX}" ]]; then
echo "APP_DEPLOY_DIR does not contain correct path";
exit 255;
fi
- if [ ! -d "${APP_DEPLOY_DIR}" ]; then
cd "$(dirname "${APP_DEPLOY_DIR}")";
git clone "${CI_REPOSITORY_URL}" "$(basename "${APP_DEPLOY_DIR}")";
fi
- cd "${APP_DEPLOY_DIR}";
# fetch tags so checkout doesn't create a branch
- git fetch --tags;
# switch to requested tag/branch
- git checkout "${CI_COMMIT_REF_NAME}" || git checkout -b "${CI_COMMIT_REF_NAME}";
# pull updates if on a branch
- |
if ! git status | grep -q detached ; then
git pull --ff-only "${CI_REPOSITORY_URL}" "${CI_COMMIT_REF_NAME}";
fi
stage: deploy
tags:
- deploy
- swarm
variables:
APP_DEPLOY_DIR: "/geomag/geomag-algorithms"
REQUIRED_PREFIX: "/geomag"
# rules to define which branches should trigger actions
.development-env: &development-env
if: $CI_PROJECT_PATH != $UPSTREAM_PATH
variables:
ENVIRONMENT: development
.staging-env: &staging-env
if: >
$CI_PROJECT_PATH == $UPSTREAM_PATH
&& $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
ENVIRONMENT: staging
.production-env: &production-env
if: >
$CI_PROJECT_PATH == $UPSTREAM_PATH
&& ( $CI_COMMIT_BRANCH == 'production' || $CI_COMMIT_TAG)
variables:
ENVIRONMENT: production
## --------------------------------------------------
# Init Stage
## --------------------------------------------------
Poetry:
artifacts:
paths:
- poetry.toml
- .venv
cache:
paths:
# cache pip installed dependencies, see PIP_CACHE_DIR variable below
- .cache/pip
script:
# install into .venv for artifact
- poetry config virtualenvs.in-project true --local
- poetry install
- poetry run safety check
stage: init
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
## --------------------------------------------------
# Test Stage
## --------------------------------------------------
Python Build:
needs:
- Poetry
script:
- poetry build
stage: test
Python Lint:
needs:
- Poetry
script:
- poetry run black --check .
stage: test
Python Test:
artifacts:
reports:
cobertura: coverage.xml
junit: junit.xml
needs:
- Poetry
script:
- poetry run pytest --cov=geomagio --junitxml junit.xml
- poetry run coverage xml
stage: test
## --------------------------------------------------
# Integration Stage
## --------------------------------------------------
Build Docker Image:
extends:
- .adjust_image_names
image: ${DEVOPS_REGISTRY}docker:19.03-git
needs:
- Python Build
script:
- LOCAL_IMAGE="local/${IMAGE_NAME}"
## build image
- docker build
--pull
--build-arg FROM_IMAGE=${FROM_IMAGE}
--build-arg GIT_BRANCH_NAME=${CI_COMMIT_REF_NAME}
--build-arg GIT_COMMIT_SHA=${CI_COMMIT_SHA}
--tag "${LOCAL_IMAGE}"
"."
## trivy scan before push
- wget https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz
- tar zxvf trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz
# fail LOW,MEDIUM vulnerabilities that have a fix available
- ./trivy image --exit-code 1 --ignore-unfixed --severity LOW,MEDIUM "${LOCAL_IMAGE}";
# fail HIGH,CRITICAL vulnerabilities
- ./trivy image --exit-code 1 --severity HIGH,CRITICAL "${LOCAL_IMAGE}";
## push image
- if [ "${CI_PROJECT_PATH}" != "${UPSTREAM_PATH}" ]; then
echo "Skipping push on non-upstream (${CI_PROJECT_PATH})";
exit 0;
fi
- |
for IMAGE in "${IMAGE_NAME}" "${INTERNAL_IMAGE_NAME}"; do
docker tag "${LOCAL_IMAGE}" "${IMAGE}";
docker push "${IMAGE}";
done
services:
- alias: docker
name: ${DEVOPS_REGISTRY}docker:19.03-dind
stage: integration
tags:
- build
variables:
APP_NAME: geomag-algorithms
DOCKER_DRIVER: overlay2
FROM_IMAGE: ${DEVOPS_REGISTRY}usgs/python:3.9-obspy
TRIVY_VERSION: "0.27.1"
## --------------------------------------------------
# Deploy Stage
## --------------------------------------------------
Staging Library:
extends:
- .deploy-library
rules:
- <<: *staging-env
when: on_success
- <<: *production-env
when: manual
tags:
- deploy
- swarm
- mage01
Production Library:
extends:
- .deploy-library
rules:
- <<: *production-env
when: manual
tags:
- deploy
- swarm
- mage02
Web Service Staging:
extends:
- .deploy
parallel:
matrix:
- RUNNER:
- mage01
- staging01
rules:
- <<: *staging-env
when: on_success
- <<: *production-env
when: manual
tags:
- deploy
- swarm
- "${RUNNER}"
Web Service Production:
extends:
- .deploy
parallel:
matrix:
- RUNNER:
- mage02
- production01
- production02
rules:
- <<: *production-env
when: manual
tags:
- deploy
- swarm
- "${RUNNER}"