forked from netless-io/flat-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
102 lines (87 loc) · 3.3 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
stages:
- build_image
- deploy
.dev: &DEV
environment:
name: development
only:
- main
- merge_requests
.prod: &PROD
environment:
name: production
only:
- stable
- merge_requests
.docker_build: &DOCKER_BUILD
stage: build_image
image: docker:19.03.12
services:
- docker:19.03.12-dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker pull $DOCKER_BUILDER_TAG || true
- docker build --target builder --cache-from $DOCKER_BUILDER_TAG -t $DOCKER_BUILDER_TAG -f Dockerfile .
- docker push $DOCKER_BUILDER_TAG
- docker pull $DOCKER_BUILDER_DEPENDENCIES_TAG || true
- docker build --target prod-dependencies --cache-from $DOCKER_BUILDER_TAG --cache-from $DOCKER_BUILDER_DEPENDENCIES_TAG -t $DOCKER_BUILDER_DEPENDENCIES_TAG -f Dockerfile .
- docker push $DOCKER_BUILDER_DEPENDENCIES_TAG
- docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
PROD_LATEST_IMAGE_TAG: $DOCKERHUB_USERNAME/flat-server:latest
PROD_SHA_IMAGE_TAG: $DOCKERHUB_USERNAME/flat-server:$CI_COMMIT_SHA
DEV_LATEST_IMAGE_TAG: $DOCKERHUB_USERNAME/flat-server:dev-latest
DEV_SHA_IMAGE_TAG: $DOCKERHUB_USERNAME/flat-server:dev-$CI_COMMIT_SHA
DOCKER_BUILDER_TAG: $CI_REGISTRY_IMAGE/flat-server:$CI_ENVIRONMENT_SLUG-cache-builder
DOCKER_BUILDER_DEPENDENCIES_TAG: $CI_REGISTRY_IMAGE/flat-server:$CI_ENVIRONMENT_SLUG-cache-builder-dependencies
docker_build_dev:
<<: *DOCKER_BUILD
script:
- echo $DEV_LATEST_IMAGE_TAG
- echo $DEV_SHA_IMAGE_TAG
- docker pull $DEV_LATEST_IMAGE_TAG || true
- docker build --cache-from $DOCKER_BUILDER_TAG --cache-from $DOCKER_BUILDER_DEPENDENCIES_TAG --cache-from $DEV_LATEST_IMAGE_TAG -t $DEV_SHA_IMAGE_TAG -t $DEV_LATEST_IMAGE_TAG -f Dockerfile .
after_script:
- docker push $DEV_LATEST_IMAGE_TAG
- docker push $DEV_SHA_IMAGE_TAG
<<: *DEV
docker_build_prod:
<<: *DOCKER_BUILD
script:
- docker pull $PROD_LATEST_IMAGE_TAG || true
- docker build --cache-from $DOCKER_BUILDER_TAG --cache-from $DOCKER_BUILDER_DEPENDENCIES_TAG --cache-from $PROD_LATEST_IMAGE_TAG -t $PROD_SHA_IMAGE_TAG -t $PROD_LATEST_IMAGE_TAG -f Dockerfile .
after_script:
- docker push $PROD_LATEST_IMAGE_TAG
- docker push $PROD_SHA_IMAGE_TAG
<<: *PROD
.deploy: &DEPLOY
stage: deploy
image: dtzar/helm-kubectl
before_script:
- mkdir ~/.kube
- echo "$KUBE_CONFIG" > ~/.kube/config
- export IMAGE_REPO="$DOCKERHUB_USERNAME/flat-server"
- export VALUES=`envsubst < ./helm/values.yaml`
- echo "$VALUES" > ./helm/values.yaml
script:
# link: https://github.com/stakater/Reloader
- |
export DEPLOYED_RELOADER=$(helm ls --namespace $KUBE_NAMESPACE | grep "stakater-reloader" | wc -l)
if [ $DEPLOYED_RELOADER -eq 0 ]; then
helm repo add stakater https://stakater.github.io/stakater-charts
helm repo update
helm install stakater-reloader stakater/reloader --set reloader.watchGlobally=false --namespace $KUBE_NAMESPACE
fi
- helm upgrade --install flat-server-$CI_ENVIRONMENT_SLUG helm
deploy_dev:
<<: *DEPLOY
variables:
DOCKER_TAG: dev-$CI_COMMIT_SHA
<<: *DEV
deploy_prod:
<<: *DEPLOY
variables:
DOCKER_TAG: $CI_COMMIT_SHA
<<: *PROD