Skip to content

Instantly share code, notes, and snippets.

@timvw
Created January 20, 2021 05:54
Show Gist options
  • Save timvw/7a245947a9b3b027d5a0fcd5ad3d9977 to your computer and use it in GitHub Desktop.
Save timvw/7a245947a9b3b027d5a0fcd5ad3d9977 to your computer and use it in GitHub Desktop.
github actions to plan/apply multiple terraform projects
name: "Terraform"
on:
push:
branches:
- master
pull_request:
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
env:
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
strategy:
matrix:
folder: ['01-demo', '02-demo']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
- name: Terraform Init
id: init
run: terraform -chdir=${{ matrix.folder }} init
- name: Terraform Plan
id: plan
if: github.event_name == 'pull_request'
run: terraform -chdir=${{ matrix.folder }} plan -no-color
continue-on-error: true
- name: Add PR comment
uses: peter-evans/create-or-update-comment@v1
if: github.event_name == 'pull_request'
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
#### Directory: \`${{ matrix.folder }}\`
#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖${{ steps.validate.outputs.stdout }}
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`${{ steps.plan.outputs.stdout }}\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
id: apply
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
run: terraform -chdir=${{ matrix.folder }} apply -auto-approve
- name: Add Merge comment
uses: peter-evans/commit-comment@v1
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
with:
body: |
#### Directory: \`${{ matrix.folder }}\`
#### Terraform Apply 📖\`${{ steps.apply.outputs.outcome }}\`
<details><summary>Show Apply</summary>
\`\`\`${{ steps.apply.outputs.stdout }}\`\`\`
</details>
*Merger: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment