-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
352 lines (335 loc) · 11.1 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
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
include:
- local: '.gitlab-ci-scan.yml'
- local: '.gitlab-ci-package.yml'
stages:
- build
- scan
- package
- deploy
- release
- manual-deploy
variables:
ARTIFACT_COMPRESSION_LEVEL: "fast"
CACHE_COMPRESSION_LEVEL: "fast"
.triggers:
rules:
- if: '$CI_PIPELINE_SOURCE == "pipeline"'
when: never
- if: $CI_COMMIT_REF_NAME == "main"
when: always
- if: $CI_COMMIT_REF_NAME == "staging"
when: always
- if: $CI_COMMIT_REF_NAME == "dev"
when: always
- if: '$CI_PIPELINE_SOURCE == "web"'
when: always
set-vars:
stage: .pre
image: docker.io/alpine:3.15
# before_script:
# - >
# apk add yq --no-cache
# --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
script:
- cat .env >> build.env
- source build.env
- "echo APP_NAME=ckan-${CI_COMMIT_REF_NAME} >> build.env"
# - echo "NAMESPACE=ckan" >> build.env
# - echo CHART_VERSION=$(yq '.version' chart/Chart.yaml) >> build.env
- "echo CKAN_IMAGE=${INTERNAL_REG}/ckan-container/ckan:\
${CKAN_VERSION}-${CI_COMMIT_REF_NAME} >> build.env"
- "echo PROXY_IMAGE=${INTERNAL_REG}/ckan-container/proxy:\
${NGINX_IMG_TAG}-${CI_COMMIT_REF_NAME} >> build.env"
- "echo DB_IMAGE=${INTERNAL_REG}/ckan-container/db:\
${POSTGRES_IMG_TAG}-${CI_COMMIT_REF_NAME} >> build.env"
- "echo SOLR_IMAGE=${INTERNAL_REG}/ckan-container/solr:\
${SOLR_IMG_TAG}-${CI_COMMIT_REF_NAME} >> build.env"
- "echo SOLR_INIT_IMAGE=${INTERNAL_REG}/ckan-container/init-solr:\
${SOLR_IMG_TAG}-${CI_COMMIT_REF_NAME} >> build.env"
- echo "Printing build.env"
- cat build.env
artifacts:
reports:
dotenv: build.env
.build:
stage: build
needs:
- set-vars
image:
name: gcr.io/kaniko-project/executor:v1.9.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 "Build image ${IMAGE_NAME}"
- >
/kaniko/executor
--force
--context "${CONTEXT_DIR}"
--dockerfile "${CONTEXT_DIR}/Dockerfile"
--destination "${IMAGE_NAME}-unverified"
--build-arg MAINTAINER="${MAINTAINER}"
--build-arg EXTERNAL_REG="${EXTERNAL_REG}"
--build-arg INTERNAL_REG="${INTERNAL_REG}"
--build-arg PYTHON_IMG_TAG="${PYTHON_IMG_TAG}"
--build-arg CKAN_VERSION="${CKAN_VERSION}"
--build-arg POSTGRES_IMG_TAG="${POSTGRES_IMG_TAG}"
--build-arg SOLR_IMG_TAG="${SOLR_IMG_TAG}"
--build-arg NGINX_IMG_TAG="${NGINX_IMG_TAG}"
ckan-build:
extends:
- .triggers
- .build
variables:
IMAGE_NAME: ${CKAN_IMAGE}
CONTEXT_DIR: "${CI_PROJECT_DIR}"
proxy-build:
extends:
- .triggers
- .build
cache:
key: nginx-config
paths:
- nginx/conf.d/ckan.conf.template
policy: pull
variables:
IMAGE_NAME: ${PROXY_IMAGE}
CONTEXT_DIR: "${CI_PROJECT_DIR}/nginx"
db-build:
extends:
- .triggers
- .build
variables:
IMAGE_NAME: ${DB_IMAGE}
CONTEXT_DIR: "${CI_PROJECT_DIR}/postgresql"
solr-build:
extends:
- .triggers
- .build
variables:
IMAGE_NAME: ${SOLR_IMAGE}
CONTEXT_DIR: "${CI_PROJECT_DIR}/solr"
solr-init-build:
extends:
- .triggers
- .build
variables:
IMAGE_NAME: ${SOLR_INIT_IMAGE}
CONTEXT_DIR: "${CI_PROJECT_DIR}/init_solr"
remote-docker:
stage: deploy
environment: $CI_COMMIT_REF_NAME
image: docker.io/docker:24.0
extends:
- .triggers
needs:
- set-vars
- ckan-retag
- proxy-retag
- db-retag
- solr-retag
- solr-init-retag
variables:
DOCKER_HOST: "ssh://$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME"
before_script:
- echo "***The app image is ${IMAGE_NAME}"
- echo "***The app version is ${CKAN_VERSION}"
- echo "Going to docker remote ($DEPLOY_HOSTNAME) with file docker-compose.$CI_COMMIT_REF_NAME.yml"
- echo "$CKAN_INI" > ckan.ini
- echo "$DB_ENV" > .db.env
- echo "$SOLR_ENV" > .solr.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 -H $DEPLOY_HOSTNAME >> ~/.ssh/known_hosts
# - cat ~/.ssh/known_hosts
script:
- |
USER_DIR=$(echo "/home/$DEPLOY_SSH_USER")
ssh "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME" "mkdir -p $USER_DIR/config/ckan"
scp ckan.ini "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME:$USER_DIR/config/ckan/$CI_COMMIT_REF_NAME.ini"
- HOME=$USER_DIR
# 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
# Job for manual deploying of a docker-compose file, to use it's
# docker images, make sure before you run it that the images referenced
# in the docker-compose.$CI_COMMIT_REF_NAME.yml exist!
manual-remote-docker:
stage: manual-deploy
environment: $CI_COMMIT_REF_NAME
image: docker.io/docker:24.0
needs:
- set-vars
rules:
- if: '$CI_PIPELINE_SOURCE == "pipeline"'
when: always
- when: manual
variables:
DOCKER_HOST: "ssh://$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME"
before_script:
- echo "$CKAN_INI" > ckan.ini
- echo "$DB_ENV" > .db.env
- echo "$SOLR_ENV" > .solr.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 -H $DEPLOY_HOSTNAME >> ~/.ssh/known_hosts
# - cat ~/.ssh/known_hosts
script:
- USER_DIR=$(echo "/home/$DEPLOY_SSH_USER")
- echo "Defined USER_DIR ${USER_DIR}"
- echo "Going to create the config/ckan directory for the ckan.ini file"
- ssh "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME" "mkdir -p $USER_DIR/config/ckan"
- ls -la
- echo "Going to copy the local ckan.ini file to ${USER_DIR}/config/ckan/2.9-backup.ini"
- scp ckan.ini "$DEPLOY_SSH_USER@$DEPLOY_HOSTNAME:$USER_DIR/config/ckan/2.9-backup.ini"
- HOME=$USER_DIR
- echo "Going to remotely start the docker-compose.2.9.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.2.9.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.2.9.yml" \
up --detach
build-binary:
stage: release
image: docker.io/debian:bookworm
rules:
- if: $CI_COMMIT_TAG
before_script:
- apt update && apt install -y curl
- curl -LO https://github.com/ajdiaz/bashc/releases/download/5.2.15/bashc
- chmod +x bashc
script:
- ./bashc scripts/envidat-in-a-box.sh envidat
after_script:
- echo "BIN_JOB_ID=$CI_JOB_ID" >> job.env
artifacts:
paths:
- envidat
expire_in: never
reports:
dotenv: job.env
release-binary:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
needs:
- job: build-binary
artifacts: true
rules:
- if: $CI_COMMIT_TAG
script:
- echo "running release_job for $CI_COMMIT_TAG"
- echo "Using artifact from job $BIN_JOB_ID"
release:
name: "Release $CI_COMMIT_TAG"
tag_name: "$CI_COMMIT_TAG"
description: "$CI_COMMIT_TAG"
ref: "$CI_COMMIT_SHA"
assets:
links:
- name: envidat-in-a-box
url: $CI_SERVER_URL/$CI_PROJECT_PATH/-/jobs/$BIN_JOB_ID/artifacts/raw/envidat
# .helm:
# stage: deploy
# image:
# name: docker.io/alpine/helm:3.8.0
# entrypoint: [""]
# before_script:
# - 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 != "main"
# when: never
# - when: always
# script:
# - >
# apk add yq --no-cache
# --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
# - helm dependencies build chart
# - yq -i '.appVersion = strenv(CKAN_VERSION)' chart/Chart.yaml
# - helm package chart
# - CHART_VER=$(yq '.version' chart/Chart.yaml)
# - helm push "ckan-${CHART_VER}.tgz" oci://registry-gitlab.wsl.ch/envidat/ckan-container
# helm-deploy:
# extends:
# - .helm
# rules:
# - if: $CI_COMMIT_REF_NAME == "main"
# when: never
# - when: on_success
# variables:
# GIT_STRATEGY: none
# script:
# - >
# helm upgrade --install "${APP_NAME}"
# oci://registry-gitlab.wsl.ch/envidat/ckan-container/ckan
# --namespace "${NAMESPACE}" --create-namespace
# --version "${CHART_VERSION}"
# --set image.tag="${CKAN_VERSION}-${CI_COMMIT_REF_NAME}"
# --set proxy.image.tag="${NGINX_IMG_TAG}-${CI_COMMIT_REF_NAME}"
# --set image.pullPolicy="Always"
# --set configIniSecret="${APP_NAME}-config-ini"
# --set authCookieName="${APP_NAME//-/_}"
# --set extraEnvFrom[0].secretRef.name="solr-${APP_NAME}-vars"
# --set ingress.hosts[0].host="${APP_NAME}.envidat.ch"
# --set ingress.hosts[0].paths[0].path="/"
# --set ingress.hosts[0].paths[0].pathType="ImplementationSpecific"
# --set ingress.tls[0].secretName="envidat-star"
# --set ingress.tls[0].hosts[0]="${APP_NAME}.envidat.ch"
# --set db.auth.existingSecret="db-${APP_NAME}-creds"
# --set db.primary.resources.requests.cpu="100m"
# --set db.primary.resources.requests.memory="100Mi"
# --set db.syncData.restoreDbSecret="db-${APP_NAME}-vars"
# --set solr.image.tag="${SOLR_IMG_TAG}-${CI_COMMIT_REF_NAME}"
# --set solr.ingress.hostname="solr-${CI_COMMIT_REF_NAME}.envidat.ch"
# --set solr.passwordInitCredsSecret="solr-${APP_NAME}-vars"
# --set autoscaling.enabled=false
# helm-deploy-prod:
# extends:
# - .helm
# rules:
# - if: $CI_COMMIT_REF_NAME != "main"
# when: never
# - when: on_success
# variables:
# GIT_STRATEGY: none
# script:
# - >
# helm upgrade --install "${APP_NAME}"
# oci://registry-gitlab.wsl.ch/envidat/ckan-container/ckan
# --namespace "${NAMESPACE}" --create-namespace
# --version "${CHART_VERSION}"
# --set image.tag="${CKAN_VERSION}-${CI_COMMIT_REF_NAME}"
# --set proxy.image.tag="${NGINX_IMG_TAG}-${CI_COMMIT_REF_NAME}"