Skip to content

Instantly share code, notes, and snippets.

@varunchandak
Created January 26, 2022 05:41
Show Gist options
  • Save varunchandak/73175fbe550f4ea4d529a94626cadfca to your computer and use it in GitHub Desktop.
Save varunchandak/73175fbe550f4ea4d529a94626cadfca to your computer and use it in GitHub Desktop.
terraform validate and plan github action
---
name: "Validate Terraform files"
on:
push:
branches:
- main
pull_request:
jobs:
terraform:
name: Validate Terraform files
runs-on: self-hosted
defaults:
run:
working-directory: example
steps:
- name: Checkout Repository
uses: actions/checkout@v2
with:
fetch-depth: 1
- name: Setup Terraform
uses: hashicorp/[email protected]
- name: Run `terraform fmt`
id: fmt
run: terraform fmt -diff -check -no-color -recursive
- name: Run `terraform init`
id: init
run: terraform init
- name: Run `terraform validate`
id: validate
if: github.event_name == 'pull_request'
run: terraform validate -no-color
- name: Run `terraform plan`
id: validate
if: github.event_name == 'pull_request'
run: terraform plan -no-color -out=terraform-plan.json
continue-on-error: false
- name: Update Pull Request
uses: actions/[email protected]
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Format and Style πŸ–Œ\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization βš™οΈ\`${{ steps.init.outcome }}\`
#### Terraform Plan πŸ“–\`${{ steps.plan.outcome }}\`
#### Terraform Validation πŸ€–\`${{ steps.validate.outputs.stdout }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment