Production deployment #176
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 plan | |
on: | |
pull_request: | |
branches: | |
- main | |
- develop | |
paths: | |
- .github/workflows/terraform-plan.yml | |
- .github/workflows/build-email-function.yml | |
- terraform/** | |
- email-function/** | |
- emails/** | |
- cloudformation/** | |
jobs: | |
build_email_function: | |
uses: ./.github/workflows/build-email-function.yml | |
plan: | |
runs-on: ubuntu-22.04 | |
# Related | |
# - https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs | |
# - https://docs.github.com/en/rest/authentication/permissions-required-for-github-apps | |
permissions: | |
id-token: write # Required for aws-actions/configure-aws-credentials | |
contents: read # Required for aws-actions/configure-aws-credentials | |
pull-requests: write # Required for actions/github-script | |
needs: [build_email_function] | |
steps: | |
- uses: actions/checkout@v4.1.1 | |
- uses: hashicorp/setup-terraform@v3.0.0 | |
with: | |
terraform_version: 1.6.6 | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4.0.1 | |
with: | |
role-to-assume: arn:aws:iam::220746603587:role/github-actions | |
aws-region: eu-central-1 | |
- name: Retrieve email-function artifact | |
uses: actions/download-artifact@v4.1.1 | |
with: | |
name: email-function-dist | |
path: email-function/dist | |
- name: terraform init | |
run: terraform init -backend-config=test.s3.tfbackend | |
working-directory: terraform | |
- name: terraform plan | |
run: terraform plan -no-color | |
working-directory: terraform | |
id: plan | |
continue-on-error: true | |
- uses: actions/github-script@v7.0.1 | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: ${{ 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('Terraform Plan') | |
}) | |
// 2. Prepare format of the comment | |
const output = `#### Terraform Plan 📖 \`${{ steps.plan.outcome }}\` | |
<details><summary>Show Plan</summary> | |
\`\`\`\n | |
${process.env.PLAN} | |
\`\`\` | |
</details>` | |
// 3. If we have a comment, update it, otherwise create a new one | |
if (botComment) { | |
github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: botComment.id, | |
body: output | |
}) | |
} else { | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
} |