-
Notifications
You must be signed in to change notification settings - Fork 3
/
azure-pipelines.yml
788 lines (688 loc) · 27.7 KB
/
azure-pipelines.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
name: $(BuildID)
#
# Setup pipeline triggers
# https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#triggers
#
# Enable Pull request validation triggers
# https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#pr-trigger
pr:
autoCancel: false # indicates whether additional pushes to a PR should cancel in-progress runs for the same PR.
branches:
include: # branch names which will trigger a build
- stg
- pro
exclude: # branch names which will not trigger a build
- master
- base
- dev
paths:
exclude: # file paths which will not trigger a build
- README.md
- LICENSE.md
- docs/*
- helpers/*
- scripts/*
- docker/*
# Enable Continuous integration (CI)
# https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#push-trigger
trigger:
batch: true # batch changes if true, start a new build for every push if false
branches:
include: # branch names which will trigger a build
- base
- stg
- pro
exclude: # branch names which will not trigger a build
- master
- dev
paths:
exclude: # file paths which will not trigger a build
- README.md
- LICENSE.md
- docs/*
- helpers/*
- scripts/*
- docker/*
#
# Setup pipeline environment
#
# Setup pipeline resources
# https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#resources
resources:
containers:
- container: gcloud
image: google/cloud-sdk:243.0.0-slim
- container: gitopsci
image: raelga/gitopsci:v1.0
# Setup variable groups
# https://docs.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#variables
variables:
- group: gcloud-environment
# The gcloud-environment contains:
# - GCLOUD_SERVICE_ACCOUNT: The Google Cloud Service Account (as secret)
# - GCLOUD_PROJECT_ID: The Google Cloud Project ID (as secret)
- name: env.provider
value: gcp
- name: env.name
value: $[ coalesce(variables['System.PullRequest.TargetBranch'], variables['Build.SourceBranchName']) ]
- name: env.prefix
value: $[ format('pr{0}', variables['System.PullRequest.PullRequestNumber']) ]
# Paths
- name: env.paths.infra
value: infra/$(env.provider)/deployments/$(env.name)
- name: env.paths.storage
value: storage/$(env.name)
- name: env.paths.k8s
value: k8s/overlays/$(env.name)
#
# Setup pipeline stages
#
stages:
- stage: Environment
displayName: Environment
jobs:
- job: EnvPrint
displayName: Check vars
steps:
- bash: |
echo "Trigger information:"
echo " Reason $(Build.Reason)"
echo " Environment $(env.name)"
echo " Provider $(env.provider)"
echo " Path Infra $(env.paths.infra)"
echo " Path Storage $(env.paths.storage)"
echo " Path K8S $(env.paths.k8s)"
displayName: Print trigger information
- script: |
echo " Prefix $(env.prefix)"
displayName: Print prefix information
condition: eq(variables['Build.Reason'], 'PullRequest')
- job: GoogleCloudEnvCheck
displayName: Check gcloud
container: gcloud
steps:
- bash: |
if which gcloud &> /dev/null;
then
echo "[i] gcloud installed with $(gcloud version | grep 'Google Cloud SDK').";
else
echo "[!] gcloud SDK is not installed."; exit 1;
fi
displayName: Check Google Cloud SDK version
- bash: |
if which gsutil &> /dev/null;
then
echo "[i] gsutil installed with $(gsutil version).";
else
echo "[!] gsutil is not installed."; exit 1;
fi
displayName: Check gsutil version
- bash: |
if test ! -z ${GCLOUD_SERVICE_ACCOUNT:-};
then
if gcloud auth activate-service-account \
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d);
then
echo "[i] Service account $(gcloud config get-value account) set.";
else
echo "[!] Unable to activate the service account.";
exit 1;
fi
else
echo "[!] GCLOUD_SERVICE_ACCOUNT environment variable must be set.";
exit 1;
fi
displayName: Check Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- bash: |
if test ! -z ${GCLOUD_PROJECT_ID:-};
then
echo "[i] GCLOUD_PROJECT_ID project set to ${GCLOUD_PROJECT_ID}.";
if test ACTIVE == $(gcloud projects describe ${GCLOUD_PROJECT_ID} \
--format="value(lifecycleState)"
);
then
echo "[i] ${GCLOUD_PROJECT_ID} project is ACTIVE.";
else
echo "[!] ${GCLOUD_PROJECT_ID} status must be active.";
exit 1;
fi
else
echo "[!] GCLOUD_PROJECT_ID environment variable must be set.";
exit 1;
fi
displayName: Check Google Cloud SDK project
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- job: gitopsciEnvCheck
displayName: Check gitopsci
container: gitopsci
steps:
- bash: |
if which gcloud &> /dev/null;
then
echo "[i] gcloud installed with $(gcloud version | grep 'Google Cloud SDK').";
else
echo "[!] gcloud SDK is not installed."; exit 1;
fi
displayName: Check Google Cloud SDK version
- bash: |
if test ! -z ${GCLOUD_SERVICE_ACCOUNT:-};
then
if gcloud auth activate-service-account \
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d);
then
echo "[i] Service account $(gcloud config get-value account) set.";
else
echo "[!] Unable to activate the service account.";
exit 1;
fi
else
echo "[!] GCLOUD_SERVICE_ACCOUNT environment variable must be set.";
exit 1;
fi
displayName: Check Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- bash: |
if which kubectl &> /dev/null;
then
if test $(sed 's/.*Minor:"\([~0-9]\+\)".*/\1/' <(kubectl version --client=true 2>&1) ) -ge 14;
then
echo "[i] kubectl installed with $(kubectl version --client=true).";
else
echo "[!] kubectl minor version should be greather than 14."
fi
else
echo "[!] kubectl is not installed."; exit 1;
fi
displayName: Check kubectl version
- stage: Infrastructure
displayName: Infrastructure
jobs:
- job: InfrastructureDiff
displayName: Preview changes
container: gcloud
steps:
- script: >
gcloud auth activate-service-account
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d)
displayName: Activate Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- script: >
gcloud config set project ${GCLOUD_PROJECT_ID}
displayName: Activate Google Cloud SDK project
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- script: >
echo '##vso[task.setvariable variable=PREFIX]$(env.prefix)'
displayName: Set environment prefix if required
condition: eq(variables['Build.Reason'], 'PullRequest')
- bash: |
while read manifest;
do
deployment=$(sed 's#.*/\([a-z]\+\)/\(.*\).yaml$#\1-\2#' <<< ${manifest});
test -z "${PREFIX:-}" || deployment=${PREFIX}-${deployment};
if test ! -z "$(gcloud deployment-manager deployments list \
--format="value(name)" --filter="name=${deployment}")";
then
echo "[i] ${deployment} already exists."
gcloud deployment-manager deployments describe ${deployment};
else
if gcloud deployment-manager deployments create ${deployment} \
--config <(echo "resources:");
then
echo "[i] Empty deployment for ${deployment} created.";
else
echo "[!] Unable to create ${deployment} empty deployment.";
fi
fi
done < <(find $(env.paths.infra) -name '*yaml');
displayName: Checking deployment
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- bash: |
while read manifest;
do
deployment=$(sed 's#.*/\([a-z]\+\)/\(.*\).yaml$#\1-\2#' <<< ${manifest});
test -z "${PREFIX:-}" || deployment=${PREFIX}-${deployment};
echo "[i] Previewing deployment";
echo " Deployment ${deployment}";
echo " Manifest ${manifest}";
echo " Project ${GCLOUD_PROJECT_ID}";
gcloud deployment-manager deployments update ${deployment} \
--config ${manifest} --preview \
--create-policy CREATE_OR_ACQUIRE --delete-policy DELETE \
|| { "[!] Previewing of ${deployment} failed."; exit 1; }
gcloud deployment-manager deployments cancel-preview ${deployment} \
> /dev/null \
|| { "[!] Preview cancel of ${deployment} failed."; exit 1; }
done < <(find $(env.paths.infra) -name '*yaml');
displayName: Preview changes with deployment manager
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- job: InfrastructureApply
displayName: Apply changes
container: gcloud
dependsOn:
- InfrastructureDiff
steps:
- script: >
gcloud auth activate-service-account
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d)
displayName: Activate Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- script: >
gcloud config set project ${GCLOUD_PROJECT_ID}
displayName: Activate Google Cloud SDK project
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- script: >
echo '##vso[task.setvariable variable=PREFIX]$(env.prefix)'
displayName: Set environment prefix if required
condition: eq(variables['Build.Reason'], 'PullRequest')
- bash: |
while read manifest;
do
deployment=$(sed 's#.*/\([a-z]\+\)/\(.*\).yaml$#\1-\2#' <<< ${manifest});
test -z "${PREFIX:-}" || deployment=${PREFIX}-${deployment};
echo "Update:";
echo " Deployment $deployment";
echo " Manifest $manifest";
echo " Project ${GCLOUD_PROJECT_ID}";
gcloud deployment-manager deployments update ${deployment} \
--config ${manifest} --preview \
--create-policy CREATE_OR_ACQUIRE --delete-policy DELETE \
|| { "[!] Previewing of ${deployment} failed."; exit 1; }
gcloud deployment-manager deployments update ${deployment} \
--create-policy CREATE_OR_ACQUIRE --delete-policy DELETE \
|| { "[!] Update of ${deployment} failed."; exit 1; }
done < <(find $(env.paths.infra) -name '*yaml');
displayName: Apply changes with deployment manager
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- job: InfrastructureCleanup
displayName: Clean up
container: gcloud
dependsOn:
- InfrastructureDiff
- InfrastructureApply
condition: failed()
steps:
- script: >
gcloud auth activate-service-account
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d)
displayName: Activate Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- script: >
gcloud config set project ${GCLOUD_PROJECT_ID}
displayName: Activate Google Cloud SDK project
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- script: >
echo '##vso[task.setvariable variable=PREFIX]$(env.prefix)'
displayName: Set environment prefix if required
condition: eq(variables['Build.Reason'], 'PullRequest')
- bash: |
while read manifest;
do
deployment=$(sed 's#.*/\([a-z]\+\)/\(.*\).yaml$#\1-\2#' <<< ${manifest});
test -z "${PREFIX:-}" || deployment=${PREFIX}-${deployment};
if test -n $(gcloud deployment-manager deployments list \
--format="value(name)" --filter="name:${deployment}");
then
if test 0 -eq $(grep -c -- '- id:' \
<(gcloud deployment-manager deployments \
describe ${deployment} --format="yaml(resources[].id)"
);
then
echo "Deleting empty deployment:";
echo " Deployment $deployment";
echo " Manifest $manifest";
echo " Project ${GCLOUD_PROJECT_ID}";
gcloud deployment-manager deployments delete \
${deployment} --delete-policy ABANDON \
|| { "[!] Delete of ${deployment} failed."; exit 1; }
fi
fi
done < <(find $(env.paths.infra) -name '*yaml');
displayName: Delete empty deployment manager deployments
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- stage: Storage
displayName: Storage
jobs:
- job: CheckStorage
displayName: Check sources
steps:
- bash: |
if test -d $(env.paths.storage);
then
echo "[i] $(env.paths.storage) folder found.";
echo "##vso[task.setvariable variable=exists;isOutput=true]true";
else
echo "[i] No storage folder found, skipping storage stage.";
echo "##vso[task.setvariable variable=exists;isOutput=true]false";
fi
name: storageFolder
displayName: Check if the storage folder exists
- bash: |
STORAGE_SOURCES_COUNT=$( \
find $(env.paths.storage) -maxdepth 1 -mindepth 1 -type d | wc -l
);
if test ${STORAGE_SOURCES_COUNT} -gt 0;
then
echo "[i] ${STORAGE_SOURCES_COUNT} storage sources found.";
echo "##vso[task.setvariable variable=exists;isOutput=true]true";
else
echo "[i] No storage sources found, skipping storage stage.";
echo "##vso[task.setvariable variable=exists;isOutput=true]false";
fi
name: storageSources
condition: eq(variables['storageFolder.exists'],'true')
displayName: Check if there are storage source folders
- job: UpdateStorage
displayName: Update content
container: gcloud
dependsOn:
- CheckStorage
condition:
and(
eq(dependencies.CheckStorage.outputs['storageFolder.exists'],'true'),
eq(dependencies.CheckStorage.outputs['storageSources.exists'],'true')
)
steps:
- bash: |
gcloud auth activate-service-account \
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d)
displayName: Activate Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- script: >
echo '##vso[task.setvariable variable=PREFIX]$(env.prefix)'
displayName: Set environment prefix if required
condition: eq(variables['Build.Reason'], 'PullRequest')
- bash: |
while read bucket_src;
do
bucket=$(sed 's#.*/\([a-z]\+\)/\(.*\)$#\1-\2#' <<< ${bucket_src});
test -z "${PREFIX}" || bucket=${PREFIX}-${bucket};
if gsutil ls gs://$bucket;
then
echo "[i] $bucket exists."
else
echo "[i] $bucket doesn't exists or is not accesible.";
fi
done < <(find $(env.paths.storage) -maxdepth 1 -mindepth 1 -type d)
displayName: Check if the buckets exists
- bash: |
while read bucket_src;
do
bucket=$(sed 's#.*/\([a-z]\+\)/\(.*\)$#\1-\2#' <<< ${bucket_src});
test -z "${PREFIX}" || bucket=${PREFIX}-${bucket};
if test -f ${bucket_src}/.ci/delete-extra-files;
then
echo "Sync deleting extra files not present in origin.";
echo " Source ${bucket_src}";
echo " Destination ${bucket}";
gsutil rsync -R -J -C -c -d ${bucket_src}/ gs://${bucket}
else
echo "Sync without deleting extra files not present in remote."
echo " Source ${bucket_src}";
echo " Destination ${bucket}";
gsutil rsync -R -J -C -c ${bucket_src}/ gs://${bucket}
fi
done < <(find $(env.paths.storage) -maxdepth 1 -mindepth 1 -type d)
displayName: Update buckets contents
- stage: Kubernetes
displayName: Kubernetes
jobs:
- job: CheckGkeManifests
displayName: Check manifests
steps:
- bash: |
if test -d $(env.paths.k8s);
then
echo "[i] $(env.paths.k8s) found";
echo "##vso[task.setvariable variable=exists;isOutput=true]true"
else
echo "[i] $(env.paths.k8s) folder not found, skipping kubernetes stage";
echo "##vso[task.setvariable variable=exists;isOutput=true]false";
fi
name: k8sManifests
displayName: Check k8s resources manifests
- bash: |
GKE_MANIFEST_COUNT=$( \
egrep -rlo '^\s+- name.\s+cluster-name' $(env.paths.infra) | wc -l
);
if test ${GKE_MANIFEST_COUNT} -eq 0;
then
echo "[i] No cluster deployment manifest found, skipping kubernetes stage.";
echo "##vso[task.setvariable variable=exists;isOutput=true]false";
elif test ${GKE_MANIFEST_COUNT} -gt 1;
then
echo "[!] More than one cluster deployment manifest found.";
echo "##vso[task.setvariable variable=exists;isOutput=true]false";
exit 1;
else
manifest=$(egrep -rlo '^\s+- name.\s+cluster-name' $(env.paths.infra));
echo "[i] GKE cluster deployment manifest found at ${manifest}";
# Outputs
echo "##vso[task.setvariable variable=exists;isOutput=true]true"
echo "##vso[task.setvariable variable=path;isOutput=true]${manifest}"
fi
name: gkeManifest
condition: eq(variables['k8sManifests.exists'],'true')
displayName: Check GKE infrastructure manifests
- job: CheckGkeCluster
displayName: Check GKE cluster
container: gitopsci
dependsOn: CheckGkeManifests
condition: >
and(
eq(dependencies.CheckGkeManifests.outputs['gkeManifest.exists'],'true'),
eq(dependencies.CheckGkeManifests.outputs['k8sManifests.exists'],'true')
)
variables:
- name: GKE_MANIFEST
value: $[ dependencies.CheckGkeManifests.outputs['gkeManifest.path'] ]
steps:
- script: >
gcloud auth activate-service-account \
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d)
displayName: Activate Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- script: >
gcloud config set project ${GCLOUD_PROJECT_ID}
displayName: Activate Google Cloud SDK project
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- script: >
echo '##vso[task.setvariable variable=PREFIX]$(env.prefix)'
displayName: Set environment prefix if required
condition: eq(variables['Build.Reason'], 'PullRequest')
- bash: |
test -f "${GKE_MANIFEST}" \
|| { echo "[!] ${GKE_MANIFEST} file not found."; exit 1; }
deployment=$(sed 's#.*/\([a-z]\+\)/\(.*\).yaml$#\1-\2#' <<< ${GKE_MANIFEST});
test -n "${deployment}" \
|| { echo "[!] Unable to obtain the deployment name."; exit 1; }
test -z "${PREFIX:-}" || deployment=${PREFIX}-${deployment};
echo "##vso[task.setvariable variable=name;isOutput=true]${deployment}";
name: gkeDeployment
displayName: Get the GKE cluster deployment name
- bash: |
GCLOUD_CLUSTER_NAME=$(gcloud deployment-manager deployments \
describe $(gkeDeployment.name) \
--format='get[](outputs.name.cluster-name.finalValue)' \
);
if test -n "${GCLOUD_CLUSTER_NAME}";
then
echo "[i] GCLOUD_CLUSTER_NAME set to ${GCLOUD_CLUSTER_NAME}";
else
echo "[i] Unable to get `cluster-name` output from $(gkeDeployment.name) deployment.";
exit 1;
fi
GCLOUD_CLUSTER_TRAEFIK_DNS=$(gcloud deployment-manager deployments \
describe $(gkeDeployment.name) \
--format='get[](outputs.name.cluster-traefik-lb-dns.finalValue)' \
);
if test -n "${GCLOUD_CLUSTER_TRAEFIK_DNS}";
then
echo "[i] GCLOUD_CLUSTER_TRAEFIK_DNS set to ${GCLOUD_CLUSTER_TRAEFIK_DNS}";
else
echo "[i] Unable to get `cluster-traefik-lb-dns` output from $(gkeDeployment.name) deployment.";
exit 1;
fi
GCLOUD_CLUSTER_LOCATION=$(gcloud deployment-manager deployments \
describe $(gkeDeployment.name) \
--format='get[](outputs.name.cluster-location.finalValue)'
);
if test -n "${GCLOUD_CLUSTER_LOCATION}";
then
echo "[i] GCLOUD_CLUSTER_LOCATION set to ${GCLOUD_CLUSTER_LOCATION}";
else
echo "[i] Unable to get `cluster-location` output from $(gkeDeployment.name) deployment.";
exit 1;
fi
if egrep -q '[a-z]+-[a-z]+[0-9]-[a-z]' <<<${GCLOUD_CLUSTER_LOCATION};
then
GCLOUD_CLUSTER_LOCATION_FLAG="--zone ${GCLOUD_CLUSTER_LOCATION}";
else
GCLOUD_CLUSTER_LOCATION_FLAG="--region ${GCLOUD_CLUSTER_LOCATION}";
fi
echo "[i] GCLOUD_CLUSTER_LOCATION_FLAG set to ${GCLOUD_CLUSTER_LOCATION_FLAG}";
# Outputs
echo "##vso[task.setvariable variable=name;isOutput=true]${GCLOUD_CLUSTER_NAME}";
echo "##vso[task.setvariable variable=location;isOutput=true]${GCLOUD_CLUSTER_LOCATION}";
echo "##vso[task.setvariable variable=locationFlag;isOutput=true]${GCLOUD_CLUSTER_LOCATION_FLAG}";
echo "##vso[task.setvariable variable=traefikDns;isOutput=true]${GCLOUD_CLUSTER_TRAEFIK_DNS}";
name: gkeCluster
displayName: Get GKE cluster info
- bash: |
GITHUB_API_PR_COMMENTS_URL="https://api.github.com/repos/${BUILD_REPOSITORY_NAME}/issues/${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}/comments"
if curl \
-s "${GITHUB_API_PR_COMMENTS_URL}" \
-H "Authorization: token ${GITHUB_TOKEN}" \
-X POST \
-d "{ \"body\": \"Traefik URL http://${GCLOUD_CLUSTER_TRAEFIK_DNS}:8080/\" }";
then
echo "[i] GitHub message sent!";
else
echo "[i] Unable to send GitHub message, skipping.";
fi
displayName: Add message to GitHub pull request
condition: eq(variables['Build.Reason'], 'PullRequest')
env:
GITHUB_TOKEN: $(GITHUB_TOKEN)
- bash: |
if gcloud container clusters get-credentials $(gkeCluster.name) \
$(gkeCluster.locationFlag);
then
echo "[i] kubectl cluster set to $(kubectl config current-context).";
else
echo "[!] Unable to retrieve cluster credentials.";
exit 1;
fi
kubectl cluster-info \
|| { echo "[!] Unable to connect to the GKE cluster."; exit 1; }
displayName: Check GKE cluster credentials
- job: KubernetesDiff
displayName: Preview changes
container: gitopsci
dependsOn:
- CheckGkeManifests
- CheckGkeCluster
variables:
- name: GKE_CLUSTER_NAME
value: $[ dependencies.CheckGkeCluster.outputs['gkeCluster.name'] ]
- name: GKE_CLUSTER_LOCATION_FLAG
value: $[ dependencies.CheckGkeCluster.outputs['gkeCluster.locationFlag'] ]
- name: GKE_TRAEFIK_DNS
value: $[ dependencies.CheckGkeCluster.outputs['gkeCluster.traefikDns'] ]
steps:
- script: >
gcloud auth activate-service-account \
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d)
displayName: Activate Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- script: >
gcloud config set project ${GCLOUD_PROJECT_ID}
displayName: Activate Google Cloud SDK project
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- bash: >
gcloud container clusters get-credentials \
${GKE_CLUSTER_NAME} ${GKE_CLUSTER_LOCATION_FLAG}
displayName: Get GKE cluster credentials
- bash: |
echo "[i] Cluster traefik DNS is ${GKE_TRAEFIK_DNS}";
while read manifest;
do
echo "[i] Updating ${manifest} GKE_TRAEFIK_DNS string.";
sed -i "s/GKE_TRAEFIK_DNS/${GKE_TRAEFIK_DNS}/g" ${manifest};
done < <(egrep -lrn 'host:.*GKE_TRAEFIK_DNS' k8s/);
displayName: Update GKE_TRAEFIK_DNS string in k8s manifests
- bash: >
kubectl apply --dry-run -k '$(env.paths.k8s)'
displayName: Check kubernetes manifests
- bash: |
if kubectl diff -k $(env.paths.k8s);
then
echo "[i] No changes detected, skipping apply job."
echo "##vso[task.setvariable variable=changes;isOutput=true]false";
else
echo "[i] Changes detected against the deployed resources."
echo "##vso[task.setvariable variable=changes;isOutput=true]true";
fi
name: k8sDiff
displayName: Preview kubernetes manifests changes
- job: KubernetesApply
displayName: Apply changes
container: gitopsci
dependsOn:
- CheckGkeManifests
- CheckGkeCluster
- KubernetesDiff
condition: >
eq(dependencies.KubernetesDiff.outputs['k8sDiff.changes'],'true')
variables:
- name: GKE_CLUSTER_NAME
value: $[ dependencies.CheckGkeCluster.outputs['gkeCluster.name'] ]
- name: GKE_CLUSTER_LOCATION_FLAG
value: $[ dependencies.CheckGkeCluster.outputs['gkeCluster.locationFlag'] ]
- name: GKE_TRAEFIK_DNS
value: $[ dependencies.CheckGkeCluster.outputs['gkeCluster.traefikDns'] ]
steps:
- script: >
gcloud auth activate-service-account \
--key-file <(echo ${GCLOUD_SERVICE_ACCOUNT} | base64 -d)
displayName: Activate Google Cloud SDK service account
env:
GCLOUD_SERVICE_ACCOUNT: $(GCLOUD_SERVICE_ACCOUNT)
- script: >
gcloud config set project ${GCLOUD_PROJECT_ID}
displayName: Activate Google Cloud SDK project
env:
GCLOUD_PROJECT_ID: $(GCLOUD_PROJECT_ID)
- bash: >
gcloud container clusters get-credentials \
${GKE_CLUSTER_NAME} ${GKE_CLUSTER_LOCATION_FLAG}
displayName: Get GKE cluster credentials
- bash: |
echo "[i] Cluster traefik DNS is ${GKE_TRAEFIK_DNS}";
while read manifest;
do
echo "[i] Updating ${manifest} GKE_TRAEFIK_DNS string.";
sed -i "s/GKE_TRAEFIK_DNS/${GKE_TRAEFIK_DNS}/g" ${manifest};
done < <(egrep -lrn 'host:.*GKE_TRAEFIK_DNS' k8s/);
displayName: Update GKE_TRAEFIK_DNS string in k8s manifests
- bash: >
kubectl apply --kustomize='$(env.paths.k8s)'
displayName: Apply kubernetes manifests