Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

chore: Rename parameters where required #46

chore: Rename parameters where required

chore: Rename parameters where required #46

name: 'Terraform'
on:
pull_request:
paths:
- 'infrastructure/**'
- '.github/workflows/terraform**'
branches:
- main
permissions:
contents: read
pull-requests: write
#concurrency, we should only ever run this once across the entire repository
concurrency:
group: terraform
cancel-in-progress: true
jobs:
terraform-plan:
name: 'Terraform Plan'
runs-on: ubuntu-latest
env:
TF_VAR_twilio_account_sid: ${{ secrets.TWILIO_ACCOUNT_SID }}
TF_VAR_twilio_auth_token: ${{ secrets.TWILIO_AUTH_TOKEN}}
environment: terraform-plan
# Use the Bash shell regardless of the GitHub Actions runner's OS
defaults:
run:
shell: bash
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v3
# Install the latest version of Terraform CLI
- name: Install Terraform
uses: hashicorp/[email protected]
# Configure AWS CLI (required for S3 backend)
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: 'ap-southeast-2'
# Checks that all Terraform configuration files adhere to a canonical format
- name: Terraform Format
run: terraform -chdir=infrastructure/terraform fmt -check=true -diff=true -recursive
# Configure Terraform backend with S3
- name: Configure Terraform Backend
run: |
terraform -chdir=infrastructure/terraform init -input=false -backend-config="s3.tfvars"
# Checks that all Terraform configuration files adhere to a canonical format
- name: Terraform Validate
id: validate
run: terraform validate -no-color
# Generates an execution plan for Terraform
- name: Terraform Plan
id: plan
run: |
terraform -chdir=infrastructure/terraform plan -input=false -out=tfplan
terraform -chdir=infrastructure/terraform show -no-color tfplan > plan.txt
continue-on-error: true
- name: Upload Terraform Plan
uses: actions/upload-artifact@v2
if: steps.plan.outcome == 'success'
with:
name: terraform-plan
path: plan.txt
- name: Mark Previous Plan Comments as Outdated
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issue_number = context.issue.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
// List comments
const comments = await github.rest.issues.listComments({
owner,
repo,
issue_number
});
// Loop through the comments
for (const comment of comments.data) {
// Check if the comment is from github-actions[bot] and contains the Terraform plan text
if (comment.user.login === 'github-actions[bot]' && comment.body.includes('Terraform Plan 📖')) {
// Delete the comment
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id
});
}
}
- uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Comment with \`apply\` to run Terraform apply*
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})