This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
feat: Terraforming Twillio #10
This file contains 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: 'Terraform Apply' | |
on: | |
issue_comment: | |
types: [created] | |
permissions: | |
contents: read | |
pull-requests: write | |
concurrency: | |
group: terraform | |
cancel-in-progress: true | |
jobs: | |
terraform-apply: | |
if: github.event.issue.pull_request != null && contains(github.event.comment.body, 'apply') | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
# 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' | |
# Post a running comment when Terraform apply starts | |
- name: Post Running Comment | |
if: startsWith(github.event.comment.body, 'apply') | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const issue_number = context.payload.issue.number; | |
github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
body: ':hourglass_flowing_sand: Terraform apply is running...' | |
}); | |
# Configure Terraform backend with S3 | |
- name: Configure Terraform Backend | |
run: | | |
terraform -chdir=infrastructure/terraform init -input=false -backend-config="s3.tfvars" | |
# Generates an execution plan for Terraform | |
- name: Terraform Plan | |
id: plan | |
run: | | |
terraform -chdir=infrastructure/terraform plan -input=false -out=tfplan | |
# Apply the changes required to reach the desired state of the configuration | |
- name: Terraform Apply | |
run: terraform -chdir=infrastructure/terraform apply -auto-approve -input=false tfplan | |
# Post a success comment | |
- name: Post Success Comment | |
if: success() | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const issue_number = context.payload.issue.number; | |
github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
body: ':rocket: Terraform apply successful! Attempting to merge pull request...' | |
}); | |
# Auto-apply "automerge" label to pull request | |
- name: Auto-Label Pull Request | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const issue_number = context.payload.pull_request.number; | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue_number, | |
labels: ['automerge'] | |
}); |