Skip to content

Commit 1e8ef56

Browse files
committed
Add support to terragrunt
1 parent 1acd6aa commit 1e8ef56

11 files changed

+81
-37
lines changed

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ inputs:
1111
tf_actions_version:
1212
description: 'Terraform version to install.'
1313
required: true
14+
default: 'latest'
15+
tg_actions_version:
16+
description: 'Terragrunt version to install.'
17+
required: true
18+
default: 'latest'
1419
tf_actions_cli_credentials_hostname:
1520
description: 'Hostname for the CLI credentials file.'
1621
default: 'app.terraform.io'

src/main.sh

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ function parseInputs {
2424
exit 1
2525
fi
2626

27+
if [ "${INPUT_TG_ACTIONS_VERSION}" != "" ]; then
28+
tgVersion=${INPUT_TG_ACTIONS_VERSION}
29+
else
30+
echo "Input terragrunt_version cannot be empty"
31+
exit 1
32+
fi
33+
2734
if [ "${INPUT_TF_ACTIONS_SUBCOMMAND}" != "" ]; then
2835
tfSubcommand=${INPUT_TF_ACTIONS_SUBCOMMAND}
2936
else
@@ -103,62 +110,94 @@ function installTerraform {
103110
echo "Successfully unzipped Terraform v${tfVersion}"
104111
}
105112

113+
function installTerragrunt {
114+
if [[ "${tgVersion}" == "latest" ]]; then
115+
echo "Checking the latest version of Terragrunt"
116+
latestURL=$(curl -Ls -o /dev/null -w %{url_effective} https://github.com/gruntwork-io/terragrunt/releases/latest)
117+
tgVersion=${latestURL##*/}
118+
119+
if [[ -z "${tgVersion}" ]]; then
120+
echo "Failed to fetch the latest version"
121+
exit 1
122+
fi
123+
fi
124+
125+
url="https://github.com/gruntwork-io/terragrunt/releases/download/${tgVersion}/terragrunt_linux_386"
126+
127+
echo "Downloading Terragrunt ${tgVersion}"
128+
curl -s -S -L -o /tmp/terragrunt_${tgVersion} ${url}
129+
if [ "${?}" -ne 0 ]; then
130+
echo "Failed to download Terragrunt v${tgVersion}"
131+
exit 1
132+
fi
133+
echo "Successfully downloaded Terragrunt v${tgVersion}"
134+
135+
echo "Unzipping Terragrunt v${tgVersion}"
136+
unzip -d /usr/local/bin /tmp/terragrunt_${tgVersion} &> /dev/null
137+
if [ "${?}" -ne 0 ]; then
138+
echo "Failed to unzip Terragrunt ${tgVersion}"
139+
exit 1
140+
fi
141+
echo "Successfully unzipped Terragrunt ${tgVersion}"
142+
}
143+
106144
function main {
107145
# Source the other files to gain access to their functions
108146
scriptDir=$(dirname ${0})
109-
source ${scriptDir}/terraform_fmt.sh
110-
source ${scriptDir}/terraform_init.sh
111-
source ${scriptDir}/terraform_validate.sh
112-
source ${scriptDir}/terraform_plan.sh
113-
source ${scriptDir}/terraform_apply.sh
114-
source ${scriptDir}/terraform_output.sh
115-
source ${scriptDir}/terraform_import.sh
116-
source ${scriptDir}/terraform_taint.sh
117-
source ${scriptDir}/terraform_destroy.sh
118-
147+
source ${scriptDir}/terragrunt_fmt.sh
148+
source ${scriptDir}/terragrunt_init.sh
149+
source ${scriptDir}/terragrunt_validate.sh
150+
source ${scriptDir}/terragrunt_plan.sh
151+
source ${scriptDir}/terragrunt_apply.sh
152+
source ${scriptDir}/terragrunt_output.sh
153+
source ${scriptDir}/terragrunt_import.sh
154+
source ${scriptDir}/terragrunt_taint.sh
155+
source ${scriptDir}/terragrunt_destroy.sh
156+
157+
installTerraform
119158
parseInputs
120159
configureCLICredentials
121160
cd ${GITHUB_WORKSPACE}/${tfWorkingDir}
122161

123162
case "${tfSubcommand}" in
124163
fmt)
125-
installTerraform
126-
terraformFmt ${*}
164+
installTerragrunt
165+
terragruntFmt ${*}
127166
;;
128167
init)
129-
installTerraform
130-
terraformInit ${*}
168+
installTerragrunt
169+
terragruntInit ${*}
131170
;;
132171
validate)
133-
installTerraform
134-
terraformValidate ${*}
172+
installTerragrunt
173+
terragruntValidate ${*}
135174
;;
136175
plan)
137-
installTerraform
138-
terraformPlan ${*}
176+
installTerragrunt
177+
terragruntPlan ${*}
139178
;;
140179
apply)
141-
installTerraform
142-
terraformApply ${*}
180+
installTerragrunt
181+
terragruntApply ${*}
143182
;;
144183
output)
145-
installTerraform
146-
terraformOutput ${*}
184+
installTerragrunt
185+
terragruntOutput ${*}
147186
;;
148187
import)
149-
installTerraform
150-
terraformImport ${*}
188+
installTerragrunt
189+
terragruntImport ${*}
151190
;;
152191
taint)
153-
installTerraform
154-
terraformTaint ${*}
192+
installTerragrunt
193+
terragruntTaint ${*}
155194
;;
156195
destroy)
157-
installTerraform
158-
terraformDestroy ${*}
196+
installTerragrunt
197+
terragruntDestroy ${*}
159198
;;
160199
*)
161-
echo "Error: Must provide a valid value for terraform_subcommand"
200+
echo "Error: Must provide a valid value for terragrunt_subcommand"
162201
exit 1
163202
;;
164203
esac
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/bin/bash
22

