From 6ff9a604263ab4b6cf28e3c5c862b242d36891fd Mon Sep 17 00:00:00 2001 From: David Gethings Date: Fri, 29 Jan 2021 17:18:30 +0000 Subject: [PATCH 01/12] initial test of tg show command support --- src/main.sh | 117 +++++++++++++++++++++-------------------- src/terragrunt_show.sh | 53 +++++++++++++++++++ 2 files changed, 114 insertions(+), 56 deletions(-) create mode 100755 src/terragrunt_show.sh diff --git a/src/main.sh b/src/main.sh index ae9b3963..2b5bab3a 100755 --- a/src/main.sh +++ b/src/main.sh @@ -1,21 +1,21 @@ #!/bin/bash -function stripColors { +function stripColors() { echo "${1}" | sed 's/\x1b\[[0-9;]*m//g' } -function hasPrefix { +function hasPrefix() { case ${2} in - "${1}"*) - true - ;; - *) - false - ;; + "${1}"*) + true + ;; + *) + false + ;; esac } -function parseInputs { +function parseInputs() { # Required inputs if [ "${INPUT_TF_ACTIONS_VERSION}" != "" ]; then tfVersion=${INPUT_TF_ACTIONS_VERSION} @@ -75,9 +75,9 @@ function parseInputs { fi } -function configureCLICredentials { +function configureCLICredentials() { if [[ ! -f "${HOME}/.terraformrc" ]] && [[ "${tfCLICredentialsToken}" != "" ]]; then - cat > ${HOME}/.terraformrc << EOF + cat >${HOME}/.terraformrc < /dev/null + unzip -d /usr/local/bin /tmp/terraform_${tfVersion} &>/dev/null if [ "${?}" -ne 0 ]; then echo "Failed to unzip Terraform v${tfVersion}" exit 1 @@ -115,7 +115,7 @@ function installTerraform { echo "Successfully unzipped Terraform v${tfVersion}" } -function installTerragrunt { +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) @@ -139,7 +139,7 @@ function installTerragrunt { echo "Moving Terragrunt ${tgVersion} to PATH" chmod +x /tmp/terragrunt - mv /tmp/terragrunt /usr/local/bin/terragrunt + mv /tmp/terragrunt /usr/local/bin/terragrunt if [ "${?}" -ne 0 ]; then echo "Failed to move Terragrunt ${tgVersion}" exit 1 @@ -147,7 +147,7 @@ function installTerragrunt { echo "Successfully moved Terragrunt ${tgVersion}" } -function main { +function main() { # Source the other files to gain access to their functions scriptDir=$(dirname ${0}) source ${scriptDir}/terragrunt_fmt.sh @@ -159,6 +159,7 @@ function main { source ${scriptDir}/terragrunt_import.sh source ${scriptDir}/terragrunt_taint.sh source ${scriptDir}/terragrunt_destroy.sh + source ${scriptDir}/terragrunt_show.sh parseInputs configureCLICredentials @@ -166,46 +167,50 @@ function main { 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 - ;; + fmt) + installTerragrunt + terragruntFmt ${*} + ;; + init) + installTerragrunt + terragruntInit ${*} + ;; + validate) + installTerragrunt + terragruntValidate ${*} + ;; + plan) + installTerragrunt + terragruntPlan ${*} + ;; + show) + installTerragrunt + terragruntShow ${*} + ;; + 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 } diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh new file mode 100755 index 00000000..8ddacf68 --- /dev/null +++ b/src/terragrunt_show.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +function terragruntShow() { + # Gather the output of `terragrunt plan`. + echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" + showOutput=$(${tfBinary} show -detailed-exitcode -no-color ${*} 2>&1) + showExitCode=${?} + showCommentStatus="Failed" + + # Exit code of 0 indicates success with no changes. Print the output and exit. + if [ ${showExitCode} -eq 0 ]; then + echo "show: info: successfully showed Terragrunt configuration in ${tfWorkingDir}" + echo "${showOutput}" + echo + exit ${showExitCode} + fi + + # Exit code of !0 indicates failure. + if [ ${showExitCode} -ne 0 ]; then + echo "show: error: failed to show Terragrunt configuration in ${tfWorkingDir}" + echo "${showOutput}" + echo + fi + + # Comment on the pull request if necessary. + if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ] && [ "${showCommentStatus}" == "Failed" ]; then + showCommentWrapper="#### \`${tfBinary} show\` ${showCommentStatus} +
Show Output + +\`\`\` +${showOutput} +\`\`\` + +
+ +*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`, Workspace: \`${tfWorkspace}\`*" + + showCommentWrapper=$(stripColors "${showCommentWrapper}") + echo "show: info: creating JSON" + showPayload=$(echo "${showCommentWrapper}" | jq -R --slurp '{body: .}') + showCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url) + echo "show: info: commenting on the pull request" + echo "${showPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${showCommentsURL}" >/dev/null + fi + + # https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/38372/highlight/true#M3322 + showOutput="${showOutput//'%'/'%25'}" + showOutput="${showOutput//$'\n'/'%0A'}" + showOutput="${showOutput//$'\r'/'%0D'}" + + echo "::set-output name=tf_actions_show_output::${showOutput}" + exit ${showExitCode} +} From e4e823076c5a55982f51fe8921986c9de6fd31af Mon Sep 17 00:00:00 2001 From: David Gethings Date: Fri, 29 Jan 2021 17:27:47 +0000 Subject: [PATCH 02/12] remove command arg --- src/terragrunt_show.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index 8ddacf68..08621151 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,7 +3,7 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - showOutput=$(${tfBinary} show -detailed-exitcode -no-color ${*} 2>&1) + showOutput=$(${tfBinary} show -no-color ${*} 2>&1) showExitCode=${?} showCommentStatus="Failed" From 1027c5d915e70dad497a5297776d6cf0f14267fd Mon Sep 17 00:00:00 2001 From: David Gethings Date: Fri, 29 Jan 2021 19:50:27 +0000 Subject: [PATCH 03/12] nasty hack --- src/terragrunt_show.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index 08621151..c6c6d7d1 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,7 +3,8 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - showOutput=$(${tfBinary} show -no-color ${*} 2>&1) + # showOutput=$(${tfBinary} show -no-color ${*} 2>&1) + showOutput=$(${tfBinary} show -no-color -json plan.out >plan.json) showExitCode=${?} showCommentStatus="Failed" From 7378e5cd50c7aa0eabc756fc866ddd3e0d3af30f Mon Sep 17 00:00:00 2001 From: David Gethings Date: Fri, 29 Jan 2021 20:00:19 +0000 Subject: [PATCH 04/12] revert nasty hack --- src/terragrunt_show.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index c6c6d7d1..badc93b8 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,8 +3,8 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - # showOutput=$(${tfBinary} show -no-color ${*} 2>&1) - showOutput=$(${tfBinary} show -no-color -json plan.out >plan.json) + showOutput=$(${tfBinary} show -no-color ${*} 2>&1) + # showOutput=$(${tfBinary} show -no-color -json plan.out >plan.json) showExitCode=${?} showCommentStatus="Failed" From 3efcbc8dcdbe1b257c5b773165af70c5de85d377 Mon Sep 17 00:00:00 2001 From: David Gethings Date: Fri, 29 Jan 2021 20:03:39 +0000 Subject: [PATCH 05/12] disable output redirect --- src/terragrunt_show.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index badc93b8..3c0effb8 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,7 +3,7 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - showOutput=$(${tfBinary} show -no-color ${*} 2>&1) + showOutput=$(${tfBinary} show -no-color ${*}) # showOutput=$(${tfBinary} show -no-color -json plan.out >plan.json) showExitCode=${?} showCommentStatus="Failed" From 1e6defca13f0ab121d64829b7e6482cd76303f68 Mon Sep 17 00:00:00 2001 From: David Gethings Date: Fri, 29 Jan 2021 20:12:02 +0000 Subject: [PATCH 06/12] helps to add all outputs... --- action.yml | 52 ++++++++++++++++++++++-------------------- src/terragrunt_show.sh | 3 +-- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/action.yml b/action.yml index 07724456..e94aec89 100644 --- a/action.yml +++ b/action.yml @@ -1,47 +1,49 @@ -name: 'Terragrunt GitHub Actions' -description: 'Runs Terragrunt commands via GitHub Actions.' -author: 'HashiCorp, Inc. Terraform Team ' +name: "Terragrunt GitHub Actions" +description: "Runs Terragrunt commands via GitHub Actions." +author: "HashiCorp, Inc. Terraform Team " branding: - icon: 'cloud' - color: 'purple' + icon: "cloud" + color: "purple" inputs: tf_actions_subcommand: - description: 'Terraform or Terragrunt subcommand to execute.' + description: "Terraform or Terragrunt subcommand to execute." required: true tf_actions_binary: - description: 'Binary to use. Terraform or Terragrunt' - default: 'terragrunt' + description: "Binary to use. Terraform or Terragrunt" + default: "terragrunt" tf_actions_version: - description: 'Terraform version to install.' + description: "Terraform version to install." required: true - default: 'latest' + default: "latest" tg_actions_version: - description: 'Terragrunt version to install.' + description: "Terragrunt version to install." required: true - default: 'latest' + default: "latest" tf_actions_cli_credentials_hostname: - description: 'Hostname for the CLI credentials file.' - default: 'app.terraform.io' + description: "Hostname for the CLI credentials file." + default: "app.terraform.io" tf_actions_cli_credentials_token: - description: 'Token for the CLI credentials file.' + description: "Token for the CLI credentials file." tf_actions_comment: - description: 'Whether or not to comment on pull requests.' + description: "Whether or not to comment on pull requests." default: true tf_actions_working_dir: - description: 'Terragrunt working directory.' - default: '.' + description: "Terragrunt working directory." + default: "." tf_actions_fmt_write: - description: 'Write Terragrunt fmt changes to source files.' + description: "Write Terragrunt fmt changes to source files." default: false outputs: tf_actions_output: - description: 'The Terragrunt outputs in JSON format.' + description: "The Terragrunt outputs in JSON format." tf_actions_plan_has_changes: - description: 'Whether or not the Terragrunt plan contained changes.' + description: "Whether or not the Terragrunt plan contained changes." tf_actions_plan_output: - description: 'The Terragrunt plan output.' + description: "The Terragrunt plan output." tf_actions_fmt_written: - description: 'Whether or not the Terragrunt formatting was written to source files.' + description: "Whether or not the Terragrunt formatting was written to source files." + tf_actions_show_output: + description: "The Terragrunt show output" runs: - using: 'docker' - image: './Dockerfile' + using: "docker" + image: "./Dockerfile" diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index 3c0effb8..08621151 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,8 +3,7 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - showOutput=$(${tfBinary} show -no-color ${*}) - # showOutput=$(${tfBinary} show -no-color -json plan.out >plan.json) + showOutput=$(${tfBinary} show -no-color ${*} 2>&1) showExitCode=${?} showCommentStatus="Failed" From ce4b8bf385dc8f654fb34f5c6a1a4ab3560cbe65 Mon Sep 17 00:00:00 2001 From: David Gethings Date: Mon, 1 Feb 2021 11:26:45 +0000 Subject: [PATCH 07/12] remove stderr redirect to stdout --- src/terragrunt_show.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index 08621151..1380d888 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,7 +3,8 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - showOutput=$(${tfBinary} show -no-color ${*} 2>&1) + # showOutput=$(${tfBinary} show -no-color ${*} 2>&1) + showOutput=$(${tfBinary} show -no-color ${*}) showExitCode=${?} showCommentStatus="Failed" From 5cf2220b68fc7a8aa2c06d90daf110086570688d Mon Sep 17 00:00:00 2001 From: David Gethings Date: Mon, 1 Feb 2021 11:31:45 +0000 Subject: [PATCH 08/12] write output to a file --- src/terragrunt_show.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index 1380d888..13cbc454 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,8 +3,7 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - # showOutput=$(${tfBinary} show -no-color ${*} 2>&1) - showOutput=$(${tfBinary} show -no-color ${*}) + showOutput=$(${tfBinary} show -no-color ${*} 2>&1) showExitCode=${?} showCommentStatus="Failed" @@ -12,6 +11,7 @@ function terragruntShow() { if [ ${showExitCode} -eq 0 ]; then echo "show: info: successfully showed Terragrunt configuration in ${tfWorkingDir}" echo "${showOutput}" + echo ${showOutput} >plan.json echo exit ${showExitCode} fi From f0139c85ad06b2477b7183c1ce572f40364c92e8 Mon Sep 17 00:00:00 2001 From: David Gethings Date: Mon, 1 Feb 2021 11:36:02 +0000 Subject: [PATCH 09/12] don't redirect stderr to stdout --- src/terragrunt_show.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index 13cbc454..789f7a1a 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -3,7 +3,7 @@ function terragruntShow() { # Gather the output of `terragrunt plan`. echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}" - showOutput=$(${tfBinary} show -no-color ${*} 2>&1) + showOutput=$(${tfBinary} show -no-color ${*}) showExitCode=${?} showCommentStatus="Failed" From d5b8db4951ff0ce2d4813d5a07ff8ed936d7897e Mon Sep 17 00:00:00 2001 From: David Gethings Date: Mon, 1 Feb 2021 11:46:05 +0000 Subject: [PATCH 10/12] add tf_actions_show_file param --- action.yml | 3 +++ src/main.sh | 2 ++ src/terragrunt_show.sh | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index e94aec89..c5743494 100644 --- a/action.yml +++ b/action.yml @@ -33,6 +33,9 @@ inputs: tf_actions_fmt_write: description: "Write Terragrunt fmt changes to source files." default: false + tf_actions_show_file: + description: "File name to write the output of show subcommand" + default: "plan.json" outputs: tf_actions_output: description: "The Terragrunt outputs in JSON format." diff --git a/src/main.sh b/src/main.sh index 2b5bab3a..e1f6800a 100755 --- a/src/main.sh +++ b/src/main.sh @@ -69,6 +69,8 @@ function parseInputs() { tfFmtWrite=1 fi + tfShowFile="${INPUT_TF_ACTIONS_SHOW_FILE}:-plan.json" + tfWorkspace="default" if [ -n "${TF_WORKSPACE}" ]; then tfWorkspace="${TF_WORKSPACE}" diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index 789f7a1a..ad6f42c7 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -11,7 +11,7 @@ function terragruntShow() { if [ ${showExitCode} -eq 0 ]; then echo "show: info: successfully showed Terragrunt configuration in ${tfWorkingDir}" echo "${showOutput}" - echo ${showOutput} >plan.json + echo ${showOutput} >${tfShowFile} echo exit ${showExitCode} fi From 96bac77a9ae13bc8edce9d972731ec58950d7e80 Mon Sep 17 00:00:00 2001 From: David Gethings Date: Mon, 1 Feb 2021 11:53:15 +0000 Subject: [PATCH 11/12] add info output --- src/terragrunt_show.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/terragrunt_show.sh b/src/terragrunt_show.sh index ad6f42c7..ec50a4e7 100755 --- a/src/terragrunt_show.sh +++ b/src/terragrunt_show.sh @@ -10,7 +10,8 @@ function terragruntShow() { # Exit code of 0 indicates success with no changes. Print the output and exit. if [ ${showExitCode} -eq 0 ]; then echo "show: info: successfully showed Terragrunt configuration in ${tfWorkingDir}" - echo "${showOutput}" + # echo "${showOutput}" + echo "show: info: saving data to ${tfShowFile}" echo ${showOutput} >${tfShowFile} echo exit ${showExitCode} From 412d33ef94d0fd979e5197137559169d27eae5cc Mon Sep 17 00:00:00 2001 From: David Gethings Date: Mon, 1 Feb 2021 11:55:58 +0000 Subject: [PATCH 12/12] fix typo --- src/main.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.sh b/src/main.sh index e1f6800a..818e73b8 100755 --- a/src/main.sh +++ b/src/main.sh @@ -69,7 +69,7 @@ function parseInputs() { tfFmtWrite=1 fi - tfShowFile="${INPUT_TF_ACTIONS_SHOW_FILE}:-plan.json" + tfShowFile="${INPUT_TF_ACTIONS_SHOW_FILE:-plan.json}" tfWorkspace="default" if [ -n "${TF_WORKSPACE}" ]; then