-
Notifications
You must be signed in to change notification settings - Fork 46
/
.gitlab-ci.yml
294 lines (263 loc) · 6.39 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
image: ${DEVOPS_REGISTRY}usgs/centos:latest
cache:
paths:
# cache pip installed dependencies, see PIP_CACHE_DIR variable below
- .cache/pip
stages:
- test
- integration
- scan
- deploy
variables:
CI_REGISTRY: ${CODE_REGISTRY}
CI_REGISTRY_IMAGE: ${CODE_REGISTRY_IMAGE}
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
TRIVY_VERSION: "0.11.0"
# docker variables
DOCKER_DRIVER: overlay2
FROM_IMAGE: ${CODE_REGISTRY}/devops/images/usgs/centos:7
# environment variables
APP_NAME: geomag-algorithms
DATA_HOST: "cwbpub.cr.usgs.gov"
DATA_PORT: "2060"
DATA_TYPE: "edge"
## --------------------------------------------------
# Templates
## --------------------------------------------------
.adjust_image_names:
before_script:
- IMAGE_NAME=usgs/${APP_NAME}:${CI_COMMIT_REF_SLUG}
- IMAGE_NAME=${IMAGE_NAME/:master/:latest}
- INTERNAL_IMAGE_NAME=${CI_REGISTRY_IMAGE}/${IMAGE_NAME}
- STACK_NAME=${APP_NAME}
.build_docker_image:
cache: {}
extends:
- .adjust_image_names
image: docker:19.03-git
only:
- master@ghsc/geomag/geomag-algorithms
- production@ghsc/geomag/geomag-algorithms
- tags@ghsc/geomag/geomag-algorithms
script:
- 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_NAME} .
- docker tag local/${IMAGE_NAME} ${IMAGE_NAME}
- docker push ${IMAGE_NAME}
- docker image rm ${IMAGE_NAME}
- docker tag local/${IMAGE_NAME} ${INTERNAL_IMAGE_NAME}
- docker push ${INTERNAL_IMAGE_NAME}
- docker image rm ${INTERNAL_IMAGE_NAME}
services:
- docker:19.03-dind
stage: integration
tags:
- build
.check_code:
image: ${DEVOPS_REGISTRY}usgs/conda:latest
script:
- export PYTHON_VERSION=${PYTHON_VERSION:-3.8}
- scripts/ci_check_code.sh
artifacts:
paths:
- cov.xml
reports:
junit: cov.xml
stage: test
tags:
- development
.deploy:
cache: {}
extends:
- .adjust_image_names
image: ${CODE_REGISTRY}/ghsc/hazdev/cloud-formation/hazdev-build-runner:latest
script:
- git clone ${GENERIC_SWARM_DEPLOY_REPO} generic-deploy
- cp -v
generic-deploy/default.config.sh
generic-deploy/default.funcs.sh
generic-deploy/deploy.sh
scripts/.
- export APP_NAME=${APP_NAME}
- export IMAGE_NAME=${IMAGE_NAME}
- export REGISTRY=${CI_REGISTRY_IMAGE}
- export STACK_NAME=${APP_NAME}
- ./scripts/deploy.sh
stage: deploy
tags:
- deploy
- swarm
.deploy-library:
cache: {}
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}";
- git checkout "${CI_COMMIT_REF_NAME}" || git checkout -b "${CI_COMMIT_REF_NAME}";
- git pull --ff-only "${CI_REPOSITORY_URL}" "${CI_COMMIT_REF_NAME}";
stage: deploy
tags:
- deploy
- swarm
variables:
APP_DEPLOY_DIR: '/geomag/geomag-algorithms'
REQUIRED_PREFIX: '/geomag'
.staging:
only:
- master@ghsc/geomag/geomag-algorithms
- tags@ghsc/geomag/geomag-algorithms
.production:
except:
- ^.*beta.*$
- ^.*-rc.*$
only:
- tags@ghsc/geomag/geomag-algorithms
- production@ghsc/geomag/geomag-algorithms
when: manual
## --------------------------------------------------
# Test Stage
## --------------------------------------------------
Check Python 3.6:
extends:
- .check_code
script:
- export PYTHON_VERSION=3.6
- scripts/ci_check_code.sh
Check Python 3.7:
extends:
- .check_code
script:
- export PYTHON_VERSION=3.7
- scripts/ci_check_code.sh
Check Python 3.8:
extends:
- .check_code
script:
- export PYTHON_VERSION=3.8
- scripts/ci_check_code.sh
## --------------------------------------------------
# Integration Stage
## --------------------------------------------------
Build Docker Image:
extends:
- .build_docker_image
variables:
APP_NAME: geomag-algorithms
## --------------------------------------------------
# Scanning Stage (e.g. OWASP ZAP etc...)
## --------------------------------------------------
Scan Docker Image:
cache: {}
extends:
- .adjust_image_names
image: docker:19.03-git
only:
- master@ghsc/geomag/geomag-algorithms
- production@ghsc/geomag/geomag-algorithms
- tags@ghsc/geomag/geomag-algorithms
script:
# install trivy
- 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
# run trivy
- ./trivy image --no-progress --exit-code 1 --severity HIGH,CRITICAL ${INTERNAL_IMAGE_NAME}
services:
- docker:19.03-dind
stage: scan
tags:
- build
## --------------------------------------------------
# Deploy Stage
## --------------------------------------------------
Mage01 Library:
extends:
- .deploy-library
- .staging
tags:
- deploy
- swarm
- mage01
variables:
APP_NAME: geomag-algorithms
Mage01 Web Service:
extends:
- .deploy
- .staging
tags:
- deploy
- swarm
- mage01
variables:
APP_NAME: geomag-algorithms
Mage02 Library:
extends:
- .deploy-library
- .production
tags:
- deploy
- swarm
- mage02
variables:
APP_NAME: geomag-algorithms
Mage02 Web Service:
extends:
- .deploy
- .production
tags:
- deploy
- swarm
- mage02
variables:
APP_NAME: geomag-algorithms
Production01 Web Service:
extends:
- .deploy
- .production
tags:
- deploy
- swarm
- production01
variables:
APP_NAME: geomag-algorithms
Production02 Web Service:
extends:
- .deploy
- .production
tags:
- deploy
- swarm
- production02
variables:
APP_NAME: geomag-algorithms
Staging01 Web Service:
extends:
- .deploy
- .staging
tags:
- deploy
- swarm
- staging01
variables:
APP_NAME: geomag-algorithms
Staging02 Web Service:
extends:
- .deploy
- .staging
tags:
- deploy
- swarm
- staging02
variables:
APP_NAME: geomag-algorithms