FMT #9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Copyright (c) Microsoft Corporation | |
# Licensed under the MIT License. | |
# | |
name: PR Fmt | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened] | |
env: | |
TF_VERSION: "1.8.4" | |
jobs: | |
fmt_check: | |
name: Terraform formatting Checks | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: latest | |
- name: Check Terraform formatting | |
id: fmt | |
run: | | |
FMT_OUTPUT=$(terraform fmt -check -recursive | base64) | |
echo "fmt_output=${FMT_OUTPUT}" >> $GITHUB_OUTPUT | |
if [ -n "$FMT_OUTPUT" ]; then | |
echo "Formatting issues found in the following files:" | |
echo "$FMT_OUTPUT" | |
exit 1 | |
fi | |
- name: Post PR comment on fmt failure | |
if: failure() && steps.fmt.outcome == 'failure' | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const base64FmtOutput = `${{ steps.fmt.outputs.fmt_output }}`; | |
const fmtOutput = Buffer.from(base64FmtOutput, 'base64').toString('utf-8'); | |
const issue_number = context.issue.number; | |
const owner = context.repo.owner; | |
const repo = context.repo.repo; | |
let body = `Please run \`terraform fmt\` to format your Terraform files according to the standard conventions. The following files need formatting:\n\`\`\`\n${fmtOutput}\n\`\`\``; | |
github.rest.pulls.createReview({ | |
owner, | |
repo, | |
pull_number: issue_number, | |
body, | |
event: "COMMENT", | |
}); |