Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for terragrunt show #4

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
name: 'Terragrunt GitHub Actions'
description: 'Runs Terragrunt commands via GitHub Actions.'
author: 'HashiCorp, Inc. Terraform Team <terraform@hashicorp.com>'
name: "Terragrunt GitHub Actions"
description: "Runs Terragrunt commands via GitHub Actions."
author: "HashiCorp, Inc. Terraform Team <terraform@hashicorp.com>"
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
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.'
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"
119 changes: 63 additions & 56 deletions src/main.sh
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -69,23 +69,25 @@ function parseInputs {
tfFmtWrite=1
fi

tfShowFile="${INPUT_TF_ACTIONS_SHOW_FILE:-plan.json}"

tfWorkspace="default"
if [ -n "${TF_WORKSPACE}" ]; then
tfWorkspace="${TF_WORKSPACE}"
fi
}

function configureCLICredentials {
function configureCLICredentials() {
if [[ ! -f "${HOME}/.terraformrc" ]] && [[ "${tfCLICredentialsToken}" != "" ]]; then
cat > ${HOME}/.terraformrc << EOF
cat >${HOME}/.terraformrc <<EOF
credentials "${tfCLICredentialsHostname}" {
token = "${tfCLICredentialsToken}"
}
EOF
fi
}

function installTerraform {
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)
Expand All @@ -107,15 +109,15 @@ function installTerraform {
echo "Successfully downloaded Terraform v${tfVersion}"

echo "Unzipping Terraform v${tfVersion}"
unzip -d /usr/local/bin /tmp/terraform_${tfVersion} &> /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
fi
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)
Expand All @@ -139,15 +141,15 @@ 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
fi
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
Expand All @@ -159,53 +161,58 @@ function main {
source ${scriptDir}/terragrunt_import.sh
source ${scriptDir}/terragrunt_taint.sh
source ${scriptDir}/terragrunt_destroy.sh
source ${scriptDir}/terragrunt_show.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
;;
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
}

Expand Down
55 changes: 55 additions & 0 deletions src/terragrunt_show.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

function terragruntShow() {
# Gather the output of `terragrunt plan`.
echo "show: info: showing Terragrunt configuration in ${tfWorkingDir}"
showOutput=$(${tfBinary} show -no-color ${*})
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 "show: info: saving data to ${tfShowFile}"
echo ${showOutput} >${tfShowFile}
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}
<details><summary>Show Output</summary>

\`\`\`
${showOutput}
\`\`\`

</details>

*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}
}