Merge pull request #20 from Coalfire-CF/tfv #10
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
name: Terraform Validate | |
on: | |
push: | |
branches: | |
- '**' | |
pull_request: | |
branches: | |
- '**' | |
workflow_call: | |
workflow_dispatch: | |
inputs: | |
terraform_version: | |
description: 'The version of Terraform to use' | |
required: true | |
default: '1.9.0' | |
type: string | |
jobs: | |
verify: | |
name: Validate Terraform Configuration | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: ${{ inputs.terraform_version || '1.9.0' }} | |
- name: Configure Git for private modules | |
env: | |
GH_TOKEN: ${{ secrets.ORG_GITHUB_PAT }} | |
run: | | |
git config --global url."https://${GH_TOKEN}@github.com/".insteadOf "https://github.com/" | |
- name: Initialise with no backend | |
run: terraform init -backend=false | |
- name: Validate Terraform | |
id: validate | |
run: | | |
set +e | |
OUTPUT=$(terraform validate) | |
CLEAN_OUTPUT=$(echo "$OUTPUT" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g") | |
echo "$CLEAN_OUTPUT" | |
echo "result=$CLEAN_OUTPUT" >> $GITHUB_OUTPUT | |
set -e | |
continue-on-error: true | |
- name: Create comment | |
if: github.event_name == 'pull_request' | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const issue_number = context.issue.number; | |
const result = `${{ steps.validate.outputs.result }}`; | |
github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
body: `Terraform validation output:\n\`\`\`\n${result}\n\`\`\`` | |
}); |