From 3a6565d807a0bf7c8db536f232779855b7f0d055 Mon Sep 17 00:00:00 2001 From: Adrian Martin <89433968+amartin-isp@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:18:34 -0400 Subject: [PATCH] feat: added support for a custom message to the terraform action output (#439) * feat: added support for a custom message to the terraform action output * fix typo * description and usage * fix variable assignment * fix variable assignment * test * re-ordering * re-ordering * clean up tests * cleanup --- terraform-actions/README.md | 5 +++++ terraform-actions/action.yml | 6 +++++- terraform-actions/fmt.sh | 1 + terraform-actions/init.sh | 1 + terraform-actions/main.sh | 5 +++-- terraform-actions/plan.sh | 1 + terraform-actions/validate.sh | 1 + 7 files changed, 17 insertions(+), 3 deletions(-) diff --git a/terraform-actions/README.md b/terraform-actions/README.md index 2631e5c..e746d73 100644 --- a/terraform-actions/README.md +++ b/terraform-actions/README.md @@ -8,6 +8,10 @@ inputs: command: description: "The terraform command to run" required: true + header_message: + description: "Message to add before the terraform output that is posted to a PR. (Must be a single line string)" + required: false + default: "" working_directory: description: "The directory to run all the commands in" required: false @@ -23,6 +27,7 @@ Example usage of `terraform plan`: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: command: plan + header_message: "*Note*: This is a message that appears before the terraform plan output" working_directory: terraform/ ``` diff --git a/terraform-actions/action.yml b/terraform-actions/action.yml index dc2b5ac..de58b9f 100644 --- a/terraform-actions/action.yml +++ b/terraform-actions/action.yml @@ -4,6 +4,10 @@ inputs: command: description: "The terraform command to run" required: true + header_message: + description: "Message to add before the terraform output that is posted to a PR. (Must be a single line string)" + required: false + default: "" working_directory: description: "The directory to run all the commands in" required: false @@ -22,4 +26,4 @@ runs: shell: bash id: main working-directory: ${{ inputs.working_directory }} - run: ${{ github.action_path }}/main.sh "${{ inputs.command }}" "${{ inputs.working_directory }}" + run: ${{ github.action_path }}/main.sh "${{ inputs.command }}" "${{ inputs.working_directory }}" "${{ inputs.header_message }}" diff --git a/terraform-actions/fmt.sh b/terraform-actions/fmt.sh index 3489848..34ec087 100644 --- a/terraform-actions/fmt.sh +++ b/terraform-actions/fmt.sh @@ -20,6 +20,7 @@ function terraformFmt { if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${commentStatus}" == "Failed" ]; then commentWrapper="#### \`terraform fmt\` ${commentStatus} for \`${workingDir}\` +${header_message}
Show Output \`\`\`diff diff --git a/terraform-actions/init.sh b/terraform-actions/init.sh index bd9295a..cba8c1f 100644 --- a/terraform-actions/init.sh +++ b/terraform-actions/init.sh @@ -20,6 +20,7 @@ function terraformInit { if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${commentStatus}" == "Failed" ]; then commentWrapper="#### \`terraform init\` ${commentStatus} for \`${workingDir}\` +${header_message}
Show Output \`\`\` diff --git a/terraform-actions/main.sh b/terraform-actions/main.sh index ad5ea50..bda816c 100755 --- a/terraform-actions/main.sh +++ b/terraform-actions/main.sh @@ -1,7 +1,7 @@ #!/bin/bash -if [ "$#" -ne 2 ]; then - echo 'Usage: ./main.sh $command $working_directory' +if [ "$#" -ne 3 ]; then + echo 'Usage: ./main.sh $command $working_directory $header_message' exit 1 fi @@ -9,6 +9,7 @@ fi # See: https://learn.hashicorp.com/tutorials/terraform/automate-terraform#controlling-terraform-output-in-automation export TF_IN_AUTOMATION=true +header_message="$3" workingDir="$2" function main { command="$1" diff --git a/terraform-actions/plan.sh b/terraform-actions/plan.sh index f02753d..d09b984 100644 --- a/terraform-actions/plan.sh +++ b/terraform-actions/plan.sh @@ -47,6 +47,7 @@ function terraformPlan { if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && ([ "${hasChanges}" == "true" ] || [ "${commentStatus}" == "Failed" ]); then commentWrapper="#### \`terraform plan\` ${commentStatus} for \`${workingDir}\` +${header_message}
Show Output \`\`\`diff diff --git a/terraform-actions/validate.sh b/terraform-actions/validate.sh index 7c12dc4..81416c4 100644 --- a/terraform-actions/validate.sh +++ b/terraform-actions/validate.sh @@ -20,6 +20,7 @@ function terraformValidate { if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${commentStatus}" == "Failed" ]; then commentWrapper="#### \`terraform validate\` ${commentStatus} for \`${workingDir}\` +${header_message}
Show Output \`\`\`