pez-infra/.github/workflows/terraform.yml
Rasmus Wejlgaard ebe8540ec8 ci: serialize infra runs and enable terraform state locking
Add concurrency guards to the terraform and deploy-on-merge workflows so
two merges in quick succession can't run against the same state or the
same hosts at once (queue, never cancel an in-flight run).

Enable native S3 state locking (use_lockfile) on the Backblaze B2 backend,
which needs OpenTofu 1.10+, so bump the CI tofu version 1.9.0 -> 1.10.10
and the required_version constraint to >= 1.10.0.
2026-06-02 19:29:54 +01:00

116 lines
3.3 KiB
YAML

name: Terraform
on:
push:
branches: [main]
paths:
- "terraform/**"
- ".github/workflows/terraform.yml"
# Requires these repository secrets:
# AGE_SECRET_KEY — age private key for SOPS decryption
# Serialize Terraform runs so two merges can't apply against the state
# concurrently. Never cancel an in-flight run (an interrupted apply can
# corrupt state) — queue instead.
concurrency:
group: terraform-state
cancel-in-progress: false
jobs:
plan:
name: Plan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- name: Install OpenTofu
uses: opentofu/setup-opentofu@v2
with:
tofu_version: 1.10.10
- name: Install SOPS
run: |
wget -qO /tmp/sops.deb https://github.com/getsops/sops/releases/download/v3.9.4/sops_3.9.4_amd64.deb
sudo dpkg -i /tmp/sops.deb
- name: Decrypt secrets
env:
SOPS_AGE_KEY: ${{ secrets.AGE_SECRET_KEY }}
run: |
find . -name '*.enc.yml' -o -name '*.enc.yaml' | while read f; do
out="${f/.enc/}"
sops -d "$f" > "$out"
echo "Decrypted: $f -> $out"
done
- name: Set backend credentials
working-directory: terraform/
run: |
echo "AWS_ACCESS_KEY_ID=$(yq '.backblaze_keyID' secrets.yaml)" >> "$GITHUB_ENV"
echo "AWS_SECRET_ACCESS_KEY=$(yq '.backblaze_applicationKey' secrets.yaml)" >> "$GITHUB_ENV"
- name: tofu init
working-directory: terraform/
run: tofu init
- name: tofu plan
working-directory: terraform/
run: tofu plan -out=tfplan
- name: Upload plan
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: actions/upload-artifact@v7
with:
name: tfplan
path: terraform/tfplan
retention-days: 1
apply:
name: Apply
needs: plan
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v6
- name: Install OpenTofu
uses: opentofu/setup-opentofu@v2
with:
tofu_version: 1.10.10
- name: Install SOPS
run: |
wget -qO /tmp/sops.deb https://github.com/getsops/sops/releases/download/v3.9.4/sops_3.9.4_amd64.deb
sudo dpkg -i /tmp/sops.deb
- name: Decrypt secrets
env:
SOPS_AGE_KEY: ${{ secrets.AGE_SECRET_KEY }}
run: |
find . -name '*.enc.yml' -o -name '*.enc.yaml' | while read f; do
out="${f/.enc/}"
sops -d "$f" > "$out"
echo "Decrypted: $f -> $out"
done
- name: Set backend credentials
working-directory: terraform/
run: |
echo "AWS_ACCESS_KEY_ID=$(yq '.backblaze_keyID' secrets.yaml)" >> "$GITHUB_ENV"
echo "AWS_SECRET_ACCESS_KEY=$(yq '.backblaze_applicationKey' secrets.yaml)" >> "$GITHUB_ENV"
- name: tofu init
working-directory: terraform/
run: tofu init
- name: Download plan
uses: actions/download-artifact@v8
with:
name: tfplan
path: terraform/
- name: tofu apply
working-directory: terraform/
run: tofu apply -auto-approve tfplan