-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path.gitlab-ci.yml
268 lines (242 loc) · 8.61 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
variables:
AUTO_DEPLOY_IMAGE_VERSION: v2.50.0
CI_TEMPLATE_REGISTRY_HOST: registry.gitlab.com
DOCKER_TLS_CERTDIR: ""
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
HELM_VERSION: 3.10.2
KUBE_CONTEXT: openchainvote/openchainvote:openchainvote
KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: gitlab-admin
KUBERNETES_VERSION: 1.24.9
stages:
- .pre
- build
- preview
- mainnet
- .post
before_script:
- |
export APP_VERSION=$(awk '/version/{gsub(/("|",)/,"",$2);print $2};' application/package.json)
export APP_VERSION_AND_BUILD="${APP_VERSION}-${CI_PIPELINE_ID}"
export CI_APPLICATION_REPOSITORY=$CI_REGISTRY_IMAGE
export CI_APPLICATION_TAG=$APP_VERSION_AND_BUILD
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- ./node_modules
services:
- name: docker:dind
alias: docker
command: ["--tls=false"]
########## Helper Scripts ############
######################################
.auto-deploy:
image: "${CI_TEMPLATE_REGISTRY_HOST}/gitlab-org/cluster-integration/auto-deploy-image:${AUTO_DEPLOY_IMAGE_VERSION}"
dependencies: []
.setup: &setup
- apk add --no-cache ca-certificates curl bash openssl git
- curl -LO https://dl.k8s.io/release/v1.25.9/bin/linux/amd64/kubectl && chmod +x kubectl && mv kubectl /usr/local/bin/kubectl
- curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 && chmod 700 get_helm.sh && DESIRED_VERSION=v$HELM_VERSION ./get_helm.sh
- kubectl config get-contexts
- kubectl config use-context openchainvote/openchainvote:openchainvote
.app: &app_template
stage: mainnet
image: "${CI_TEMPLATE_REGISTRY_HOST}/gitlab-org/cluster-integration/auto-deploy-image:${AUTO_DEPLOY_IMAGE_VERSION}"
retry: 0
script:
- auto-deploy check_kube_domain
- auto-deploy download_chart
- auto-deploy create_secret
- |
DEPLOYMENT_NAME="www-openchainvote.$CI_ENVIRONMENT_SLUG"
cp $ENV_FILE ./chart/app_env
if [ "${CI_ENVIRONMENT_NAME}" == "mainnet" ]; then
export ANNOTATIONS='traefik-redirect-http-to-https@kubernetescrd\,traefik-redirect-to-www@kubernetescrd\,traefik-default-headers@kubernetescrd'
else
export ANNOTATIONS='traefik-redirect-http-to-https@kubernetescrd\,traefik-default-headers@kubernetescrd'
fi
helm upgrade $DEPLOYMENT_NAME ./chart --install \
-f ./chart/values.yaml \
--timeout 300s \
--wait \
--atomic \
--set image.repository=$CI_APPLICATION_REPOSITORY/www.openchainvote.com \
--set lucid.image.repository=$CI_APPLICATION_REPOSITORY/wallet-backend \
--set image.tag=$CI_APPLICATION_TAG \
--set app.DB_HOST=$DB_HOST \
--set redis.password=$REDIS_PASSWORD \
--set ingress.host="$AUTO_DEVOPS_WEB_HOST" \
--set ingress.hosts[0]="$AUTO_DEVOPS_WEB_DOMAIN" \
--set ingress.hosts[1]="$AUTO_DEVOPS_DOMAIN" \
--set ingress.tls[0].secretName="www-openchainvote-tls" \
--set ingress.tls[0].hosts[0]="$AUTO_DEVOPS_WEB_DOMAIN" \
--set ingress.tls[0].hosts[1]="$AUTO_DEVOPS_DOMAIN" \
--set ingress.annotations."traefik\.ingress\.kubernetes\.io\/router\.middlewares"="$ANNOTATIONS" \
--set namespace=${KUBE_NAMESPACE} \
--namespace="$KUBE_NAMESPACE" \
--create-namespace
- auto-deploy persist_environment_url
environment:
name: mainnet
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
artifacts:
paths: [environment_url.txt]
.db: &db_template
stage: mainnet
image: "${CI_TEMPLATE_REGISTRY_HOST}/gitlab-org/cluster-integration/auto-deploy-image:${AUTO_DEPLOY_IMAGE_VERSION}"
retry: 0
script:
- auto-deploy download_chart
- |
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
echo "install db"
helm upgrade --install \
--timeout 300s \
--wait \
--atomic \
--namespace="$KUBE_NAMESPACE" \
--create-namespace \
-f ./chart/values.db.yaml \
--set global.postgresql.auth.database="$DB_DATABASE" \
--set global.postgresql.auth.username="$DB_USERNAME" \
--set global.postgresql.auth.password="$DB_PASSWORD" \
--set global.postgresql.auth.postgresPassword="$POSTGRES_PASSWORD" \
--set primary.persistence.size="80Gi" \
openchainvote-web-db \
bitnami/postgresql
- auto-deploy persist_environment_url
environment:
name: mainnet
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
artifacts:
paths: [environment_url.txt]
.redis: &redis_template
image: "${CI_TEMPLATE_REGISTRY_HOST}/gitlab-org/cluster-integration/auto-deploy-image:${AUTO_DEPLOY_IMAGE_VERSION}"
retry: 0
script:
- auto-deploy download_chart
- |
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
echo "install redis"
helm upgrade --install \
--timeout 300s \
--wait \
--atomic \
--namespace="$KUBE_NAMESPACE" \
--create-namespace \
-f ./chart/values.redis.yaml \
--set global.redis.password="$REDIS_PASSWORD" \
--set auth.password="$REDIS_PASSWORD" \
--set master.persistence.enabled="true" \
redis \
bitnami/redis
- auto-deploy persist_environment_url
artifacts:
paths: [environment_url.txt]
############ Build Jobs ##############
######################################
build_web:
stage: build
image: docker:24.0.2-git
artifacts:
paths:
- application/.env
untracked: true
when: on_success
expire_in: "3 days"
retry: 0
script:
- *setup
- |
cp $ENV_FILE application/.env
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
echo "Building Dockerfile with tags $CI_APPLICATION_REPOSITORY/www.openchainvote.com:$CI_APPLICATION_TAG and $CI_APPLICATION_REPOSITORY/www.openchainvote.com:latest ..."
docker build --network host -f application/Dockerfile \
-t "$CI_APPLICATION_REPOSITORY/www.openchainvote.com:$CI_APPLICATION_TAG" \
-t "$CI_APPLICATION_REPOSITORY/www.openchainvote.com:latest" .
docker push "$CI_APPLICATION_REPOSITORY/www.openchainvote.com:$CI_APPLICATION_TAG"
docker push "$CI_APPLICATION_REPOSITORY/www.openchainvote.com:latest"
only:
- branches
build_lucid:
stage: build
image: docker:24.0.2-git
retry: 2
script:
- *setup
- |
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
echo "Building Dockerfile with tags $CI_APPLICATION_REPOSITORY/wallet-backend:$CI_APPLICATION_TAG and $CI_APPLICATION_REPOSITORY/wallet-backend:latest ..."
docker build --network host -f lucid/Dockerfile \
-t "$CI_APPLICATION_REPOSITORY/wallet-backend:$CI_APPLICATION_TAG" \
-t "$CI_APPLICATION_REPOSITORY/wallet-backend:latest" ./lucid
docker push "$CI_APPLICATION_REPOSITORY/wallet-backend:$CI_APPLICATION_TAG"
docker push "$CI_APPLICATION_REPOSITORY/wallet-backend:latest"
only:
- branches
######## Preview Deployments #########
######################################
db_preview:
<<: *db_template
stage: preview
environment:
name: preview
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == 'main'
when: manual
redis_preview:
<<: *redis_template
stage: preview
environment:
name: preview
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == 'main'
when: manual
app_preview:
<<: *app_template
stage: preview
environment:
name: preview
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == 'main'
when: manual
######## Mainnet Deployments #########
######################################
db_mainnet:
<<: *db_template
stage: mainnet
environment:
name: mainnet
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
needs:
- job: db_preview
rules:
- if: $CI_COMMIT_BRANCH == 'main'
when: manual
redis_mainnet:
<<: *redis_template
stage: mainnet
environment:
name: mainnet
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
needs:
- job: redis_preview
rules:
- if: $CI_COMMIT_BRANCH == 'main'
when: manual
app_mainnet:
<<: *app_template
stage: mainnet
environment:
name: mainnet
url: https://$CI_PROJECT_PATH_SLUG.$AUTO_DEVOPS_DOMAIN
needs:
- job: app_preview
rules:
- if: $CI_COMMIT_BRANCH == 'main'
when: manual