-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy path.gitlab-ci.yml
575 lines (519 loc) · 17.4 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
variables:
GET_SOURCES_ATTEMPTS: 3
DEPLOY_DOMAIN_SUFFIX: -ce.review.mist.io
TEST_DEPLOY_DOMAIN_SUFFIX: -ce.test.mist.io
# Possible values are CI_REGISTRY, EXT_REGISTRY, DOCKERHUB
COMPOSE_REGISTRY: DOCKERHUB
DEPLOY_REGISTRY: EXT_REGISTRY
# Set to empty string '' to disable.
DEPLOY_REGISTRY_AUTH: '1'
# API & UI tests image
TESTS_IMAGE: $DOCKERHUB_IMAGE/tests:$CI_COMMIT_SHA
SUBMODULES_IMAGE: mist
stages:
- discover
- build
- release
- deploy
- test
- undeploy
#################### DISCOVER STAGE ####################
discover submodules:
stage: discover
image: alpine:3.8
before_script:
- apk add --no-cache git
script:
- |
git submodule status --recursive | \
sed 's/^[ +-]\([0-9a-f]*\) \([^ ]*\).*$/\1,\2/' > submodules.csv
artifacts:
paths:
- submodules.csv
expire_in: 10 days
dependencies: []
#################### BUILD STAGE ####################
.dind_template: &docker_template
stage: build
image: docker:git
services:
- docker:dind
variables: &docker_variables
DOCKER_DRIVER: overlay2
DOCKER_HOST: "tcp://localhost:2375"
retry: 2
tags:
- cpu
before_script:
# Configure registries.
- |
export REGISTRIES=""
# Login to gitlab docker registry.
if [ -n "$CI_REGISTRY_IMAGE" ]; then
echo "Logging in to gitlab registry."
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" \
$CI_REGISTRY
export REGISTRIES="$REGISTRIES $CI_REGISTRY_IMAGE"
echo "Logged in to gitlab registry."
else
echo "No credentials provided for gitlab registry, skipping."
fi
# Login to other registry.
if [ -n "$EXT_REGISTRY" ] && \
[ -n "$EXT_REGISTRY_IMAGE" ] && \
[ -n "$EXT_REGISTRY_USER" ] && \
[ -n "$EXT_REGISTRY_PASSWORD" ]
then
echo "Logging in to $EXT_REGISTRY."
docker login -u "$EXT_REGISTRY_USER" -p "$EXT_REGISTRY_PASSWORD" \
"$EXT_REGISTRY"
export REGISTRIES="$REGISTRIES $EXT_REGISTRY_IMAGE"
echo "Logged in to $EXT_REGISTRY."
else
echo "No credentials provided for external registry, skipping."
fi
# Login to dockerhub.
if [ -n "$DOCKERHUB_IMAGE" ] && \
[ -n "$DOCKERHUB_USER" ] && \
[ -n "$DOCKERHUB_PASSWORD" ]
then
echo "Logging in to dockerhub."
docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_PASSWORD"
export REGISTRIES="$REGISTRIES $DOCKERHUB_IMAGE"
echo "Logged in to dockerhub."
else
echo "No credentials provided for dockerhub, skipping."
fi
# Check if any registry configured.
if [ -z "$REGISTRIES" ]; then
echo "No registries configured, exiting."
exit 1
fi
- |
function docker_pull() {
# docker_pull <img> : Will retry several times to pull image.
img=$1
tries=60
sleep=10
echo "Will try to pull image $img ($tries tries, ${sleep}s sleep)."
for i in $(seq 1 $tries); do
if docker pull $img; then
echo "Image $img pulled successfully!"
return 0
fi
sleep $sleep
done
echo "Failed too pull image $img."
return 1
}
- |
function docker_push() {
# docker_push <src> <dest>: Retag and push image
# <src> must be the full name of a local image.
# <dest> must be just the image identifier, registry and tag will be
# added automatically.
tags="$CI_COMMIT_SHA $CI_COMMIT_REF_SLUG"
if [ "$CI_COMMIT_REF_NAME" = "master" ]; then
tags="$tags latest"
fi
src=$1
for registry in $REGISTRIES; do
for tag in $tags; do
dest="$registry/$2:$tag"
echo "Tagging image $src as $dest..."
docker tag $src $dest
echo "Pushing image $dest..."
docker push $dest
done
done
}
- |
function get_submodule_sha() {
cat submodules.csv | grep ",$1$" | cut -d, -f1
}
- |
function docker_retag_submodule() {
repo=$1
img=$2
src_img="$img:$(get_submodule_sha $repo)"
docker_pull $src_img
docker_push $src_img $repo
}
dependencies:
- discover submodules
build api:
<<: *docker_template
script:
- SRC_IMG="${SUBMODULES_IMAGE:-mist}/mist:$(get_submodule_sha api)"
- docker_pull $SRC_IMG
- |
docker build \
-t $CI_PROJECT_NAME/api:$CI_COMMIT_SHA \
--build-arg FROM_IMAGE=$SRC_IMG \
--build-arg MIST_VERSION_SHA=$CI_COMMIT_SHA \
--build-arg MIST_VERSION_NAME=$CI_COMMIT_REF_NAME \
.
- docker_push $CI_PROJECT_NAME/api:$CI_COMMIT_SHA api
.docker_build_template: &docker_build_template
<<: *docker_template
script:
- export img=$(echo "$CI_JOB_NAME" | cut -d " " -f2)
- docker build -t $CI_PROJECT_NAME/$img:$CI_COMMIT_SHA docker/$img
- docker_push $CI_PROJECT_NAME/$img:$CI_COMMIT_SHA $img
dependencies: []
build nginx: *docker_build_template
build gocky: *docker_build_template
build logstash: *docker_build_template
build elasticsearch-manage: *docker_build_template
.docker_retag_template: &docker_retag_template
<<: *docker_template
variables:
<<: *docker_variables
GIT_STRATEGY: none
script:
- export arg1=$(echo "$CI_JOB_NAME" | cut -d " " -f2)
- export arg2=$(echo "$CI_JOB_NAME" | cut -d " " -f3)
- export img="${SUBMODULES_IMAGE:-mist}/${arg1}"
- export submodule="${arg2:-$arg1}"
- echo "Will retag image $img from submodule $submodule."
- docker_retag_submodule $submodule $img
retag landing: *docker_retag_template
retag ui: *docker_retag_template
retag tests: *docker_retag_template
compose:
stage: build
image: python:alpine
before_script:
- pip install docker-compose
- apk add --no-cache gettext
script:
- mkdir -p artifacts
- export IMG_REGISTRY=$(eval echo \$${COMPOSE_REGISTRY}_IMAGE)
- echo "IMG_REGISTRY=$IMG_REGISTRY"
- export IMG_TAG=$CI_COMMIT_REF_SLUG
- echo "IMG_TAG=$IMG_TAG"
- export CONF_DIR=/etc/mist
- envsubst < docker-compose.yml > artifacts/docker-compose.yml
artifacts:
paths:
- artifacts/docker-compose.yml
dependencies: []
#################### DEPLOY STAGE ####################
.deploy_template: &deploy_template
stage: deploy
image: dtzar/helm-kubectl:2.10.0
before_script:
- |
set -e
function set_if_not_empty() {
key="$1"
val="$2"
if [ -n "$val" ]; then
echo " --set $key=$val"
fi
}
function elasticsearch_params() {
set_if_not_empty elasticsearch.host $ELASTICSEARCH_HOST
set_if_not_empty elasticsearch.port $ELASTICSEARCH_PORT
set_if_not_empty elasticsearch.username $ELASTICSEARCH_USERNAME
set_if_not_empty elasticsearch.password $ELASTICSEARCH_PASSWORD
set_if_not_empty elasticsearch.tls $ELASTICSEARCH_TLS
}
function deploy() {
export namespace="$1"
export release="$2"
touch values.yaml
export host=$(echo $CI_ENVIRONMENT_URL | sed 's/^https\?:\/\/\(.*\)/\1/')
kubectl describe namespace/$namespace || \
kubectl create namespace $namespace
export image_secret_name=
export REGISTRY_IMAGE="$(eval echo \$${DEPLOY_REGISTRY}_IMAGE)"
export REGISTRY_USER="$(eval echo \$${DEPLOY_REGISTRY}_USER)"
export REGISTRY_PASSWORD="$(eval echo \"\$${DEPLOY_REGISTRY}_PASSWORD\")"
if [ "$DEPLOY_REGISTRY" = "CI_REGISTRY" ]; then
if [ "$CI_PROJECT_VISIBILITY" = "public" ]; then
export DEPLOY_REGISTRY_AUTH=
elif [ -n "$CI_REGISTRY_USER_PERMANENT" -a -n "$CI_REGISTRY_PASSWORD_PERMANENT" ]; then
export REGISTRY_USER="$CI_REGISTRY_USER_PERMANENT
export REGISTRY_PASSWORD="$CI_REGISTRY_PASSWORD_PERMANENT
fi
fi
if [ -n "$DEPLOY_REGISTRY_AUTH" ]; then
export image_secret_name=registry
kubectl create secret --namespace $namespace \
docker-registry $image_secret_name \
--docker-server=$REGISTRY_IMAGE \
--docker-username=$REGISTRY_USER \
--docker-password="$REGISTRY_PASSWORD" \
--docker-email=$GITLAB_USER_EMAIL \
-o yaml --dry-run | \
kubectl replace --namespace $namespace --force -f -
fi
export tls_settings=
if [ -n "$TLS_KEY" ] && [ -n "$TLS_CRT" ]; then
tls_tmpdir=$(mktemp -d)
echo "$TLS_KEY" > $tls_tmpdir/tls.key
echo "$TLS_CRT" > $tls_tmpdir/tls.crt
kubectl create secret --namespace $namespace \
tls https \
--key $tls_tmpdir/tls.key \
--cert $tls_tmpdir/tls.crt \
-o yaml --dry-run | \
kubectl replace --namespace $namespace --force -f -
export tls_settings="--set http.tlsSecret=https"
rm -rf $tls_tmpdir
fi
helm init --client-only
helm repo update
helm dependency update ./chart/mist/
helm upgrade --install --namespace $namespace \
--wait --timeout=600 \
-f values.yaml \
--set image.tag="$CI_COMMIT_SHA" \
--set image.registry="$REGISTRY_IMAGE" \
--set image.pullPolicy=IfNotPresent \
--set image.secrets[0].name="$image_secret_name" \
--set http.host="$host" \
--set stripe.publicApiKey="$STRIPE_PUBLIC_APIKEY" \
--set stripe.secretApiKey="$STRIPE_SECRET_APIKEY" \
--set docker.host="$DOCKER_HOST" \
--set docker.port="$DOCKER_PORT" \
--set docker.key="$DOCKER_KEY" \
--set docker.cert="$DOCKER_CERT" \
--set docker.ca="$DOCKER_CA" \
--set awsAccessKey="$AWS_ACCESS_KEY" \
--set awsSecretKey="$AWS_SECRET_KEY" \
$tls_settings \
$(elasticsearch_params) \
$release ./chart/mist/
apk add --no-cache curl
echo "Waiting for environment to be ready..."
while kubectl --namespace $namespace get pods -l release=$release | grep -v -e NAME -e Running -e Completed -e Evicted -e OutOfcpu; do
sleep 10 && echo '.'
done
while !(curl -sSLf "$CI_ENVIRONMENT_URL" >/dev/null); do
sleep 5 && echo 'Environment not ready yet...'
done
}
script:
- echo "$values" > values.yaml
- deploy $namespace $release
dependencies: []
deploy test:
<<: *deploy_template
environment:
name: test/$CI_COMMIT_REF_SLUG
url: https://$CI_COMMIT_REF_SLUG$TEST_DEPLOY_DOMAIN_SUFFIX
on_stop: undeploy test
variables:
namespace: test-$CI_PROJECT_NAME
release: test-$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG
values: |-
http:
tlsSecret: tls-wildcard-test-mist-io
tlsHosts:
- '*.test.mist.io'
tlsAnnotations:
certmanager.k8s.io/cluster-issuer: letsencrypt-prod
certmanager.k8s.io/acme-challenge-type: dns01
certmanager.k8s.io/acme-dns01-provider: mist-dns
enableDevUsers: true
nodeSelector:
cloud.google.com/gke-nodepool: generic
rabbitmq:
nodeSelector:
cloud.google.com/gke-nodepool: generic
.deploy_review_template: &deploy_review_template
<<: *deploy_template
variables:
namespace: $CI_PROJECT_NAME
release: $CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG
values: |
http:
tlsSecret: tls-wildcard-review-mist-io
tlsHosts:
- '*.review.mist.io'
tlsAnnotations:
certmanager.k8s.io/cluster-issuer: letsencrypt-prod
certmanager.k8s.io/acme-challenge-type: dns01
certmanager.k8s.io/acme-dns01-provider: mist-dns
enableDevUsers: false
smtp:
host: smtp.sendgrid.net
port: 2525
starttls: true
username: apikey
password: $SENDGRID_PASSWORD
nodeSelector:
cloud.google.com/gke-nodepool: generic
rabbitmq:
nodeSelector:
cloud.google.com/gke-nodepool: generic
deploy review:
<<: *deploy_review_template
environment:
name: review/$CI_COMMIT_REF_SLUG
url: https://$CI_COMMIT_REF_SLUG$DEPLOY_DOMAIN_SUFFIX
on_stop: undeploy review
except:
- master
when: manual
deploy master:
<<: *deploy_review_template
environment:
name: master
url: https://master$DEPLOY_DOMAIN_SUFFIX
only:
- master
when: manual
#################### TEST STAGE ####################
.test_template: &test_template
stage: test
image: $TESTS_IMAGE
variables:
GIT_STRATEGY: none
MIST_URL: https://$CI_COMMIT_REF_SLUG$TEST_DEPLOY_DOMAIN_SUFFIX
DISPLAY: ":1.0"
REGISTER_USER_BEFORE_FEATURE: "True"
EMAIL: thingirl.tester.mist.io+${CI_JOB_ID}@gmail.com
IMAP_HOST: test-$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG-mailmock.test-$CI_PROJECT_NAME.svc.cluster.local
before_script:
- 'export TEST=$(echo "$CI_JOB_NAME" | cut -d/ -f1 | cut -d: -f2,3 | tr : /)'
- echo "Test argument is $TEST"
- echo "Image used is $TESTS_IMAGE"
- echo $TEST_SETTINGS | base64 -d > test_settings.py
dependencies: []
.api_test_template: &api_test_template
<<: *test_template
script:
- /mist.tests/prepare_env.py --api -s /mist.tests/misttests/integration/api/$TEST.py
API:main:api_token: *api_test_template
API:main:clouds: *api_test_template
API:main:dns: *api_test_template
API:main:keys: *api_test_template
API:main:images: *api_test_template
API:main:libcloud_1/2: *api_test_template
API:main:libcloud_2/2: *api_test_template
API:main:machines: *api_test_template
API:main:networks: *api_test_template
API:main:schedules: *api_test_template
API:main:scripts: *api_test_template
API:main:teams: *api_test_template
API:main:ip_whitelisting: *api_test_template
API:main:volumes: *api_test_template
.API:plugin:orchestration: *api_test_template
.API:plugin:tunnels: *api_test_template
.ui_test_template: &ui_test_template
<<: *test_template
script:
- /mist.tests/prepare_env.py -k --stop --tags=$TEST /mist.tests/misttests/integration/gui/core/pr/features
artifacts:
paths:
- js_console.log
- artifacts/error.png
- artifacts/output.mp4
expire_in: 3 days
when: on_failure
tags:
- chrome
UI:clouds-add-1/2: *ui_test_template
UI:clouds-add-2/2: *ui_test_template
UI:clouds-actions: *ui_test_template
UI:keys: *ui_test_template
UI:scripts: *ui_test_template
UI:images-networks: *ui_test_template
UI:user-actions: *ui_test_template
UI:teams: *ui_test_template
UI:zones: *ui_test_template
UI:rules-1/2: *ui_test_template
UI:rules-2/2: *ui_test_template
UI:machines-1/2:
<<: *ui_test_template
retry: 1
UI:machines-2/2:
<<: *ui_test_template
retry: 1
UI:monitoring:
<<: *ui_test_template
retry: 1
UI:schedulers-1/2:
<<: *ui_test_template
retry: 1
UI:schedulers-2/2:
<<: *ui_test_template
retry: 1
UI:ip-whitelisting:
<<: *ui_test_template
only:
- staging
.UI:rbac-1/4: *ui_test_template
.UI:rbac-2/4: *ui_test_template
.UI:rbac-3/4: *ui_test_template
.UI:rbac-4/4: *ui_test_template
.UI:orchestration: *ui_test_template
.UI:insights:
<<: *ui_test_template
retry: 1
#################### UNDEPLOY STAGE ####################
.undeploy_template: &undeploy_template
stage: undeploy
image: dtzar/helm-kubectl:2.10.0
variables:
GIT_STRATEGY: none
script:
- helm delete --purge $release
allow_failure: true
dependencies: []
undeploy test:
<<: *undeploy_template
environment:
name: test/$CI_COMMIT_REF_SLUG
action: stop
variables:
GIT_STRATEGY: none
release: test-$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG
undeploy review:
<<: *undeploy_template
environment:
name: review/$CI_COMMIT_REF_SLUG
action: stop
variables:
GIT_STRATEGY: none
release: $CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG
when: manual
except:
- master
#################### RELEASE STAGE ####################
release: &release_template
when: manual
stage: release
image: python:2.7-alpine
before_script:
- apk add --update libmagic openssl
- pip install requests python-magic
- wget -O /usr/local/bin/submark https://github.com/dahlia/submark/releases/download/0.2.0/submark-linux-x86_64
- chmod +x /usr/local/bin/submark
script:
- mkdir release
- cp artifacts/docker-compose.yml release/docker-compose.yml
- HEADER=$(sed -n "s/^## \($CI_BUILD_REF_NAME .*\)$/\1/p" CHANGELOG.md)
- submark --omit-heading --h2="$HEADER" --out-file=release.md CHANGELOG.md
- sed -i 's/^### Changes/## Changes/' release.md
- echo >> release.md
- sed "s/TAG/$CI_BUILD_REF_NAME/g;s/REPO/$CI_PROJECT_NAME/g" ci/release.md >> release.md
- ./ci/release.py $RELEASE_ARGS --draft -f release/* -m @release.md mistio $CI_PROJECT_NAME $CI_BUILD_REF_NAME
only:
# only for references that match a version string (v1.2.3)
- /^v\d+\.\d+\.\d+$/
except:
# Except branches, meaning it'll only apply to git tags
- branches
dependencies:
- compose
prerelease:
<<: *release_template
variables:
RELEASE_ARGS: "--prerelease"
only:
# only for references that match a prelease version string (v1.2.3-beta)
- /^v\d+\.\d+\.\d+-.+$/