-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
307 lines (290 loc) · 8.85 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
stages:
- build
- scan
- package
- deploy
- manual-deploy
variables:
ARTIFACT_COMPRESSION_LEVEL: "fast"
CACHE_COMPRESSION_LEVEL: "fast"
workflow:
rules:
- if: $CI_COMMIT_REF_NAME == "master"
when: always
- if: $CI_COMMIT_REF_NAME == "staging"
when: always
- if: '$CI_PIPELINE_SOURCE == "web"'
when: always
set-vars:
stage: .pre
image: docker.io/alpine:3.15
before_script:
- >
apk add jq yq --no-cache
--repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
script:
- cat .env >> build.env
- source build.env
- echo CHART_VERSION=$(yq '.version' chart/Chart.yaml) >> build.env
- "echo MONITORING_API_IMAGE=${INTERNAL_REG}/${APP_NAME}:\
${APP_VERSION}-${CI_COMMIT_REF_NAME} >> build.env"
- "echo PROXY_IMAGE=${INTERNAL_REG}/${APP_NAME}/proxy:\
${NGINX_VERSION}-${CI_COMMIT_REF_NAME} >> build.env"
- cat build.env
artifacts:
reports:
dotenv: build.env
.build:
stage: build
image:
name: gcr.io/kaniko-project/executor:v1.8.1-debug
entrypoint: [""]
before_script:
- mkdir -p /kaniko/.docker
- >
echo "{\"auths\":{\"${INTERNAL_REG}\":{\"auth\":\"$(printf
"%s:%s" "${IMAGE_REGISTRY_USER}" "${IMAGE_REGISTRY_PASS}"
| base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
script:
- echo "Going to build image ${IMAGE_NAME}-unverified"
- >
/kaniko/executor
--force
--context "${CONTEXT_DIR}"
--dockerfile "${CONTEXT_DIR}/Dockerfile"
--cache=true
--destination "${IMAGE_NAME}-unverified"
--build-arg APP_MAINTAINER="${APP_MAINTAINER}"
--build-arg DEVOPS_MAINTAINER="${DEVOPS_MAINTAINER}"
--build-arg EXTERNAL_REG="${EXTERNAL_REG}"
--build-arg INTERNAL_REG="${INTERNAL_REG}"
--build-arg PYTHON_IMG_TAG="${PYTHON_IMG_TAG}"
--build-arg APP_VERSION="${APP_VERSION}"
--build-arg NGINX_VERSION="${NGINX_VERSION}"
api-build:
extends:
- .build
needs:
- set-vars
variables:
IMAGE_NAME: $MONITORING_API_IMAGE
CONTEXT_DIR: "${CI_PROJECT_DIR}"
proxy-build:
extends:
- .build
needs:
- set-vars
variables:
IMAGE_NAME: $PROXY_IMAGE
CONTEXT_DIR: "${CI_PROJECT_DIR}/nginx"
.scan:
stage: scan
image:
name: docker.io/aquasec/trivy:0.23.0
entrypoint: [""]
variables:
GIT_STRATEGY: none
before_script:
- >
echo "${IMAGE_REGISTRY_CA_CERT}"
| base64 -d >> /etc/ssl/certs/ca-certificates.crt
script:
- echo "Going to scan image ${IMAGE_NAME}-unverified"
- trivy --version
- time trivy image --clear-cache
- time trivy --cache-dir .trivycache/ image --download-db-only --no-progress
# Create report artifact
- >
time trivy --cache-dir .trivycache/ image --exit-code 0 --ignore-unfixed
--no-progress --format template --template "@/contrib/gitlab.tpl"
--output "$CI_PROJECT_DIR/$SHORT_NAME-imgscan.json"
"${IMAGE_NAME}-unverified"
# Print full report
- >
time trivy --cache-dir .trivycache/ image --exit-code 0 --ignore-unfixed
--no-progress "${IMAGE_NAME}-unverified"
# Fail on critical vulnerabilities
- >
time trivy --cache-dir .trivycache/ image --exit-code 1 --ignore-unfixed
--severity CRITICAL --no-progress "${IMAGE_NAME}-unverified"
cache:
key: trivy-cache
paths:
- .trivycache/
policy: pull-push
artifacts:
when: always
reports:
container_scanning: $SHORT_NAME-imgscan.json
api-scan:
extends:
- .scan
needs:
- set-vars
- api-build
variables:
IMAGE_NAME: $MONITORING_API_IMAGE
SHORT_NAME: "monitoring-api"
proxy-scan:
extends:
- .scan
needs:
- set-vars
- proxy-build
variables:
IMAGE_NAME: $PROXY_IMAGE
SHORT_NAME: "proxy"
.retag:
stage: package
image: docker.io/regclient/regctl:v0.3-alpine
variables:
GIT_STRATEGY: none
before_script:
- export REG_HOST=${INTERNAL_REG%/*}
- >
echo "{\"hosts\":{\"${REG_HOST}\":{\"tls\":\"enabled\",\"regcert\":
\"$(printf "%s" "${IMAGE_REGISTRY_CERT}" | base64 -d
| awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}')\",\"hostname\":
\"${REG_HOST}\",\"user\":\"${IMAGE_REGISTRY_USER}\",\"pass\":
\"${IMAGE_REGISTRY_PASS}\"}}}" >> /home/appuser/.regctl/config.json
script:
- echo "Going to retag image ${IMAGE_NAME}-unverified to ${IMAGE_NAME}"
- >
regctl --verbosity debug image copy
"${IMAGE_NAME}-unverified" "${IMAGE_NAME}"
- regctl --verbosity debug tag delete "${IMAGE_NAME}-unverified"
api-retag:
extends:
- .retag
needs:
- set-vars
- api-scan
variables:
IMAGE_NAME: $MONITORING_API_IMAGE
proxy-retag:
extends:
- .retag
needs:
- set-vars
- proxy-scan
variables:
IMAGE_NAME: $PROXY_IMAGE
remote-docker:
stage: deploy
environment: $CI_COMMIT_REF_NAME
image: docker.io/docker:24.0
needs:
- set-vars
- api-retag
- proxy-retag
only:
- branches
variables:
DOCKER_HOST: "ssh://$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME"
before_script:
- apk update
- apk add openssh
- echo "$RUNTIME_ENV" > monitoring-api/project/.env
- echo "Adding SSH Key"
- mkdir -p ~/.ssh
- echo "$DEPLOY_SSH_KEY" > ~/.ssh/gitlab
- chmod 400 ~/.ssh/gitlab
- echo 'IdentityFile ~/.ssh/gitlab' > ~/.ssh/config
- echo "going to keyscan on HOST ${DEPLOY_HOSTNAME}"
- ssh-keyscan -v -H $DEPLOY_HOSTNAME >> ~/.ssh/known_hosts
script:
- USER_DIR=$(echo "/home/$DEPLOY_SSH_USER")
- echo "Defined USER_DIR ${USER_DIR}"
- HOME=$USER_DIR
- echo "Going to remotely start the docker-compose.${CI_COMMIT_REF_NAME}.yml on ${DEPLOY_SSH_USER}@${DEPLOY_HOSTNAME}"
# use docker compose create to recreate the container and force pulling the latest image
# to avoid using the local cache
- |
docker compose --project-name="$APP_NAME" \
--file "docker-compose.$CI_COMMIT_REF_NAME.yml" \
create --force-recreate --pull="always"
# use docker compose up to start the service on the remote machine
- |
docker compose --project-name="$APP_NAME" \
--file "docker-compose.$CI_COMMIT_REF_NAME.yml" \
up --detach
manual-remote-docker:
stage: manual-deploy
environment: $CI_COMMIT_REF_NAME
image: docker.io/docker:24.0
needs:
- set-vars
when: manual
variables:
DOCKER_HOST: "ssh://$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME"
before_script:
- apk update
- apk add openssh
- echo "$RUNTIME_ENV" > monitoring-api/project/.env
- echo "Adding SSH Key"
- mkdir -p ~/.ssh
- echo "$DEPLOY_SSH_KEY" > ~/.ssh/gitlab
- chmod 400 ~/.ssh/gitlab
- echo 'IdentityFile ~/.ssh/gitlab' > ~/.ssh/config
- echo "going to keyscan on HOST ${DEPLOY_HOSTNAME}"
- ssh-keyscan -v -H $DEPLOY_HOSTNAME >> ~/.ssh/known_hosts
script:
- USER_DIR=$(echo "/home/$DEPLOY_SSH_USER")
- echo "Defined USER_DIR ${USER_DIR}"
- HOME=$USER_DIR
- echo "Going to remotely start the docker-compose.${CI_COMMIT_REF_NAME}.yml on ${DEPLOY_SSH_USER}@${DEPLOY_HOSTNAME}"
# use docker compose create to recreate the container and force pulling the latest image
# to avoid using the local cache
- |
docker compose --project-name="$APP_NAME" \
--file "docker-compose.$CI_COMMIT_REF_NAME.yml" \
create --force-recreate --pull="always"
# use docker compose up to start the service on the remote machine
- |
docker compose --project-name="$APP_NAME" \
--file "docker-compose.$CI_COMMIT_REF_NAME.yml" \
up --detach
# .helm:
# stage: deploy
# image:
# name: docker.io/alpine/helm:3.8.0
# entrypoint: [""]
# before_script:
# - >
# echo "${IMAGE_REGISTRY_CA_CERT}"
# | base64 -d >> /etc/ssl/certs/ca-certificates.crt
# - mkdir -p /root/.config/helm/registry
# - >
# echo "{\"auths\":{\"${INTERNAL_REG}\":{\"auth\":\"$(printf "%s:%s"
# "${IMAGE_REGISTRY_USER}" "${IMAGE_REGISTRY_PASS}" | base64
# | tr -d '\n')\"}}}" > /root/.config/helm/registry/config.json
# create-helm-chart:
# extends:
# - .helm
# stage: package
# rules:
# - if: $CI_COMMIT_REF_NAME == "master"
# when: always
# script:
# - >
# apk add yq --no-cache
# --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
# - helm package chart
# - CHART_VER=$(yq '.version' chart/Chart.yaml)
# - helm push "monitoring-api-${CHART_VER}.tgz" oci://registry.envidat.ch/envidat
# helm-deploy:
# extends:
# - .helm
# rules:
# - if: $CI_COMMIT_REF_NAME == "master"
# when: always
# variables:
# GIT_STRATEGY: none
# script:
# - >
# helm upgrade --install "${APP_NAME}"
# oci://registry.envidat.ch/envidat/monitoring-api
# --namespace envidat --create-namespace
# --version "${CHART_VERSION}"
# --set image.tag="${APP_VERSION}-${CI_COMMIT_REF_NAME}"
# --set proxy.image.tag="${NGINX_VERSION}-${CI_COMMIT_REF_NAME}"