-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path07-create-cloud-build-triggers.sh
executable file
·111 lines (90 loc) · 4.99 KB
/
07-create-cloud-build-triggers.sh
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
#!/bin/bash
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Created with codelabba.rb v.1.4a
source .env.sh || fatal "Config doesnt exist please create .env.sh"
#set -x
set -e
#######################################################################
# This script requires some manual config for the script which I'm working
# on ATM and will add to README.md when ready. Simply put, you need to
# have any GCR repo with `/apps/app01/` and `/apps/app02/` code.
#
#
# Internal ref: b/170325147
#######################################################################
# Add your code here:
# array: https://stackoverflow.com/questions/12317483/array-of-arrays-in-bash
# TeamNumber ; TeamName ; CloudBuild file ; Source To Be listening
TEAMS[0]='T1py;app01;cloudbuild.yaml;apps/app01/;green'
TEAMS[1]='T2rb;app02;cloudbuild.yaml;apps/app02/;red'
# TODO(ricc): make this activation based on ENV var ENABLE_APP03
if [ "true" = "$ENABLE_APP03" ] ; then
TEAMS[2]='T3js;app03;cloudbuild.yaml;apps/app03/;purple'
fi
TRIGGERVERSION="1-8"
# 1.8 20220819 Only pushes on MAIN branch, as suggested by bielski.
# 1.7 20220708 Changed colors cos Ruby is Red and Python is green, but more importantly I dont want to confuse people with blue/green deployment since app_id is orthogonal to deployment stage.
# 1.6 20220610 Added 'cloud-build/**' to trigger changes.
# 1.5b 20220603 I didnt change a thing but DESTROYED everything since i had 4 triggers, 2 in 1.5a and 2 in 1.3 so wanted to have a clean slate.
# 1.5a ????
# 1.4 Added DEPLOY_REGION as per CD necessity.
# 1.3 Made from regional to GLOBAL (local have QUOTA issues) #important
# 1.2 Made better substitutions.
touch /tmp/MyEmptyFile
# TODO(ricc): empty the file
GITHUB_REPO_NAME="clouddeploy-platinum-path"
GCR_REPO="${GITHUB_REPO_OWNER}/clouddeploy-platinum-path"
yellow "*** WARNING ***"
yellow " To run this script you need to have enabled the 6.5 docs in the README.md"
yellow " Also you need to have set up your GITHUB_REPO_OWNER='$GITHUB_REPO_OWNER' (if this an empty string, this ain't gonna work)"
yellow "*** END ***"
echo
for TEAM_ARR in "${TEAMS[@]}"; do
IFS=";" read -r -a arr <<< "${TEAM_ARR}"
TEAM_NUMBER="${arr[0]}"
TEAM_NAME="${arr[1]}"
BUILD_FILE="${arr[2]}"
SRC_SUBFOLDER="${arr[3]}"
FAV_COLOR="${arr[4]}"
#gray $TEAMS
gsutil cp /tmp/MyEmptyFile gs://$SKAFFOLD_BUCKET/skaffold-cache/$TEAM_NAME.txt
SUBSTITUTIONS="_DEPLOY_UNIT=$TEAM_NAME,_REGION=$REGION,_ARTIFACT_REPONAME=$ARTIFACT_REPONAME,_DEPLOY_REGION=$CLOUD_DEPLOY_REGION"
set -x
# This sets up the mirror on GCR
BRANCH_PATTERN="^main$" # bielski suggests to only trigger on main.
#was: gcloud alpha builds triggers create github --repo-owner="$GITHUB_REPO_OWNER" --repo-name="$GITHUB_REPO_NAME" --branch-pattern='.*' \
# proceed_if_error_matches 'ALREADY_EXISTS: trigger' \
proceed_if_error_matches 'ALREADY_EXISTS: trigger' \
gcloud beta builds triggers create github --repo-owner="$GITHUB_REPO_OWNER" --repo-name="$GITHUB_REPO_NAME" --branch-pattern="$BRANCH_PATTERN" \
--description="[$TEAM_NUMBER] CB trigger from CLI for $TEAM_NAME module" --included-files="${SRC_SUBFOLDER}**,*.yaml,cloud-build/**" \
--build-config cloudbuild.yaml --substitutions="$SUBSTITUTIONS" \
--name "$TEAM_NUMBER-CLIv$TRIGGERVERSION-$TEAM_NAME"
# Important note: bYou might want to add "" --region "$REGION" "" which was needed for some.
# * Nate (US) had to add it it or it wouldnt work.
# * Ricc (EU) added it and it wouldnt work WITH, only without.
# I'm unable to explain this difference unless US and EMEA regions are not equal
# If you choose to connect to GCR, use this comand instead (note: might be a bit old and not updated like the one above #metafishing ).
# echo NOT_THIS gcloud alpha builds triggers create cloud-source-repositories \
# --repo=$GITHUB_REPO_OWNER/clouddeploy-platinum-path --branch-pattern='.*' \
# --description="[$TEAM_NUMBER] CB trigger from CLI for $TEAM_NAME module" --included-files="${SRC_SUBFOLDER}**,*.yaml" \
# --build-config cloudbuild.yaml --substitutions="_DEPLOY_UNIT=$TEAM_NAME,_FAVORITE_COLOR=$FAV_COLOR" \
# --region=$REGION --name $TEAM_NUMBER-CLIv$TRIGGERVERSION-$TEAM_NAME
done
# some self gratification :)
echo Here are the triggers:
gcloud alpha builds triggers list --format 'table(name,github.owner,github.user,github.name,description,includedFiles)'
# End of your code here
_allgood_post_script
echo Everything is ok.