-
Notifications
You must be signed in to change notification settings - Fork 7
/
deploy.sh
123 lines (106 loc) · 3.42 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
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
#!/bin/bash
# Exit immediately if a command returns a non-zero status
set -e
# Printing script usage
program_name=$0
usage () {
echo "usage: $program_name { dev | prod | --google-env-file google-env-file.json --github-env-file github-env-file.json --backend-config-file backend.json } [ --skip-packer-deploy ] [ --skip-terraform-deploy ] [ --auto-approve ]"
exit 1
}
packer_deploy () {
# Deploy packer image
echo "Deploying runner image using packer..."
cd image
base_packer_cmd="bash packer.sh --env-file $google_env_file_path --packer-action"
packer_cmd_build="$base_packer_cmd 'build'"
set +e
eval "$packer_cmd_build"
packer_cmd_exit_code=$?
set -e
if [ $packer_cmd_exit_code -ne 0 ]; then
echo "Packer build failed, maybe the image already exists, check logs for more info"
if [ "$auto_approve" = false ]; then
read -r -p "Would you like to force deploy the image? (y/n):" input
fi
if [ "$input" = "y" ] || [ "$auto_approve" = true ]; then
packer_cmd_build_force="$base_packer_cmd 'build -force'"
eval "$packer_cmd_build_force"
fi
fi
echo "Deploying runner image using packer done"
cd "$project_root_path"
}
terraform_deploy () {
# Compile TS
declare -a js_src_folders=("modules/start-and-stop/function" "modules/github-api/src" "modules/github-hook/function")
for js_src_folder in "${js_src_folders[@]}"
do
cd "$project_root_path/$js_src_folder"
npm install
npm run build
done
cd "$project_root_path"
# Deploy terraform
echo "Deploying infra using terraform..."
terraform init -backend-config="$backend_config_file_path"
terraform_apply_cmd="terraform apply -var-file=$google_env_file_path -var-file=$github_env_file_path"
if [ "$auto_approve" = true ]; then
terraform_apply_cmd="$terraform_apply_cmd -auto-approve"
fi
eval "$terraform_apply_cmd"
echo "Deploying infra using terraform done"
}
# Default scripts params
skip_packer_deploy=false
skip_terraform_deploy=false
auto_approve=false
# Parsing script params
while true; do
case "$1" in
--google-env-file ) google_env_file="$2"; shift 2 ;;
--github-env-file ) github_env_file="$2"; shift 2 ;;
--backend-config-file ) backend_config_file="$2"; shift 2 ;;
dev ) dev=true; shift 1 ;;
prod ) prod=true; shift 1;;
--skip-packer-deploy ) skip_packer_deploy=true; shift 1;;
--skip-terraform-deploy ) skip_terraform_deploy=true; shift 1;;
--auto-approve ) auto_approve=true; shift 1;;
* ) break ;;
esac
done
if [ "$dev" = true ]; then
google_env_file="google-dev.tfvars.json"
github_env_file="github-dev.tfvars.json"
backend_config_file="backend-dev.tfvars.json"
fi
if [ "$prod" = true ]; then
google_env_file="google-prod.tfvars.json"
github_env_file="github-prod.tfvars.json"
backend_config_file="backend-prod.tfvars.json"
fi
# Checking script params
if [ -z "$google_env_file" ]; then
usage
fi
if [ -z "$github_env_file" ]; then
usage
fi
if [ -z "$backend_config_file" ]; then
usage
fi
google_env_file_path=$(realpath "$google_env_file")
github_env_file_path=$(realpath "$github_env_file")
backend_config_file_path=$(realpath "$backend_config_file")
project_root_path=$(realpath "$(dirname "$0")/..")
# cd project root directory
cd "$project_root_path"
if [ "$skip_packer_deploy" = true ]; then
echo "Skipping packer deploy"
else
packer_deploy
fi
if [ "$skip_terraform_deploy" = true ]; then
echo "Skipping terraform deploy"
else
terraform_deploy
fi