-
Notifications
You must be signed in to change notification settings - Fork 5
/
deploy.sh
88 lines (80 loc) · 2.23 KB
/
deploy.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
#!/bin/bash
REPOSITORY=gcr.io/covid-helpline/covid-helpline
SERVICE=covid-helpline
GCPLOGIN=true
GCPCREDS=./keys.json
CREDS=./keys.py
while getopts "nchdot:i:" opt; do
case ${opt} in
h)
echo "Available optional arguments:
-h : help
-n : no gcloud login
-t : version tag
-d : deploy"
exit 0
;;
d)
echo "Building and deploying:" ${REPOSITORY}
DEPLOY=true;
;;
n)
GCPLOGIN=false;
;;
c)
COMMITPUSH=true;
;;
t)
tagNum='^[0-9]+([.][0-9]+)?([.][0-9]+)?([-][A-Za-z0-9.-]+)?$'
if [[ $OPTARG =~ $tagNum ]]
then
VERSION_TAG=$OPTARG
else
echo "A numeric tag number is required"
exit -1
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit -1
;;
esac
done
# make sure you have necessary credentials so deployment does not fail
if ! test -f "$CREDS" ; then
echo "$CREDS not found. Exiting..."
exit 0
fi
if ! test -f "$GCPCREDS" ; then
echo "$GCPCREDS not found. Exiting..."
exit 0
else
echo "$CREDS and $GCPCREDS found. Proceeding with build..."
fi
# login to GCP
if [[ ${GCPLOGIN} == true ]]; then
echo "Login to GCP:"
${SUDO} gcloud auth login
fi
# build image
echo "Using branch:" $(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ -z "$VERSION_TAG" ]; then
echo "No specific tag entered. Here are the tagged repo images on GCP: ";
gcloud container images list-tags $REPOSITORY
echo "Enter new version tag: (or CTRL+C to exit)";
read VERSION_TAG
echo "Building image..."
${SUDO} gcloud builds submit --tag ${REPOSITORY}:${VERSION_TAG}
else
echo "Building image with tag version: '$VERSION_TAG'...";
${SUDO} gcloud builds submit --tag ${REPOSITORY}:${VERSION_TAG}
fi
# push to cloud run
if [[ ${DEPLOY} == true ]]; then
echo "Pushing image to GCP Cloud Run..."
gcloud run deploy ${SERVICE} --image ${REPOSITORY}:${VERSION_TAG} --platform managed --region us-central1
echo "Routing traffic to new revision..."
gcloud alpha run services update-traffic ${SERVICE} --to-latest --platform managed --region us-central1
else
echo "WARNING: You built the image but did not deploy it. Rerun the deploy script with -d to deploy..."
fi