-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.gitlab-ci.yml
200 lines (189 loc) · 6.33 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
---
image: alpine:latest
stages:
- build
- test
- review
- release
- cleanup
- deploy_production
build:
stage: build
only:
- branches
image: docker:git
services:
- docker:18.09.7-dind
variables:
DOCKER_DRIVER: overlay2
CI_REGISTRY: 'index.docker.io'
CI_APPLICATION_REPOSITORY: $CI_REGISTRY/$CI_PROJECT_PATH
CI_APPLICATION_TAG: $CI_COMMIT_REF_SLUG
CI_CONTAINER_NAME: ci_job_build_${CI_JOB_ID}
before_script:
- >
if [[ -n "$CI_REGISTRY_USER" ]] && [[ -n "$CI_REGISTRY_PASSWORD" ]]; then
echo "Logging to GitLab Container Registry with CI credentials..."
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD"
echo -e "\n"
else
docker login
echo -e "\n"
fi
if ! docker info &>/dev/null; then
if [ -z "$DOCKER_HOST" -a "$KUBERNETES_PORT" ]; then
export DOCKER_HOST='tcp://docker:2375'
fi
fi
script:
- echo "Building and pushing to GitLab Container Registry..."
- echo `git show --format="%h" HEAD | head -1` > build_info.txt
- echo `git rev-parse --abbrev-ref HEAD` >> build_info.txt
- docker buildx build -t "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" --platform=linux/amd64 --push .
test:
stage: test
script:
- exit 0
only:
- branches
release:
stage: release
image: docker
services:
- docker:18.09.7-dind
variables:
CI_REGISTRY: 'index.docker.io'
CI_APPLICATION_REPOSITORY: $CI_REGISTRY/$CI_PROJECT_PATH
CI_APPLICATION_TAG: $CI_COMMIT_REF_SLUG
CI_CONTAINER_NAME: ci_job_build_${CI_JOB_ID}
before_script:
- >
if [[ -n "$CI_REGISTRY_USER" ]] && [[ -n "$CI_REGISTRY_PASSWORD" ]]; then
echo "Logging to GitLab Container Registry with CI credentials..."
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD"
echo -e "\n"
else
docker login
echo -e "\n"
fi
if ! docker info &>/dev/null; then
if [ -z "$DOCKER_HOST" -a "$KUBERNETES_PORT" ]; then
export DOCKER_HOST='tcp://docker:2375'
fi
fi
script:
- echo "Updating docker images ..."
- docker buildx imagetools create "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" --tag "$CI_APPLICATION_REPOSITORY:$(cat VERSION)"
only:
- master
- main
review:
stage: review
variables:
KUBE_NAMESPACE: review
host: $CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG
TILLER_NAMESPACE: kube-system
CI_APPLICATION_TAG: $CI_COMMIT_REF_SLUG
name: $CI_ENVIRONMENT_SLUG
environment:
name: review/$CI_PROJECT_PATH/$CI_COMMIT_REF_NAME
url: http://$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG
on_stop: stop_review
only:
refs:
- branches
kubernetes: active
except:
- master
- main
before_script:
- apk add -U openssl curl tar gzip bash ca-certificates git
- wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
- wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.23-r3/glibc-2.23-r3.apk
- apk add glibc-2.23-r3.apk
- rm glibc-2.23-r3.apk
- curl https://kubernetes-helm.storage.googleapis.com/helm-v2.13.1-linux-amd64.tar.gz | tar zx
- mv linux-amd64/helm /usr/bin/
- helm version --client
- curl -L -o /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
- chmod +x /usr/bin/kubectl
- kubectl version --client
- kubectl describe namespace "$KUBE_NAMESPACE" || kubectl create namespace "$KUBE_NAMESPACE"
- echo "Checking Tiller..."
- helm init --client-only
- kubectl rollout status -n "$TILLER_NAMESPACE" -w "deployment/tiller-deploy"
- >
if ! helm version --debug; then
echo "Failed to init Tiller."
exit 1
fi
script:
- export track="${1-stable}"
- >
if [[ "$track" != "stable" ]]; then
name="$name-$track"
fi
- echo "Clone deploy repository..."
- git clone http://gitlab-gitlab/$CI_PROJECT_NAMESPACE/reddit-deploy.git
- echo "Download helm dependencies..."
- helm dep update reddit-deploy/reddit
- echo "Deploy helm release $name to $KUBE_NAMESPACE"
- echo "Upgrading existing release..."
- echo "helm upgrade --install --wait --set ui.ingress.host="$host" --set $CI_PROJECT_NAME.image.tag="$CI_APPLICATION_TAG" --namespace="$KUBE_NAMESPACE" --version="$CI_PIPELINE_ID-$CI_JOB_ID" "$name" reddit-deploy/reddit/"
- >
helm upgrade \
--install \
--wait \
--set ui.ingress.host="$host" \
--set $CI_PROJECT_NAME.image.tag="$CI_APPLICATION_TAG" \
--namespace="$KUBE_NAMESPACE" \
--version="$CI_PIPELINE_ID-$CI_JOB_ID" \
"$name" \
reddit-deploy/reddit/
stop_review:
stage: cleanup
variables:
GIT_STRATEGY: none
name: $CI_ENVIRONMENT_SLUG
environment:
name: review/$CI_PROJECT_PATH/$CI_COMMIT_REF_NAME
action: stop
when: manual
allow_failure: true
only:
refs:
- branches
kubernetes: active
except:
- master
- main
before_script:
- apk add -U openssl curl tar gzip bash ca-certificates git
- wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
- wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.23-r3/glibc-2.23-r3.apk
- apk add glibc-2.23-r3.apk
- curl https://storage.googleapis.com/pub/gsutil.tar.gz | tar -xz -C $HOME
- export PATH=${PATH}:$HOME/gsutil
- curl https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz | tar zx
- mv linux-amd64/helm /usr/bin/
- helm version --client
- curl -o /usr/bin/sync-repo.sh https://raw.githubusercontent.com/kubernetes/helm/master/scripts/sync-repo.sh
- chmod a+x /usr/bin/sync-repo.sh
- curl -L -o /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
- chmod +x /usr/bin/kubectl
- kubectl version --client
script:
- helm delete "$name" --purge
deploy_production:
stage: deploy_production
before_script:
- apk add -U curl
script:
- >
curl -X POST \
-F token=$token \
-F ref=master \
http://gitlab-gitlab/api/v4/projects/1/trigger/pipeline
only:
- master
- main