Skip to content

Instantly share code, notes, and snippets.

@ewhauser
Created March 23, 2021 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewhauser/0f44da4a904437ecb8a077c9d27d75d1 to your computer and use it in GitHub Desktop.
Save ewhauser/0f44da4a904437ecb8a077c9d27d75d1 to your computer and use it in GitHub Desktop.
name: "Terraform"
on:
push:
branches:
- main
paths:
- terraform/**
pull_request:
branches:
- main
paths:
- terraform/**
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
env:
tf_actions_working_dir: './terraform'
defaults:
run:
working-directory: ${{ env.tf_actions_working_dir }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set Workspace
id: set_workspace
run: |
prod="${{ github.event.pull_request.base.ref == 'tf_prod' || github.ref == 'refs/head/tf_prod' }}"
if [ "$prod" = true ]; then
echo "::set-output name=environment::prod"
else
echo "::set-output name=environment::dev"
fi
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
# terraform_version: 0.13.0:
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: Terraform Format
id: fmt
run: terraform fmt -check
- name: Terraform Init
id: init
run: terraform init
env:
TF_WORKSPACE: ${{ steps.set_workspace.outputs.environment }}
- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: terraform plan -no-color
continue-on-error: true
env:
TF_WORKSPACE: ${{ steps.set_workspace.outputs.environment }}
- uses: actions/github-script@0.9.0
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 }}\`
<details><summary>Show Plan</summary>
\`\`\`${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
- name: Terraform Apply
if: (github.ref == 'refs/heads/main' || github.ref == 'refs/head/tf_prod') && github.event_name == 'push'
run: terraform apply -auto-approve
env:
TF_WORKSPACE: ${{ steps.set_workspace.outputs.environment }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment