Skip to content

Commit 27fd5b5

Browse files
jvassboryancbutlersudomateo
authored
Add option for terraform fmt to write changes to source files (hashicorp#138)
* Add option to write formatting changes * Add output whether formatting was written to source * Update terraform_fmt.sh * using variable for recursive * Fix typo Co-Authored-By: Matthew Sanabria <[email protected]> * Improve logic check Co-Authored-By: Matthew Sanabria <[email protected]> * Use zero to represent false Co-Authored-By: Matthew Sanabria <[email protected]> * Use one to represent true Co-Authored-By: Matthew Sanabria <[email protected]> * Update logic check to use one Co-Authored-By: Matthew Sanabria <[email protected]> * Add missing spacing Co-Authored-By: Matthew Sanabria <[email protected]> Co-authored-by: Ryan Butler <[email protected]> Co-authored-by: Matthew Sanabria <[email protected]>
1 parent 875f1da commit 27fd5b5

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Inputs configure Terraform GitHub Actions to perform different actions.
7575
* `tf_actions_cli_credentials_token` - (Optional) Token for the CLI credentials file.
7676
* `tf_actions_comment` - (Optional) Whether or not to comment on GitHub pull requests. Defaults to `true`.
7777
* `tf_actions_working_dir` - (Optional) The working directory to change into before executing Terraform subcommands. Defaults to `.` which means use the root of the GitHub repository.
78+
* `tf_actions_fmt_write` - (Optional) Whether or not to write `fmt` changes to source files. Defaults to `false`.
7879

7980
## Outputs
8081

@@ -83,6 +84,7 @@ Outputs are used to pass information to subsequent GitHub Actions steps.
8384
* `tf_actions_output` - The Terraform outputs in (stringified) JSON format.
8485
* `tf_actions_plan_has_changes` - `'true'` if the Terraform plan contained changes, otherwise `'false'`.
8586
* `tf_actions_plan_output` - The Terraform plan output.
87+
* `tf_actions_fmt_written` - Whether or not the Terraform formatting from `fmt` was written to source files.
8688

8789
## Secrets
8890

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ inputs:
2222
tf_actions_working_dir:
2323
description: 'Terraform working directory.'
2424
default: '.'
25+
tf_actions_fmt_write:
26+
description: 'Write Terraform fmt changes to source files.'
27+
default: false
2528
outputs:
2629
tf_actions_output:
2730
description: 'The Terraform outputs in JSON format.'
2831
tf_actions_plan_has_changes:
2932
description: 'Whether or not the Terraform plan contained changes.'
3033
tf_actions_plan_output:
3134
description: 'The Terraform plan output.'
35+
tf_actions_fmt_written:
36+
description: 'Whether or not the Terraform formatting was written to source files.'
3237
runs:
3338
using: 'docker'
3439
image: './Dockerfile'

src/main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ function parseInputs {
5252
tfCLICredentialsToken=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}
5353
fi
5454

55+
tfFmtWrite=0
56+
if [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "1" ] || [ "${INPUT_TF_ACTIONS_FMT_WRITE}" == "true" ]; then
57+
tfFmtWrite=1
58+
fi
59+
5560
tfWorkspace="default"
5661
if [ -n "${TF_WORKSPACE}" ]; then
5762
tfWorkspace="${TF_WORKSPACE}"

src/terraform_fmt.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,14 @@ ${fmtComment}
6666
echo "${fmtPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${fmtCommentsURL}" > /dev/null
6767
fi
6868

69+
# Write changes to branch
70+
echo "::set-output name=tf_actions_fmt_written::false"
71+
if [ "${tfFmtWrite}" == "1" ]; then
72+
echo "fmt: info: Terraform files in ${tfWorkingDir} will be formatted"
73+
terraform fmt -write=true ${fmtRecursive} "${*}"
74+
fmtExitCode=${?}
75+
echo "::set-output name=tf_actions_fmt_written::true"
76+
fi
77+
6978
exit ${fmtExitCode}
7079
}

0 commit comments

Comments
 (0)