-
Notifications
You must be signed in to change notification settings - Fork 22
/
cloudbuild.yml
226 lines (205 loc) · 7.07 KB
/
cloudbuild.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
---
steps:
# FIXME: if two builds started at the same time they will block each other
# indefinitely. We need to have some kind of queue to know which build started
# first, and let this one proceed if needed. Maybe Pub/Sub from GCP can help?
- id: Wait until parallel instances of this build completed
name: gcr.io/cloud-builders/gcloud@sha256:4d329656103c8a5851e966f177a4d45c70d6d63f0de5fbeabd21e1a4947c32f9
entrypoint: bash
args:
- -ceuo
- pipefail
- |
function get_build_trigger_id() {
echo "$(gcloud builds describe "$$1" | awk '$$1 == "buildTriggerId:" {print $$2}')"
}
cur_build_trigger_id="$(get_build_trigger_id "$BUILD_ID")"
while (true); do
retry=1
for build_id in $(gcloud builds list --ongoing | awk 'NR > 1 {print $$1}'); do
[[ "$BUILD_ID" == "$$build_id" ]] && continue
build_trigger_id="$(get_build_trigger_id "$$build_id")"
if [[ "$$cur_build_trigger_id" == "$$build_trigger_id" ]]; then
echo "Other instances of this build running. Waiting for them to finish."
sleep 10
retry=0
break
fi
done
[[ $$retry -eq 1 ]] && break
done
- id: Setup
name: gcr.io/cloud-builders/git@sha256:00557896b9dbae78e37f6e1ad10dab7edbc54d425fb222031545523e35a897fc
entrypoint: bash
args:
- -ceuo
- pipefail
- |
# Restore .git dir removed by GCB:
ssh-keyscan -t rsa github.com >> /root/.ssh/known_hosts
git init
git remote add origin https://github.com/${_GITHUB_DEMO_OWNER}/${_GITHUB_DEMO_REPONAME}
git fetch
git reset --hard origin/master
git branch -u origin/master
# Setup git
git config user.name "Google Cloud Build"
git config user.email "[email protected]"
git submodule update --init
# Get list of apps to update
apps="$(echo $TAG_NAME | cut -d'_' -f2- | tr '_' ' ')"
if [[ "$$apps" == "all" ]]; then
apps="$(ls -m third-party/microservices/src/ | tr -d ',')"
fi
echo "$$apps" > /config/apps
- id: Build, push microservices to GCR
name: gcr.io/kaniko-project/executor:debug-v0.15.0
entrypoint: sh
args:
- -ce
- |
for app in $(cat /config/apps); do
executor --context=third-party/microservices/src/$$app \
--destination=gcr.io/$PROJECT_ID/$$app:$SHORT_SHA \
--cache=false --single-snapshot
done
- id: Decrypt ssh private key
name: gcr.io/cloud-builders/gcloud@sha256:4d329656103c8a5851e966f177a4d45c70d6d63f0de5fbeabd21e1a4947c32f9
entrypoint: bash
args:
- -ceuo
- pipefail
- |
base64 -d id_rsa.enc | \
gcloud -q kms decrypt --ciphertext-file=- --plaintext-file=/root/.ssh/id_rsa \
--location=global --keyring=$_KMS_KEYRING_NAME --key=$_KMS_KEY_NAME_GITHUB
- id: Setup git authorization
name: gcr.io/cloud-builders/git@sha256:00557896b9dbae78e37f6e1ad10dab7edbc54d425fb222031545523e35a897fc
entrypoint: bash
args:
- -ceuo
- pipefail
- |
cat <<EOF > /root/.ssh/config
Hostname github.com
IdentityFile /root/.ssh/id_rsa
EOF
chmod 600 /root/.ssh/id_rsa
git remote set-url origin [email protected]:${_GITHUB_DEMO_OWNER}/${_GITHUB_DEMO_REPONAME}
- id: "[staging] Update images tags to new versions in Kubernetes manifests"
name: ilyalesikov/kustomize
dir: ./kubernetes/overlays/staging
entrypoint: bash
args:
- -ceuo
- pipefail
- |
for app in $(cat /config/apps); do
kustomize edit set image $$app=gcr.io/$PROJECT_ID/$$app:$SHORT_SHA
done
- id: "[staging] Commit, push images tags update"
name: gcr.io/cloud-builders/git@sha256:00557896b9dbae78e37f6e1ad10dab7edbc54d425fb222031545523e35a897fc
dir: ./kubernetes/overlays/staging
entrypoint: bash
args:
- -ceuo
- pipefail
- |
git add kustomization.yml
git commit -m '[CI/staging] Update images tags' || true
git push
- id: "[staging] Deploy, test applications"
name: ilyalesikov/gke-demo
dir: ./terraform/environments/staging/kubernetes
entrypoint: bash
args:
- -ceuo
- pipefail
- |
terragrunt apply-all --terragrunt-non-interactive \
--terragrunt-include-external-dependencies
argocd app sync hipstershop-staging --prune --timeout 300
argocd app wait hipstershop-staging --health --timeout 300
# TODO: implement proper e2e testing
tries=0
until (kubectl get services -n hipstershop frontend-external 2>&1 1>/dev/null); do
if [[ $$tries -gt 20 ]]; then
echo "Can't find frontend-external service. Aborting"
exit 1
fi
tries=$((tries+1))
sleep 2
done
ip="$(kubectl get services -n hipstershop frontend-external \
-o custom-columns=IP:.status.loadBalancer.ingress[0].ip --no-headers)"
tries=0
until (curl -sSL "http://$$ip" | grep '>Buy<' 1>/dev/null); do
if [[ $$tries -gt 40 ]]; then
echo "Can't reach application on http://$$ip with curl. Aborting"
exit 1
fi
tries=$((tries+1))
sleep 2
done
- id: "[staging] Destroy applications"
name: ilyalesikov/gke-demo
dir: ./terraform/environments/staging/kubernetes
entrypoint: bash
args:
- -ceuo
- pipefail
- |
terragrunt destroy -target=k8s_manifest.hipstershop-argo-app -auto-approve
- id: "[prod] Update images tags to new versions in Kubernetes manifests"
name: ilyalesikov/kustomize
dir: ./kubernetes/overlays/prod
entrypoint: bash
args:
- -ceuo
- pipefail
- |
for app in $(cat /config/apps); do
kustomize edit set image $$app=gcr.io/$PROJECT_ID/$$app:$SHORT_SHA
done
- id: "[prod] Commit, push images tags update"
name: gcr.io/cloud-builders/git@sha256:00557896b9dbae78e37f6e1ad10dab7edbc54d425fb222031545523e35a897fc
dir: ./kubernetes/overlays/prod
entrypoint: bash
args:
- -ceuo
- pipefail
- |
git pull --rebase --autostash
git add kustomization.yml
git commit -m '[CI/prod] Update images tags' || true
git push
- id: "[prod] Canary deploy applications"
name: ilyalesikov/gke-demo
dir: ./terraform/environments/staging/kubernetes
entrypoint: bash
args:
- -ceuo
- pipefail
- |
argocd app sync hipstershop-prod --prune --timeout 300
argocd app wait hipstershop-prod --health --timeout 300
timeout: 2h
options:
volumes:
- name: config
path: /config
- name: ssh
path: /root/.ssh
- name: argocd
path: /root/.argocd
- name: gsutil
path: /root/.gsutil
- name: gcloud
path: /root/.config/gcloud
- name: kubectl
path: /root/.kube
- name: docker
path: /root/.docker
env:
- HOME=/root
- TF_VAR_project_id=$PROJECT_ID