forked from GoogleCloudPlatform/reliable-app-platforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·85 lines (72 loc) · 4.11 KB
/
deploy.sh
File metadata and controls
executable file
·85 lines (72 loc) · 4.11 KB
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
#!/usr/bin/env 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.
# Verify that the scripts are being run from Linux and not Mac
if [[ $OSTYPE != "linux-gnu" ]]; then
echo -e "\e[91mERROR: This script has only been tested on Linux. Currently, only Linux (debian) is supported. Please run in Cloud Shell or in a VM running Linux".
exit;
fi
# Export a SCRIPT_DIR var and make all links relative to SCRIPT_DIR
export SCRIPT_DIR=$(dirname $(readlink -f $0 2>/dev/null) 2>/dev/null || echo "${PWD}/$(dirname $0)")
usage()
{
echo ""
echo "Usage: $0"
echo -e "\t--app | -a Must be one of 'nginx' or 'whereami', or 'shop'. Default is 'nginx'."
echo -e "\tExample usage:"op
echo -e "\t./deploy.sh -a nginx"
exit 1 # Exit script after printing help
}
terraform_destroy()
{
echo -e "\e[95mSetting DESTROY var to 'true'...\e[0m"
DESTROY=true
}
# Setting default value
APPLICATION=nginx
unset DESTROY
# Define bash args
while [ "$1" != "" ]; do
case $1 in
--destroy | -d ) shift
terraform_destroy
;;
--app | -a ) shift
APPLICATION=$1
;;
--help | -h ) usage
exit
esac
shift
done
# Create a short SHA until this is tied to a git repo and can use a commit sha
while true; do
SHORT_SHA=$(head -c 64 /dev/urandom | tr -dc 'a-z0-9' | grep -E '^[a-z]' | head -n 1 | cut -c1-63)
if [[ -n $SHORT_SHA ]]; then # Check if SHORT_SHA is not empty
break
fi
done
# Set project to PROJECT_ID or exit
[[ ! "${PROJECT_ID}" ]] && echo -e "Please export PROJECT_ID variable (\e[95mexport PROJECT_ID=<YOUR POROJECT ID>\e[0m)\nExiting." && exit 0
echo -e "\e[95mPROJECT_ID is set to ${PROJECT_ID}\e[0m"
echo -e "\e[95mAPPLICATION is set to ${APPLICATION}\e[0m"
echo -e "\e[95mSHORT_SHA is set to ${SHORT_SHA}\e[0m"
gcloud config set core/project ${PROJECT_ID}
[[ ${APPLICATION} == "nginx" ]] && [[ ${DESTROY} != "true" ]] && echo -e "\e[95mStarting to deploy application ${APPLICATION}...\e[0m" && gcloud builds submit --config=examples/nginx/ci.yaml --substitutions=_PROJECT_ID=${PROJECT_ID},_SHORT_SHA=${SHORT_SHA} --async
[[ ${APPLICATION} == "nginx" ]] && [[ ${DESTROY} == "true" ]] && echo -e "\e[95mStarting to destroy application ${APPLICATION}...\e[0m" && gcloud builds submit --config=examples/nginx/ci-destroy.yaml --substitutions=_PROJECT_ID=${PROJECT_ID} --async
[[ ${APPLICATION} == "whereami" ]] && [[ ${DESTROY} != "true" ]] && echo -e "\e[95mStarting to deploy application ${APPLICATION}...\e[0m" && gcloud builds submit --config=examples/whereami/ci.yaml --substitutions=_PROJECT_ID=${PROJECT_ID},_SHORT_SHA=${SHORT_SHA} --async
[[ ${APPLICATION} == "whereami" ]] && [[ ${DESTROY} == "true" ]] && echo -e "\e[95mStarting to destroy application ${APPLICATION}...\e[0m" && gcloud builds submit --config=examples/whereami/ci-destroy.yaml --substitutions=_PROJECT_ID=${PROJECT_ID} --async
[[ ${APPLICATION} == "shop" ]] && [[ ${DESTROY} != "true" ]] && echo -e "\e[95mStarting to deploy application ${APPLICATION}...\e[0m" && gcloud builds submit --config=examples/shop/ci.yaml --substitutions=_PROJECT_ID=${PROJECT_ID},_SHORT_SHA=${SHORT_SHA} --async
[[ ${APPLICATION} == "shop" ]] && [[ ${DESTROY} == "true" ]] && echo -e "\e[95mStarting to destroy application ${APPLICATION}...\e[0m" && gcloud builds submit --config=examples/shop/ci-destroy.yaml --substitutions=_PROJECT_ID=${PROJECT_ID} --async
echo -e "\e[95mYou can view the Cloudbuild status through https://console.cloud.google.com/cloud-build\e[0m"