chore(backend): define infrastructure using iac #10
Workflow file for this run
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: 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 }}\"" | |
TF_WORKSPACE: 'snipcode-backend-storage-prod' | |
jobs: | |
terraform-plan: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
directory: | |
#- '_infra/credentials' | |
#- '_infra/global' | |
#- 'apps/backend/_infra/prod/storage' | |
#- 'apps/backend/_infra/prod/compute' | |
- 'packages/utils' | |
outputs: | |
infraChanged: ${{ steps.infra-changed.outputs.any_changed }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Check if infra files changed | |
id: infra-changed | |
uses: tj-actions/changed-files@v44 | |
with: | |
files: | | |
${{ matrix.directory }}/**/*.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: ${{ success() }} | |
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 | |
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 | |
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 | |
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 = `#### 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 | |
}); |