Skip to content

Commit

Permalink
feat: added support for a custom message to the terraform action outp…
Browse files Browse the repository at this point in the history
…ut (#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
  • Loading branch information
amartin-isp authored Oct 15, 2024
1 parent 804b11e commit 3a6565d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions terraform-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/
```

Expand Down
6 changes: 5 additions & 1 deletion terraform-actions/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}"
1 change: 1 addition & 0 deletions terraform-actions/fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function terraformFmt {

if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${commentStatus}" == "Failed" ]; then
commentWrapper="#### \`terraform fmt\` ${commentStatus} for \`${workingDir}\`
${header_message}
<details><summary>Show Output</summary>
\`\`\`diff
Expand Down
1 change: 1 addition & 0 deletions terraform-actions/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function terraformInit {

if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${commentStatus}" == "Failed" ]; then
commentWrapper="#### \`terraform init\` ${commentStatus} for \`${workingDir}\`
${header_message}
<details><summary>Show Output</summary>
\`\`\`
Expand Down
5 changes: 3 additions & 2 deletions terraform-actions/main.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/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

# This will reduce certain non-actionable command output
# 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"
Expand Down
1 change: 1 addition & 0 deletions terraform-actions/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
<details><summary>Show Output</summary>
\`\`\`diff
Expand Down
1 change: 1 addition & 0 deletions terraform-actions/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function terraformValidate {

if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${commentStatus}" == "Failed" ]; then
commentWrapper="#### \`terraform validate\` ${commentStatus} for \`${workingDir}\`
${header_message}
<details><summary>Show Output</summary>
\`\`\`
Expand Down

0 comments on commit 3a6565d

Please sign in to comment.