forked from hashicorp/terraform-github-actions
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathmain.sh
executable file
·212 lines (185 loc) · 5.26 KB
/
main.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
function stripColors {
echo "${1}" | sed 's/\x1b\[[0-9;]*m//g'
}
function hasPrefix {
case ${2} in
"${1}"*)
true
;;
*)
false
;;
esac
}
function parseInputs {
# Required inputs
if [ "${INPUT_TF_ACTIONS_VERSION}" != "" ]; then
tfVersion=${INPUT_TF_ACTIONS_VERSION}
else
echo "Input terraform_version cannot be empty"
exit 1
fi
if [ "${INPUT_TG_ACTIONS_VERSION}" != "" ]; then
tgVersion=${INPUT_TG_ACTIONS_VERSION}
else
echo "Input terragrunt_version cannot be empty"
exit 1
fi
if [ "${INPUT_TF_ACTIONS_SUBCOMMAND}" != "" ]; then
tfSubcommand=${INPUT_TF_ACTIONS_SUBCOMMAND}
else
echo "Input terraform_subcommand cannot be empty"
exit 1
fi
# Optional inputs
tfWorkingDir="."
if [[ -n "${INPUT_TF_ACTIONS_WORKING_DIR}" ]]; then
tfWorkingDir=${INPUT_TF_ACTIONS_WORKING_DIR}
fi
tfBinary="terragrunt"
if [[ -n "${INPUT_TF_ACTIONS_BINARY}" ]]; then
tfBinary=${INPUT_TF_ACTIONS_BINARY}
fi
tfComment=0
if [ "${INPUT_TF_ACTIONS_COMMENT}" == "1" ] || [ "${INPUT_TF_ACTIONS_COMMENT}" == "true" ]; then
tfComment=1
fi
tfCLICredentialsHostname=""
if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME}" != "" ]; then
tfCLICredentialsHostname=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME}
fi
tfCLICredentialsToken=""
if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}" != "" ]; then
tfCLICredentialsToken=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}
fi
tfFmtWrite=0
if [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "1" ] || [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "true" ]; then
tfFmtWrite=1
fi
tfWorkspace="default"
if [ -n "${TF_WORKSPACE}" ]; then
tfWorkspace="${TF_WORKSPACE}"
fi
}
function configureCLICredentials {
if [[ ! -f "${HOME}/.terraformrc" ]] && [[ "${tfCLICredentialsToken}" != "" ]]; then
cat > ${HOME}/.terraformrc << EOF
credentials "${tfCLICredentialsHostname}" {
token = "${tfCLICredentialsToken}"
}
EOF
fi
}
function installTerraform {
if [[ "${tfVersion}" == "latest" ]]; then
echo "Checking the latest version of Terraform"
tfVersion=$(curl -sL https://releases.hashicorp.com/terraform/index.json | jq -r '.versions[].version' | grep -v '[-].*' | sort -rV | head -n 1)
if [[ -z "${tfVersion}" ]]; then
echo "Failed to fetch the latest version"
exit 1
fi
fi
url="https://releases.hashicorp.com/terraform/${tfVersion}/terraform_${tfVersion}_linux_amd64.zip"
echo "Downloading Terraform v${tfVersion}"
curl -s -S -L -o /tmp/terraform_${tfVersion} ${url}
if [ "${?}" -ne 0 ]; then
echo "Failed to download Terraform v${tfVersion}"
exit 1
fi
echo "Successfully downloaded Terraform v${tfVersion}"
echo "Unzipping Terraform v${tfVersion}"
unzip -d /usr/local/bin /tmp/terraform_${tfVersion} &> /dev/null
if [ "${?}" -ne 0 ]; then
echo "Failed to unzip Terraform v${tfVersion}"
exit 1
fi
echo "Successfully unzipped Terraform v${tfVersion}"
}
function installTerragrunt {
if [[ "${tgVersion}" == "latest" ]]; then
echo "Checking the latest version of Terragrunt"
latestURL=$(curl -Ls -o /dev/null -w %{url_effective} https://github.com/gruntwork-io/terragrunt/releases/latest)
tgVersion=${latestURL##*/}
if [[ -z "${tgVersion}" ]]; then
echo "Failed to fetch the latest version"
exit 1
fi
fi
url="https://github.com/gruntwork-io/terragrunt/releases/download/${tgVersion}/terragrunt_linux_amd64"
echo "Downloading Terragrunt ${tgVersion}"
curl -s -S -L -o /tmp/terragrunt ${url}
if [ "${?}" -ne 0 ]; then
echo "Failed to download Terragrunt ${tgVersion}"
exit 1
fi
echo "Successfully downloaded Terragrunt ${tgVersion}"
echo "Moving Terragrunt ${tgVersion} to PATH"
chmod +x /tmp/terragrunt
mv /tmp/terragrunt /usr/local/bin/terragrunt
if [ "${?}" -ne 0 ]; then
echo "Failed to move Terragrunt ${tgVersion}"
exit 1
fi
echo "Successfully moved Terragrunt ${tgVersion}"
}
function main {
# Source the other files to gain access to their functions
scriptDir=$(dirname ${0})
source ${scriptDir}/terragrunt_fmt.sh
source ${scriptDir}/terragrunt_init.sh
source ${scriptDir}/terragrunt_validate.sh
source ${scriptDir}/terragrunt_plan.sh
source ${scriptDir}/terragrunt_apply.sh
source ${scriptDir}/terragrunt_output.sh
source ${scriptDir}/terragrunt_import.sh
source ${scriptDir}/terragrunt_taint.sh
source ${scriptDir}/terragrunt_destroy.sh
parseInputs
configureCLICredentials
installTerraform
cd ${GITHUB_WORKSPACE}/${tfWorkingDir}
case "${tfSubcommand}" in
fmt)
installTerragrunt
terragruntFmt ${*}
;;
init)
installTerragrunt
terragruntInit ${*}
;;
validate)
installTerragrunt
terragruntValidate ${*}
;;
plan)
installTerragrunt
terragruntPlan ${*}
;;
apply)
installTerragrunt
terragruntApply ${*}
;;
output)
installTerragrunt
terragruntOutput ${*}
;;
import)
installTerragrunt
terragruntImport ${*}
;;
taint)
installTerragrunt
terragruntTaint ${*}
;;
destroy)
installTerragrunt
terragruntDestroy ${*}
;;
*)
echo "Error: Must provide a valid value for terragrunt_subcommand"
exit 1
;;
esac
}
main "${*}"