-
Notifications
You must be signed in to change notification settings - Fork 133
/
.gitlab-ci.yml
163 lines (153 loc) · 4.52 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
variables:
# Domain appended to the environment slug for reviews
REVIEW_BASE_DOMAIN: "example.com"
# The domain used for the live deployment
LIVE_DOMAIN: "example.com"
KUBE_NAMESPACE: presentation-gitlab-k8s
image:
name: "golang:1.16.2-buster"
entrypoint: ["/bin/sh", "-c"]
stages:
- test
- build
- release
- review
- deploy
test:
stage: test
script:
- make test
test2:
stage: test
script:
- sleep 3
- echo "This task runs in parallel!"
compile:
stage: build
script:
# Add here all the dependencies, or use glide/govendor/...
# to get them automatically.
- make build
artifacts:
paths:
- app
# Example job to upload the built release to a S3 server with mc
# For this you need to set `S3_ACCESS_KEY` and `S3_SECRET_KEY` in your GitLab project CI's secret variables
#release_upload:
# stage: release
# image:
# name: minio/mc
# entrypoint: ["/bin/sh", "-c"]
# script:
# - echo "=> We already have artifact sotrage in GitLab! This is for demonstational purposes only."
# - mc config host add mys3host https://my-s3-host.example.com $ACCESS_KEY $SECRET_KEY S3v4
# - mc mb -p mys3host/build-release-$CI_PROJECT_NAME/
# - mc cp app mys3host/build-release-$CI_PROJECT_NAME/
image_build:
stage: release
image:
name: "docker:19.03.13"
entrypoint: ["/bin/sh", "-c"]
variables:
DOCKER_HOST: tcp://docker:2375
# See https://about.gitlab.com/blog/2019/07/31/docker-in-docker-with-docker-19-dot-03/
# Yeah, disabling TLS isn't the best way, but most Gitlab sadly haven't fixed their
# runners, so if you read this message, follow the post and fix the runners to use TLS
DOCKER_TLS_CERTDIR: ""
services:
- name: "docker:19.03.13-dind"
before_script:
# Show docker daemon info (optional, good for debugging)
- docker info
# Login to the Gitlab Container registry
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
script:
# Build the container image
- docker build -t "$CI_REGISTRY_IMAGE:latest" .
# Tag the container image from latest to the commit ref
- docker tag "$CI_REGISTRY_IMAGE:latest" "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
# Push the container image
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
# If we are building a tag, push the `latest` container image tag too
- if [ ! -z "$CI_COMMIT_TAG" ]; then docker push "$CI_REGISTRY_IMAGE:latest"; fi
deploy_review:
image:
name: "alpine/helm:3.5.3"
entrypoint: ["/bin/sh", "-c"]
stage: review
# Only deploy the review for branches (this includes the `main` / `master` branch)
# but not tags
only:
- branches
except:
- tags
environment:
name: "review/$CI_BUILD_REF_NAME"
url: "https://$CI_ENVIRONMENT_SLUG-$KUBE_NAMESPACE.$REVIEW_BASE_DOMAIN"
on_stop: stop_review
kubernetes:
namespace: "$KUBE_NAMESPACE"
variables:
DOMAIN: "$CI_ENVIRONMENT_SLUG-$KUBE_NAMESPACE.$REVIEW_BASE_DOMAIN"
script:
- |
helm upgrade \
--install \
--values values.yaml \
--set image.repository="$CI_REGISTRY_IMAGE" \
--set image.tag="$CI_COMMIT_REF_NAME" \
--set ciVars.domain="$DOMAIN" \
--set ciVars.CI_ENVIRONMENT_SLUG="$CI_ENVIRONMENT_SLUG" \
--set ciVars.CI_PROJECT_PATH_SLUG="$CI_PROJECT_PATH_SLUG" \
"$CI_BUILD_REF_NAME" \
./charts/app
stop_review:
image:
name: "alpine/helm:3.5.3"
entrypoint: ["/bin/sh", "-c"]
stage: review
variables:
GIT_STRATEGY: "none"
when: manual
# Don't stop the "review" for master branch or tags,
# it is only for branches (so PRs (most of the time))
only:
- branches
except:
- master
- main
- tags
environment:
name: "review/$CI_BUILD_REF_NAME"
action: stop
kubernetes:
namespace: "$KUBE_NAMESPACE"
script:
- helm uninstall "$CI_BUILD_REF_NAME"
deploy_live:
image:
name: alpine/helm:3.5.3
entrypoint: ["/bin/sh", "-c"]
stage: deploy
environment:
name: live
url: "https://$LIVE_DOMAIN"
kubernetes:
namespace: "$KUBE_NAMESPACE"
only:
- tags
when: manual
variables:
DOMAIN: "$LIVE_DOMAIN"
script:
- |
helm upgrade \
--install \
--values values.yaml \
--set image.repository="$CI_REGISTRY_IMAGE" \
--set image.tag="$CI_COMMIT_REF_NAME" \
--set ciVars.domain="$DOMAIN" \
--set ciVars.CI_ENVIRONMENT_SLUG="$CI_ENVIRONMENT_SLUG" \
--set ciVars.CI_PROJECT_PATH_SLUG="$CI_PROJECT_PATH_SLUG" \
live \
./charts/app