pez-infra/.github/workflows/terraform.yml
Rasmus "Pez" Wejlgaard 65090ca9d6
Some checks failed
Deploy (on merge) / Discover hosts (push) Waiting to run
Deploy (on merge) / deploy (push) Blocked by required conditions
Terraform / Plan (push) Has been cancelled
Terraform / Apply (push) Has been cancelled
ci: serialize terraform and deploy runs with concurrency guards (#114)
* 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.

* ci: bump tofu to 1.10.10 in the validate workflow too

Missed this one in the last commit — the PR-time validate still pinned
1.9.0, which trips the new required_version >= 1.10.0 constraint.

* ci: drop use_lockfile — Backblaze B2 can't do native state locking

B2's S3 API returns 501 NotImplemented for the conditional PutObject that
use_lockfile relies on, so tofu plan/apply fails to acquire the lock.
Revert the lockfile and the 1.10 version bump it required; rely on the
concurrency guard to serialize applies instead. Left a note in the
backend block so this isn't re-attempted.
2026-06-02 19:39:13 +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.9.0
- 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.9.0
- 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