Skip to content

feat: add terraform reusable workflow #12

feat: add terraform reusable workflow

feat: add terraform reusable workflow #12

Workflow file for this run

---
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_call:
inputs:
aws_region:
type: string
required: true
aws_endpoint_url_s3:
type: string
required: true
gh_owner:
type: string
required: true
gh_app_id:
type: string
required: true
gh_app_installation_id:
type: string
required: true
path:
type: string
required: true
secrets:
aws_access_key_id:
required: true
aws_secret_access_key:
required: true
gh_app_pem_file:
required: true
env:
AWS_REGION: ${{ inputs.aws_region || vars.AWS_REGION }}
AWS_ENDPOINT_URL_S3: ${{ inputs.aws_endpoint_url_s3 || vars.AWS_ENDPOINT_URL_S3 }}
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id || secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key || secrets.AWS_SECRET_ACCESS_KEY }}
GITHUB_OWNER: ${{ inputs.gh_owner || vars.OWNER }}
GITHUB_APP_ID: ${{ inputs.gh_app_id || vars.APP_ID }}
GITHUB_APP_INSTALLATION_ID: ${{ inputs.gh_app_installation_id || vars.APP_INSTALLATION_ID }}
GITHUB_APP_PEM_FILE: ${{ secrets.gh_app_pem_file || secrets.APP_PEM_FILE }}
TF_WORKSPACE: ${{ inputs.gh_owner || vars.OWNER }}
TF_VAR_path: ${{ inputs.path || 'test.yaml' }}
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
defaults:
run:
shell: bash
steps:
- name: Checkout the repository to the runner
uses: actions/checkout@v4
- name: Setup Terraform with specified version on the runner
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.11.0
- name: Terraform init
id: init
run: terraform init
- name: Terraform plan
id: plan
if: github.event_name == 'pull_request'
run: terraform plan -no-color -input=false
continue-on-error: true
- uses: actions/github-script@v7
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('<!-- GitHub Actions Terraform PR comment bot -->')
});
// 2. Put together bot new comment contents for the PR
const output = `<!-- GitHub Actions Terraform PR comment bot -->
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
// 3. Delete previous comment for the PR
if (botComment) {
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
});
}
// 4. Create a new comment
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: output
});
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
- name: Terraform Apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve -input=false