Skip to content

Commit

Permalink
Allow to choose to run commands with terraform or terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
rriveramdz committed Apr 21, 2020
1 parent 7a77dae commit 0dacfa8
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 17 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ jobs:
- name: 'Terragrunt Format'
uses: the-commons-project/terragrunt-github-actions@master
with:
tf_actions_actions_version: ${{ env.tf_version }}
tg_actions_actions_version: ${{ env.tg_version }}
tf_actions_actions_subcommand: 'fmt'
tf_actions_actions_working_dir: ${{ env.tf_working_dir }}
tf_actions_actions_comment: true
tf_actions_version: ${{ env.tf_version }}
tg_actions_version: ${{ env.tg_version }}
tf_actions_binary: 'terraform'
tf_actions_subcommand: 'fmt'
tf_actions_working_dir: ${{ env.tf_working_dir }}
tf_actions_comment: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: 'Terragrunt Init'
Expand All @@ -54,6 +55,7 @@ jobs:
with:
tf_actions_version: ${{ env.tf_version }}
tg_actions_version: ${{ env.tg_version }}
tf_actions_binary: 'terraform'
tf_actions_subcommand: 'validate'
tf_actions_working_dir: ${{ env.tf_working_dir }}
tf_actions_comment: true
Expand All @@ -80,6 +82,7 @@ Inputs configure Terraform GitHub Actions to perform different actions.
| Input Name | Description | Required |
|:------------------------------------|:-----------------------------------------------------------|:--------:|
| tf_actions_subcommand | The Terraform/Terragrunt subcommand to execute. | `Yes` |
| tf_actions_binary | The binary to run the commands with | `No` |
| tf_actions_version | The Terraform version to install and execute. If set to `latest`, the latest stable version will be used. | `Yes` |
| tg_actions_version | The Terragrunt version to install and execute. If set to `latest`, the latest stable version will be used. | `Yes` |
| tf_actions_cli_credentials_hostname | Hostname for the CLI credentials file. Defaults to `app.terraform.io`. | `No` |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
tf_actions_subcommand:
description: 'Terraform or Terragrunt subcommand to execute.'
required: true
tf_actions_binary:
description: 'Binary to use. Terraform or Terragrunt'
default: 'terragrunt'
tf_actions_version:
description: 'Terraform version to install.'
required: true
Expand Down
5 changes: 5 additions & 0 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ function parseInputs {
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
Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function terragruntApply {
# Gather the output of `terragrunt apply`.
echo "apply: info: applying Terragrunt configuration in ${tfWorkingDir}"
applyOutput=$(terragrunt apply -auto-approve -input=false ${*} 2>&1)
applyOutput=$(${tfBinary} apply -auto-approve -input=false ${*} 2>&1)
applyExitCode=${?}
applyCommentStatus="Failed"

Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_destroy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function terragruntDestroy {
# Gather the output of `terragrunt destroy`.
echo "destroy: info: destroying Terragrunt-managed infrastructure in ${tfWorkingDir}"
destroyOutput=$(terragrunt destroy -auto-approve -input=false ${*} 2>&1)
destroyOutput=$(${tfBinary} destroy -auto-approve -input=false ${*} 2>&1)
destroyExitCode=${?}
destroyCommentStatus="Failed"

Expand Down
8 changes: 4 additions & 4 deletions src/terragrunt_fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function terragruntFmt {

# Gather the output of `terragrunt fmt`.
echo "fmt: info: checking if Terragrunt files in ${tfWorkingDir} are correctly formatted"
fmtOutput=$(terragrunt fmt -check=true -write=false -diff ${fmtRecursive} ${*} 2>&1)
fmtOutput=$(${tfBinary} fmt -check=true -write=false -diff ${fmtRecursive} ${*} 2>&1)
fmtExitCode=${?}

# Exit code of 0 indicates success. Print the output and exit.
Expand All @@ -29,19 +29,19 @@ function terragruntFmt {
fi

# Exit code of !0 and !2 indicates failure.
echo "fmt: error: Terraform files in ${tfWorkingDir} are incorrectly formatted"
echo "fmt: error: Terragrunt files in ${tfWorkingDir} are incorrectly formatted"
echo "${fmtOutput}"
echo
echo "fmt: error: the following files in ${tfWorkingDir} are incorrectly formatted"
fmtFileList=$(terraform fmt -check=true -write=false -list ${fmtRecursive})
fmtFileList=$(${tfBinary} fmt -check=true -write=false -list ${fmtRecursive})
echo "${fmtFileList}"
echo

# Comment on the pull request if necessary.
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ]; then
fmtComment=""
for file in ${fmtFileList}; do
fmtFileDiff=$(terraform fmt -check=true -write=false -diff "${file}" | sed -n '/@@.*/,//{/@@.*/d;p}')
fmtFileDiff=$(${tfBinary} fmt -check=true -write=false -diff "${file}" | sed -n '/@@.*/,//{/@@.*/d;p}')
fmtComment="${fmtComment}
<details><summary><code>${tfWorkingDir}/${file}</code></summary>
Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function terragruntImport {
# Gather the output of `terragrunt import`.
echo "import: info: importing Terragrunt configuration in ${tfWorkingDir}"
importOutput=$(terragrunt import -input=false ${*} 2>&1)
importOutput=$(${tfBinary} import -input=false ${*} 2>&1)
importExitCode=${?}
importCommentStatus="Failed"

Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function terragruntInit {
# Gather the output of `terragrunt init`.
echo "init: info: initializing Terragrunt configuration in ${tfWorkingDir}"
initOutput=$(terragrunt init -input=false ${*} 2>&1)
initOutput=$(${tfBinary} init -input=false ${*} 2>&1)
initExitCode=${?}

# Exit code of 0 indicates success. Print the output and exit.
Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function terragruntOutput {
# Gather the output of `terragrunt output`.
echo "output: info: gathering all the outputs for the Terragrunt configuration in ${tfWorkingDir}"
outputOutput=$(terragrunt output -json ${*} 2>&1)
outputOutput=$(${tfBinary} output -json ${*} 2>&1)
outputExitCode=${?}

# Exit code of 0 indicates success. Print the output and exit.
Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function terragruntPlan {
# Gather the output of `terragrunt plan`.
echo "plan: info: planning Terragrunt configuration in ${tfWorkingDir}"
planOutput=$(terragrunt plan -detailed-exitcode -input=false ${*} 2>&1)
planOutput=$(${tfBinary} plan -detailed-exitcode -input=false ${*} 2>&1)
planExitCode=${?}
planHasChanges=false
planCommentStatus="Failed"
Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_taint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function terragruntTaint {
# Gather the output of `terragrunt taint`.
echo "taint: info: tainting terragrunt configuration in ${tfWorkingDir}"
#taintOutput=$(terragrunt taint ${*} 2>&1)
taintOutput=$(for resource in ${*}; do terragrunt taint -allow-missing $resource; done 2>&1)
taintOutput=$(for resource in ${*}; do ${tfBinary} taint -allow-missing $resource; done 2>&1)
taintExitCode=${?}
taintCommentStatus="Failed"

Expand Down
2 changes: 1 addition & 1 deletion src/terragrunt_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function terragruntValidate {
# Gather the output of `terragrunt validate`.
echo "validate: info: validating Terragrunt configuration in ${tfWorkingDir}"
validateOutput=$(terragrunt validate ${*} 2>&1)
validateOutput=$(${tfBinary} validate ${*} 2>&1)
validateExitCode=${?}

# Exit code of 0 indicates success. Print the output and exit.
Expand Down

0 comments on commit 0dacfa8

Please sign in to comment.