Skip to content

chore(backend): define infrastructure using iac #12

chore(backend): define infrastructure using iac

chore(backend): define infrastructure using iac #12

Workflow file for this run

name: Infrastructure Plan
on:
pull_request:
paths:
- '_infra/credentials/**/*.tf'
- '_infra/global/**/*.tf'
- 'apps/backend/_infra/prod/storage/**/*.tf'
- 'apps/backend/_infra/prod/compute/**/*.tf'
env:
TF_API_TOKEN: "${{ secrets.TERRAFORM_API_TOKEN }}"
TF_CLOUD_ORGANIZATION: ${{ secrets.TERRAFORM_CLOUD_ORGANIZATION }}
TF_VAR_organization: "\"${{ secrets.TERRAFORM_CLOUD_ORGANIZATION }}\""
jobs:
terraform-plan:
runs-on: ubuntu-latest
strategy:
matrix:
directory:
- 'credentials'
- 'global'
- 'backend-storage'
- 'backend-compute'
outputs:
infraChanged: ${{ steps.infra-changed.outputs.any_changed }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set Terraform workspace configuration
run: |
if [[ "${{ matrix.directory }}" == "credentials" ]]; then
echo "INFRA_DIR=_infra/credentials" >> $GITHUB_ENV
echo "TF_WORKSPACE=snipcode-credentials" >> $GITHUB_ENV
elif [[ "${{ matrix.directory }}" == "global" ]]; then
echo "INFRA_DIR=_infra/global" >> $GITHUB_ENV
echo "TF_WORKSPACE=snipcode-global" >> $GITHUB_ENV
elif [[ "${{ matrix.directory }}" == "backend-storage" ]]; then
echo "INFRA_DIR=apps/backend/_infra/prod/storage" >> $GITHUB_ENV
echo "TF_WORKSPACE=snipcode-backend-storage-prod" >> $GITHUB_ENV
elif [[ "${{ matrix.directory }}" == "backend-compute" ]]; then
echo "INFRA_DIR=apps/backend/_infra/prod/compute" >> $GITHUB_ENV
echo "TF_WORKSPACE=snipcode-backend-compute-prod" >> $GITHUB_ENV
fi
- name: Check if infra files changed
id: infra-changed
uses: tj-actions/changed-files@v44
with:
files: |
${{ env.INFRA_DIR }}/**/*.tf
- name: Setup Terraform
if: ${{ steps.infra-changed.outputs.any_changed == 'true' }}
uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.9.2
- name: Upload Configuration
if: ${{ steps.infra-changed.outputs.any_changed == 'true' }}
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: plan-upload
with:
workspace: ${{ env.TF_WORKSPACE }}
directory: ${{ matrix.directory }}
speculative: true
- name: Create Plan Run
if: ${{ steps.infra-changed.outputs.any_changed == 'true' }}
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: plan-run
with:
workspace: ${{ env.TF_WORKSPACE }}
configuration_version: ${{ steps.plan-upload.outputs.configuration_version_id }}
plan_only: true
- name: Get Plan Output
if: ${{ steps.infra-changed.outputs.any_changed == 'true' }}
uses: hashicorp/tfc-workflows-github/actions/[email protected]
id: plan-output
with:
plan: ${{ fromJSON(steps.plan-run.outputs.payload).data.relationships.plan.data.id }}
- name: Update Pull Request
if: ${{ steps.infra-changed.outputs.any_changed == 'true' }}
uses: actions/github-script@v6
id: plan-comment
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
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('HCP Terraform Plan Output')
});
const output = `#### ${{ matrix.directory }} - HCP Terraform Plan Output
\`\`\`
Plan: ${{ steps.plan-output.outputs.add }} to add, ${{ steps.plan-output.outputs.change }} to change, ${{ steps.plan-output.outputs.destroy }} to destroy.
\`\`\`
[View changes in detail](${{ steps.plan-run.outputs.run_link }})
`;
if (botComment) {
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
});
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
});