-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp.sh
198 lines (160 loc) · 5.78 KB
/
gcp.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
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
#!/bin/bash
convertsecs() {
((m = (${1} % 3600) / 60))
((s = ${1} % 60))
printf "%02dm %02ds\n" $m $s
}
NODE_VERSION=15.12.0-alpine3.10
API_IMAGE=hellotangle-api
WEB_IMAGE=hellotangle-web
DOMAIN=hellotangle.io
GCP_CONFIGURATION=hellotangle
GCP_HOSTNAME=gcr.io
GCP_PLATFORM=managed
GCP_PROJECT_ID=hellotangle
GCP_VPC_CONNECTOR=hellotangle-api
GCP_VPC_EGRESS=private-ranges-only
GCP_SERVICE_ACCOUNT=gcloud-api@hellotangle.iam.gserviceaccount.com
GCP_AUTH_KEY_FILE=./conf/gcloud/gcloud-api.json
GCP_API_SERVICE=hellotangle-api
GCP_WEB_SERVICE=hellotangle-web
GCP_API_IMAGE_PATH="$GCP_HOSTNAME/$GCP_PROJECT_ID/$API_IMAGE"
GCP_WEB_IMAGE_PATH="$GCP_HOSTNAME/$GCP_PROJECT_ID/$WEB_IMAGE"
API_ACTION=false
WEB_ACTION=false
for i in "$@"; do
case $i in
-a|--api)
API_ACTION=true
shift
;;
-w|--web)
WEB_ACTION=true
shift
;;
esac
done
START=1
if [ "$API_ACTION" = true ] && [ "$WEB_ACTION" = true ]
then
STEPS=12
else
if [ "$API_ACTION" = false ] && [ "$WEB_ACTION" = false ]
then
API_ACTION=true
WEB_ACTION=true
STEPS=12
else
STEPS=7
fi
fi
start_time=$(date +%s)
echo -e "\n($START/$STEPS) Initiating pre-build checks...\n"
START=2
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
if [ "$BRANCH" != "develop" ]
then
echo -e "\t[✘] Branch is set to \"develop\"\n"
echo -e "To switch to the correct branch, please use:\n\n\tgit checkout develop"
exit 1;
else
echo -e "\t[✔] Branch is set to \"develop\""
fi
CURRENT_GCP_PROJECT=$(gcloud config get-value project)
if [ "$CURRENT_GCP_PROJECT" != "$GCP_PROJECT_ID" ]
then
echo -e "\t[✘] Cloud SDK's configuration is set for $GCP_PROJECT_ID\n"
echo -e "To properly configure the SDK for this project, use:\n\n\tgcloud config configurations activate $GCP_CONFIGURATION"
exit 1;
else
echo -e "\t[✔] Cloud SDK's configuration is set for $GCP_PROJECT_ID"
fi
CURRENT_GCP_ACCOUNT=$(gcloud config list account --format "value(core.account)")
if [ "$CURRENT_GCP_ACCOUNT" != "$GCP_SERVICE_ACCOUNT" ]
then
echo -e "\t[✘] Cloud IAM service account is set to $GCP_SERVICE_ACCOUNT\n"
echo -e "To properly set and authenticate the service account for this project, use:\n\n\tgcloud config set account $GCP_SERVICE_ACCOUNT"
echo -e "and"
echo -e "\tgcloud auth activate-service-account $GCP_SERVICE_ACCOUNT --key-file=$GCP_AUTH_KEY_FILE"
exit 1;
else
echo -e "\t[✔] Cloud IAM service account is set to $GCP_SERVICE_ACCOUNT"
fi
if [ "$API_ACTION" = true ]
then
cd api/ || echo -e "[Error]: API folder does not exist" | exit
if ! npm test > /dev/null 2>&1
then
VALID=false
else
VALID=true
fi
if [ "$VALID" = true ] && [ "$API_ACTION" = true ];
then
echo -e "\t[✔] API tests passed"
else
echo -e "\t[✘] API tests passed"
echo -e "To see more details about the errors in the tests, use:\n\n\tcd api/ && npm test"
cd ../
exit 1;
fi
cd ../
fi
echo -e "\n[Success]: Pre-build checks passed!\n"
if [ "$API_ACTION" = true ]
then
cd api/ || echo -e "[Error]: API folder does not exist" | exit
echo -e "($(expr $START)/$STEPS) Building local API image...\n"
docker build . --no-cache --tag "$API_IMAGE"
echo -e "[Success]: Built local API image!\n"
echo -e "($(expr $START + 1)/$STEPS) Tagging local API image for Container Registry..."
docker tag "$API_IMAGE" "$GCP_API_IMAGE_PATH"
echo -e "[Success]: Tagged local API image!\n"
echo -e "($(expr $START + 2)/$STEPS) Pushing local API image to Container Registry...\n"
docker push "$GCP_API_IMAGE_PATH"
echo -e "[Success]: Pushed local API image!\n"
echo -e "($(expr $START + 3)/$STEPS) Deploying to Cloud Run service ($GCP_API_SERVICE)...\n"
gcloud run deploy "$GCP_API_SERVICE" --image="$GCP_API_IMAGE_PATH" --platform="$GCP_PLATFORM" --vpc-connector="$GCP_VPC_CONNECTOR" --vpc-egress="$GCP_VPC_EGRESS"
echo -e "[Success]: Deployed service!\n"
echo -e "($(expr $START + 4)/$STEPS) Removing API images from Docker...\n"
docker rmi "$API_IMAGE:latest"
docker rmi "$GCP_API_IMAGE_PATH:latest"
echo -e "[Success]: Removed API image(s)!\n"
START=7
cd ../
fi
if [ "$WEB_ACTION" = true ]
then
cd web/ || echo -e "[Error]: web folder does not exist" | exit
echo -e "($(expr $START)/$STEPS) Building local web image...\n"
docker build . --no-cache --tag "$WEB_IMAGE"
echo -e "[Success]: Built local web image!\n"
echo -e "($(expr $START + 1)/$STEPS) Tagging local web image for Container Registry..."
docker tag "$WEB_IMAGE" "$GCP_WEB_IMAGE_PATH"
echo -e "[Success]: Tagged local web image!\n"
echo -e "($(expr $START + 2)/$STEPS) Pushing local web image to Container Registry...\n"
docker push "$GCP_WEB_IMAGE_PATH"
echo -e "[Success]: Pushed local web image!\n"
echo -e "($(expr $START + 3)/$STEPS) Deploying to Cloud Run service ($GCP_WEB_SERVICE)...\n"
gcloud run deploy "$GCP_WEB_SERVICE" --image="$GCP_WEB_IMAGE_PATH" --platform="$GCP_PLATFORM" --region="$GCP_REGION"
echo -e "[Success]: Deployed service!\n"
echo -e "($(expr $START + 4)/$STEPS) Removing web images from Docker...\n"
docker rmi "$WEB_IMAGE:latest"
docker rmi "$GCP_WEB_IMAGE_PATH:latest"
echo -e "[Success]: Removed web image(s)!\n"
if [ "$STEPS" = 12 ]
then
START=12
else
START=7
fi
cd ../
fi
echo -e "($(expr $START)/$STEPS) Cleaning up dangling / unused images...\n"
docker rmi "node:$NODE_VERSION"
printf 'y\n' | docker image prune
echo -e "[Success]: Cleaned up images!\n"
end_time=$(date +%s)
execution_time=$(expr $end_time - $start_time)
echo -e "Total build and deployment time elapsed: $(convertsecs $execution_time)\n"
echo -e "COMPLETE! Check it out at https://$DOMAIN/"