Skip to content

Commit

Permalink
chore: add a workflow to check terraform fmt and show changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sorccu committed Nov 26, 2024
1 parent 4885863 commit f4f72c4
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/terraform-format-and-style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Terraform Format and Style
on:
pull_request:
paths:
- '**.tf'
- '**.tfvars'
- '**.tftest.hcl'

jobs:
check:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ env.tf_actions_working_dir }}
permissions:
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3

- name: terraform fmt
id: fmt
run: terraform fmt -check -recursive -diff
continue-on-error: true

- uses: actions/github-script@v7
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Format and Style')
})
// 2. Prepare format of the comment
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
<details><summary>Format Output</summary>
\`\`\`\n
${{ steps.fmt.outputs.stdout }}
\`\`\`
</details>`;
// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}

0 comments on commit f4f72c4

Please sign in to comment.