demo deployment example #264
Workflow file for this run
This file contains hidden or 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 Preview | |
on: | |
pull_request: | |
branches: | |
- main | |
jobs: | |
preview: | |
name: Plan Terraform changes in changed stacks | |
runs-on: ubuntu-latest | |
permissions: | |
id-token: write | |
contents: read | |
pull-requests: write | |
checks: read | |
steps: | |
### Create Pull Request comment | |
- name: Prepare pull request preview comment | |
if: github.event.pull_request | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
header: preview | |
message: | | |
## Preview of Terraform changes in ${{ github.event.pull_request.head.sha }} | |
:warning: preview is being created... please stand by! | |
### Check out the code | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.head_ref }} | |
fetch-depth: 0 | |
### Install tooling | |
- name: Install Terramate | |
uses: terramate-io/terramate-action@v2 | |
- name: Install asdf | |
uses: asdf-vm/actions/setup@v3 | |
- name: Install Terraform with asdf | |
run: | | |
asdf plugin add terraform | |
asdf install terraform | |
- name: Install OpenTofu with asdf | |
run: | | |
asdf plugin add opentofu | |
asdf install opentofu | |
### Linting | |
- name: Check Terramate formatting | |
run: terramate fmt --check | |
- name: Check Terraform formatting | |
run: terraform fmt -recursive -check -diff | |
### Check for changed stacks | |
- name: List changed stacks | |
id: list-changed | |
run: terramate list -C stacks --changed | |
### Configure cloud credentials | |
- name: Configure AWS credentials | |
if: steps.list-changed.outputs.stdout | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ${{ env.AWS_REGION }} | |
role-to-assume: arn:aws:iam::${{ env.AWS_ACCOUNT_ID }}:role/github | |
env: | |
AWS_REGION: us-east-1 | |
AWS_ACCOUNT_ID: ${{ vars.AWS_ACCOUNT_ID }} | |
- name: Verify AWS credentials | |
if: steps.list-changed.outputs.stdout | |
run: aws sts get-caller-identity | |
### Run the Terraform preview via Terramate in each changed stack | |
- name: Run Terraform init in all changed stacks | |
if: steps.list-changed.outputs.stdout | |
run: | | |
terramate script run \ | |
-C stacks \ | |
--changed \ | |
--parallel 1 \ | |
init | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
- name: Plan Terraform changes in changed stacks | |
if: steps.list-changed.outputs.stdout | |
run: | | |
terramate script run \ | |
-C stacks \ | |
--changed \ | |
--parallel 5 \ | |
--continue-on-error \ | |
-- \ | |
preview | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
### Update Pull Request comment | |
- name: Generate preview details | |
if: steps.list-changed.outputs.stdout | |
id: comment | |
run: | | |
echo >>pr-comment.txt "## Preview of Terraform changes in ${{ github.event.pull_request.head.sha }}" | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt '> [!TIP]' | |
echo >>pr-comment.txt '> [:mag: View all Preview Details on Terramate Cloud](https://cloud.terramate.io/o/terramate-demo/review-requests)' | |
echo >>pr-comment.txt | |
terramate script run --changed safe-guard >>pr-comment.txt | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt "### Changed Stacks" | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt '```bash' | |
echo >>pr-comment.txt "${{ steps.list-changed.outputs.stdout }}" | |
echo >>pr-comment.txt '```' | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt "#### Terraform Plans" | |
echo >>pr-comment.txt | |
terramate script run --changed -- terraform render | dd bs=1024 count=248 >>pr-comment.txt | |
[ "${PIPESTATUS[0]}" == "141" ] && sed -i 's/#### Terraform Plan/#### :warning: Terraform Plan truncated: please check console output :warning:/' pr-comment.txt | |
echo >>pr-comment.txt "#### OpenTofu Plans" | |
echo >>pr-comment.txt | |
terramate script run --changed -- tofu render | dd bs=1024 count=248 >>pr-comment.txt | |
[ "${PIPESTATUS[0]}" == "141" ] && sed -i 's/#### OpenTofu Plan/#### :warning: OpenTofu Plan truncated: please check console output :warning:/' pr-comment.txt | |
cat pr-comment.txt >>$GITHUB_STEP_SUMMARY | |
- name: Generate preview when no stacks changed | |
if: success() && !steps.list-changed.outputs.stdout | |
run: | | |
echo >>pr-comment.txt "## Preview of Terraform changes in ${{ github.event.pull_request.head.sha }}" | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt "### Changed Stacks" | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt 'No changed stacks, no detailed preview will be generated.' | |
cat pr-comment.txt >>$GITHUB_STEP_SUMMARY | |
- name: Generate preview when things failed | |
if: failure() | |
run: | | |
echo >>pr-comment.txt "## Preview of Terraform changes in ${{ github.event.pull_request.head.sha }}" | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt '> [!TIP]' | |
echo >>pr-comment.txt '> [:mag: View all Preview Details on Terramate Cloud](https://cloud.terramate.io/o/terramate-demo/review-requests)' | |
echo >>pr-comment.txt | |
terramate script run --changed safe-guard >>pr-comment.txt | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt "### Changed Stacks" | |
echo >>pr-comment.txt | |
echo >>pr-comment.txt '```bash' | |
echo >>pr-comment.txt "${{ steps.list-changed.outputs.stdout }}" | |
echo >>pr-comment.txt '```' | |
echo >>pr-comment.txt ':boom: Generating preview failed. Please see details in Actions output.' | |
cat pr-comment.txt >>$GITHUB_STEP_SUMMARY | |
- name: Publish generated preview as GitHub commnent | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
header: preview | |
path: pr-comment.txt |