3-
function terraformPlan {
4-
# Gather the output of `terraform plan`.
5-
echo "plan: info: planning Terraform configuration in ${tfWorkingDir}"
6-
planOutput=$(terraform plan -detailed-exitcode -input=false ${*} 2>&1)
3+
function terragruntPlan {
4+
# Gather the output of `terragrunt plan`.
5+
echo "plan: info: planning Terragrunt configuration in ${tfWorkingDir}"
6+
planOutput=$(terragrunt plan -detailed-exitcode -input=false ${*} 2>&1)
77
planExitCode=${?}
88
planHasChanges=false
99
planCommentStatus="Failed"
1010

1111
# Exit code of 0 indicates success with no changes. Print the output and exit.
1212
if [ ${planExitCode} -eq 0 ]; then
13-
echo "plan: info: successfully planned Terraform configuration in ${tfWorkingDir}"
13+
echo "plan: info: successfully planned Terragrunt configuration in ${tfWorkingDir}"
1414
echo "${planOutput}"
1515
echo
1616
echo ::set-output name=tf_actions_plan_has_changes::${planHasChanges}
@@ -23,7 +23,7 @@ function terraformPlan {
2323
planExitCode=0
2424
planHasChanges=true
2525
planCommentStatus="Success"
26-
echo "plan: info: successfully planned Terraform configuration in ${tfWorkingDir}"
26+
echo "plan: info: successfully planned Terragrunt configuration in ${tfWorkingDir}"
2727
echo "${planOutput}"
2828
echo
2929
if echo "${planOutput}" | egrep '^-{72}$' &> /dev/null; then
@@ -37,14 +37,14 @@ function terraformPlan {
3737

3838
# Exit code of !0 indicates failure.
3939
if [ ${planExitCode} -ne 0 ]; then
40-
echo "plan: error: failed to plan Terraform configuration in ${tfWorkingDir}"
40+
echo "plan: error: failed to plan Terragrunt configuration in ${tfWorkingDir}"
4141
echo "${planOutput}"
4242
echo
4343
fi
4444

4545
# Comment on the pull request if necessary.
4646
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ] && ([ "${planHasChanges}" == "true" ] || [ "${planCommentStatus}" == "Failed" ]); then
47-
planCommentWrapper="#### \`terraform plan\` ${planCommentStatus}
47+
planCommentWrapper="#### \`terragrunt plan\` ${planCommentStatus}
4848
<details><summary>Show Output</summary>
4949
5050
\`\`\`

0 commit comments

Comments
 (0)