Created
January 26, 2022 05:41
-
-
Save varunchandak/73175fbe550f4ea4d529a94626cadfca to your computer and use it in GitHub Desktop.
terraform validate and plan github action
This file contains hidden or 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: "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