-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
314 lines (295 loc) · 9.28 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
variables:
DOCKER_FILE: $CI_PROJECT_DIR/docker/Dockerfile
DOCKER_BASE_IMAGE: $CI_REGISTRY_IMAGE/base:$CI_COMMIT_REF_SLUG
DOCKER_PRODUCTION: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
DOCKER_REVIEW: $CI_REGISTRY_IMAGE/review:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA
API_URL_REVIEW: https://api.askanna.eu
stages:
- build base
- build
- test
- deploy
- release
- publish
- cleanup
include:
- template: Code-Quality.gitlab-ci.yml
# To use Kaniko extend the job and set the variable for DOCKER_DESTINATION and optionally DOCKER_FILE
.use-kaniko:
tags:
- kubernetes
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: ['']
before_script:
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
script:
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $DOCKER_FILE --destination $DOCKER_DESTINATION
build_base:
stage: build base
extends: .use-kaniko
script:
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $DOCKER_FILE --target=base-image --destination $DOCKER_BASE_IMAGE
rules:
- if: $CI_COMMIT_BRANCH
changes:
- docker/Dockerfile
- package.json
- package-lock.json
- if: $CI_COMMIT_BRANCH
when: manual
allow_failure: true
build_production:
stage: build
extends: .use-kaniko
variables:
KUBERNETES_MEMORY_REQUEST: 6Gi
rules:
- if: $CI_COMMIT_BRANCH == "main"
variables:
ENV_CONTENT: $ENV_STAGING
DOCKER_DESTINATION: $DOCKER_PRODUCTION
- if: $CI_COMMIT_BRANCH == "beta"
variables:
ENV_CONTENT: $ENV_BETA
DOCKER_DESTINATION: $DOCKER_PRODUCTION
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
ENV_CONTENT: $ENV_BETA
DOCKER_DESTINATION: $DOCKER_PRODUCTION
DOCKER_BASE_IMAGE: $CI_REGISTRY_IMAGE/base:main
- if: $CI_COMMIT_BRANCH
variables:
ENV_CONTENT: |-
VITE_APP_URL=https://$DOMAIN_REVIEW
VITE_APP_API_URL=$API_URL_REVIEW
VITE_APP_ALLOW_ROBOTS=off
VITE_APP_SENTRY_URL=$SENTRY_URL_REVIEW
VITE_APP_SENTRY_ENV=review
VITE_APP_SENTRY_TRACES_RATE= 1
DOCKER_DESTINATION: $DOCKER_REVIEW
script:
- echo "$ENV_CONTENT" >> .env.production.local
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $DOCKER_FILE --destination $DOCKER_DESTINATION --build-arg BASE_IMAGE=$DOCKER_BASE_IMAGE
code_quality:
tags:
- docker
artifacts:
paths: [gl-code-quality-report.json]
rules:
- if: $CODE_QUALITY_DISABLED
when: never
- if: ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG)
when: never
- if: $CI_COMMIT_BRANCH
test_package_lock:
stage: test
image: node:lts-alpine
needs: []
script:
- npm ci
deploy_production:
stage: deploy
image: curlimages/curl
needs:
- job: build_production
- job: code_quality
optional: true
- job: test_package_lock
variables:
GIT_STRATEGY: none
ASKANNA_DEPLOY_BRANCH: main
ASKANNA_DEPLOY_PRODUCT: askanna-frontend
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
variables:
ENV_HOST: $DOMAIN_STAGING
ENV_NAME: master
DOCKER_IMAGE: $DOCKER_PRODUCTION
- if: ($CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/)
when: manual
variables:
ENV_HOST: $DOMAIN_BETA
ENV_NAME: beta
DOCKER_IMAGE: $DOCKER_PRODUCTION
tags:
- kubernetes
environment:
name: $ENV_NAME
url: https://$ENV_HOST
script:
- curl -X POST -F token=$CI_JOB_TOKEN -F ref=$ASKANNA_DEPLOY_BRANCH -F variables[product]=$ASKANNA_DEPLOY_PRODUCT -F variables[action]=deploy -F variables[env_name]=$ENV_NAME -F variables[env_host]=$ENV_HOST -F variables[frontend_image]=$DOCKER_IMAGE $ASKANNA_DEPLOY_TRIGGER
deploy_review:
stage: deploy
image: gitlab.askanna.io:4567/askanna/services/helm
needs:
- job: build_production
- job: test_package_lock
rules:
- if: ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG)
when: never
- if: $CI_COMMIT_BRANCH
environment:
name: review/$CI_COMMIT_REF_NAME
url: https://$CI_PROJECT_NAME-$CI_ENVIRONMENT_SLUG.$DOMAIN_REVIEW
on_stop: stop_review
auto_stop_in: 2 weeks
tags:
- kubernetes
script:
- init_k8s
- helm upgrade
--install
--namespace gitlab-managed-apps
--set app.image="$DOCKER_REVIEW"
--set app.host="$CI_PROJECT_NAME-$CI_ENVIRONMENT_SLUG.$DOMAIN_REVIEW"
--set deployment.minReplicas=1
--wait
$CI_PROJECT_NAME-$CI_ENVIRONMENT_SLUG
./k8s-chart
stop_review:
stage: cleanup
image: gitlab.askanna.io:4567/askanna/services/helm
needs:
- job: deploy_review
rules:
- if: ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "beta" || $CI_COMMIT_TAG)
when: never
- if: $CI_COMMIT_BRANCH
when: manual
variables:
GIT_STRATEGY: none
environment:
name: review/$CI_COMMIT_REF_NAME
action: stop
allow_failure: true
tags:
- kubernetes
script:
- init_k8s
- helm uninstall $CI_PROJECT_NAME-$CI_ENVIRONMENT_SLUG
.functions: &functions |
# Functions
function init_k8s() {
gcloud auth activate-service-account --project $GCP_PROJECT --key-file $GCP_SERVICE_ACCOUNT_FILE_AA_SERVICES
gcloud container clusters get-credentials $GKE_CLUSTER_NAME --zone $GKE_CLUSTER_ZONE --project $GCP_PROJECT
}
before_script:
- *functions
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
script:
- echo "Creating a new release..."
release:
tag_name: $CI_COMMIT_TAG
description: $CI_COMMIT_TAG_MESSAGE
publish_to_gitlab:
stage: publish
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
optional: true
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: manual
variables:
TARGET: HEAD:main
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
TARGET: refs/tags/$CI_COMMIT_TAG
before_script:
- apt-get update -y && apt-get install openssh-client git curl -y
- eval $(ssh-agent -s)
- echo "${SSH_PRIVATE_KEY_GITLAB_COM}" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- git config --global user.email "[email protected]"
- git config --global user.name "AskAnna Robot"
script:
- git remote add gitlab [email protected]:askanna/askanna-frontend.git
- git push gitlab $TARGET
publish_release_to_gitlab:
stage: publish
image: registry.gitlab.com/gitlab-org/release-cli:latest
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
- job: publish_to_gitlab
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
GITLAB_PRIVATE_TOKEN: $GITLAB_COM_API_TOKEN
script:
- release-cli --server-url https://gitlab.com --project-id 43372103 create --tag-name $CI_COMMIT_TAG --description "$CI_COMMIT_TAG_MESSAGE"
publish_to_github:
stage: publish
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
optional: true
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
when: manual
variables:
TARGET: HEAD:main
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
TARGET: refs/tags/$CI_COMMIT_TAG
before_script:
- apt-get update -y && apt-get install openssh-client git curl -y
- eval $(ssh-agent -s)
- echo "${SSH_PRIVATE_KEY_GITHUB_COM}" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan github.com >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- git config --global user.email "[email protected]"
- git config --global user.name "AskAnna Robot"
script:
- git remote add github [email protected]:askanna-io/askanna-frontend.git
- git push github $TARGET
publish_release_to_github:
image: ubuntu:22.04
stage: publish
needs:
- job: build_production
- job: deploy_production
optional: true
- job: release
- job: publish_to_github
tags:
- kubernetes
allow_failure: true
rules:
- if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+$/'
variables:
GH_TOKEN: $GITHUB_COM_API_TOKEN
before_script:
- apt-get update -y && apt-get install curl -y
- curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
- chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null
- apt-get update -y && apt-get install gh -y
script:
- gh release create $CI_COMMIT_TAG --verify-tag --title $CI_COMMIT_TAG --notes "$CI_COMMIT_TAG_MESSAGE" --repo askanna-io/askanna-frontend