Skip to content

only trigger terraform when code is changed (#23) #67

only trigger terraform when code is changed (#23)

only trigger terraform when code is changed (#23) #67

Workflow file for this run

name: Deploy
permissions:
id-token: write
contents: read
on:
push:
paths:
- 'tf/**'
- '.github/workflows/deploy.yml'
branches:
- main
pull_request:
paths:
- 'tf/**'
- '.github/workflows/deploy.yml'
branches:
- main
jobs:
plan:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
pull-requests: write
outputs:
exitcode: ${{ steps.plan.outputs.exitcode }}
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: DeterminateSystems/nix-installer-action@cd46bde16ab981b0a7b2dce0574509104543276e # v9
- uses: DeterminateSystems/magic-nix-cache-action@8a218f9e264e9c3803c9a1ee1c30d8e4ab55be63 # v2
- uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: arn:aws:iam::686862074153:role/plan
aws-region: eu-central-1
- name: init
run: |
cd tf
nix shell --inputs-from . nixpkgs#opentofu --command ./tofu-init.sh
- name: plan
id: plan
run: |
cd tf
set +e
nix shell --inputs-from . nixpkgs#opentofu --command tofu plan -detailed-exitcode -out tfplan
export exitcode=$?
set -e
echo "exitcode=$exitcode" >> "$GITHUB_OUTPUT"
if [ $exitcode -eq 1 ]; then
exit 1
else
exit 0
fi
- name: publish plan
uses: actions/upload-artifact@c7d193f32edcb7bfad88892161225aeda64e9392 # v4
with:
name: tfplan
path: tf/tfplan
- name: Create String Output
id: tf-plan-string
run: |
cd tf
TERRAFORM_PLAN=$(nix shell --inputs-from . nixpkgs#opentofu --command tofu show -no-color tfplan)
delimiter="$(openssl rand -hex 8)"
echo "summary<<${delimiter}" >> $GITHUB_OUTPUT
echo "## Plan Output" >> $GITHUB_OUTPUT
echo "<details><summary>Click to expand</summary>" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo '```terraform' >> $GITHUB_OUTPUT
echo "$TERRAFORM_PLAN" >> $GITHUB_OUTPUT
echo '```' >> $GITHUB_OUTPUT
echo "</details>" >> $GITHUB_OUTPUT
echo "${delimiter}" >> $GITHUB_OUTPUT
- name: Publish Terraform Plan to Task Summary
env:
SUMMARY: ${{ steps.tf-plan-string.outputs.summary }}
run: |
echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY
apply:
runs-on: ubuntu-latest
needs: [plan]
environment: infra
if: github.ref == 'refs/heads/main' && needs.plan.outputs.exitcode == 2
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: DeterminateSystems/nix-installer-action@cd46bde16ab981b0a7b2dce0574509104543276e # v9
- uses: DeterminateSystems/magic-nix-cache-action@8a218f9e264e9c3803c9a1ee1c30d8e4ab55be63 # v2
- uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: arn:aws:iam::686862074153:role/deploy
aws-region: eu-central-1
- name: download plan
uses: actions/download-artifact@7a1cd3216ca9260cd8022db641d960b1db4d1be4 # v4
with:
name: tfplan
path: tf
- name: init
run: |
cd tf
nix shell --inputs-from . nixpkgs#opentofu --command ./tofu-init.sh
- name: apply
run: |
cd tf
nix shell --inputs-from . nixpkgs#opentofu --command tofu apply -auto-approve tfplan