Home

Awesome

Terraform Plan GitHub Action

Runs terraform plan on your project and posts a comment with the changes on a Pull Request (PR). It runs the following commands:

terraform init
terraform validate
terraform fmt --check
terraform plan -out=plan.tfplan
terraform show -json plan.tfplan 

This action does not install Terraform or Terragrunt, but can be installed using Terraform tools setup action. You can see how it's used in the pr-test.yaml workflow.

Settings

Use the following to control the action:

SettingDescriptionDefault
allow-failureAllow the action to failfalse
commentAdd comment with changes to the PRtrue
comment-deleteDelete previous comments made by the bot on the PRfalse
comment-titleThe title to give the PR commentPlan changes
conftest-character-limitCharacter limit for Conftest output2000
conftest-checksLocation of custom conftest check definitionsgit::https://github.com/cds-snc/opa_checks.git//aws_terraform
directoryDirectory with the *.tf files to validate.
github-tokenGitHub Token used to add comment to PR (required to add comments).
plan-character-limitCharacter limit for Terraform plan output30000
terraform-initCustom Terraform init args
terragruntUse Terragrunt instead of Terraformfalse
skip-conftestSkip the Conftest stepfalse
skip-fmtSkip the Terraform format checkfalse
skip-planSkip the Terraform plan for projects without a remote statefalse
init-run-allRun init across all modules (only applicable for terragrunt).false

Examples

# Setup Terraform, Terragrunt, and Conftest
- name: Setup terraform tools
  uses: cds-snc/terraform-tools-setup@v1

# Run Terraform plan and add a comment with changes on the PR
- name: Terraform plan
  uses: cds-snc/terraform-plan
  with:
    github-token: ${{ secrets.GITHUB_TOKEN }}

# Use Terragrunt, allow failure and set a custom PR comment title
- name: Terraform plan
  uses: cds-snc/terraform-plan
  with:
    allow-failure: true
    comment-title: Custom comment title
    github-token: ${{ secrets.GITHUB_TOKEN }}
    terragrunt: true

# Run on a sub project folder, deleting previous PR comments made by the action
- name: Terraform plan
  uses: cds-snc/terraform-plan
  with:
    directory: ./infra
    comment-delete: true
    github-token: ${{ secrets.GITHUB_TOKEN }}

# Run Terraform plan with no PR comment
# Plan will still availabe in the workflow logs
- name: Terraform plan
  uses: cds-snc/terraform-plan
  with:
    add-comment: false

# Run Terraform plan custom Terraform init args
- name: Terraform plan
  uses: cds-snc/terraform-plan
  with:
    github-token: ${{ secrets.GITHUB_TOKEN }}
    terraform-init: |
      -backend-config="bucket=your-state-bucket-name"
      -backend-config="region=ca-central-1"

Contributing

To setup your local dev environment:

npm install
npm run prepare

Husky provides a pre-commit hook that builds the dist/index.js used by the action. To test locally, nektos/act works well.

Policy

Open Policy Agent is used to check the terraform plan for changes. Policies are written in Rego and then compiled into a WebAssembly module using npm run policy.