Skip to content

Commit 5ca58f8

Browse files
Noah Boyersclaude
andcommitted
fix: scope terraform CI to what actually changed
- Gate the apply job on has_changes. An empty change set produced an empty --filter, so `terragrunt run --all -- apply -auto-approve` was applying all 33 units unfiltered. Also handle a skipped apply in the summary, which would otherwise report every no-op push as a failure. - Watch modules/ in both workflows (trigger paths + git diff scope). 16 units consume the shared modules, so edits there were silently never validated or deployed. - Fix the self-trigger glob: the files are tf-*.yml, not terraform-*.yml, so the pattern matched nothing and workflow edits went untested. Known tradeoff: a shared-module change maps to no single Terragrunt directory, so changed_dirs is empty and the deploy applies everything. Intentional - there is no reliable way to map a module to its consumers. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 470fabc commit 5ca58f8

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

.github/workflows/tf-deploy.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- main
77
paths:
88
- 'infra/**'
9+
- 'modules/**'
910
- '.github/workflows/terraform-*.yml'
1011
workflow_dispatch:
1112

@@ -51,11 +52,11 @@ jobs:
5152
exit 0
5253
fi
5354
54-
# Find all changed files in infra/
55-
changed_files=$(git diff --name-only "$BASE_REF" "$HEAD_REF" -- infra/ || echo "")
55+
# Find all changed files in infra/ and the shared modules they consume
56+
changed_files=$(git diff --name-only "$BASE_REF" "$HEAD_REF" -- infra/ modules/ || echo "")
5657
5758
if [ -z "$changed_files" ]; then
58-
echo "No changes detected in infra/"
59+
echo "No changes detected in infra/ or modules/"
5960
echo "has_changes=false" >> $GITHUB_OUTPUT
6061
echo "changed_dirs=[]" >> $GITHUB_OUTPUT
6162
exit 0
@@ -132,6 +133,9 @@ jobs:
132133
apply:
133134
name: Apply Infrastructure
134135
needs: discover-changes
136+
# Without this gate an empty change set yields an empty filter, and
137+
# `terragrunt run --all -- apply -auto-approve` then applies every unit.
138+
if: needs.discover-changes.outputs.has_changes == 'true'
135139
runs-on: ubuntu-latest
136140
environment:
137141
name: production
@@ -308,10 +312,10 @@ jobs:
308312
echo "Manual deployment - all modules processed"
309313
echo ""
310314
elif [ "$has_changes" = "true" ]; then
311-
echo "Changes detected in infra/ but not in terragrunt module directories"
315+
echo "Changes detected in infra/ or modules/ but not in a specific terragrunt directory - applying everything"
312316
echo ""
313317
else
314-
echo "No changes detected in infra/"
318+
echo "No changes detected in infra/ or modules/"
315319
echo ""
316320
fi
317321
@@ -323,6 +327,9 @@ jobs:
323327
echo ""
324328
echo "Infrastructure changes have been applied to production."
325329
exit 0
330+
elif [[ "$apply_result" == "skipped" ]]; then
331+
echo "⏭️ Deployment skipped - no infrastructure changes to apply."
332+
exit 0
326333
else
327334
echo "❌ Deployment failed"
328335
echo ""

.github/workflows/tf-validate.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ on:
66
- main
77
paths:
88
- 'infra/**'
9-
- '.github/workflows/terraform-*.yml'
9+
- 'modules/**'
10+
- '.github/workflows/tf-*.yml'
1011

1112
permissions:
1213
contents: read
@@ -43,11 +44,11 @@ jobs:
4344
BASE_REF="${{ github.event.pull_request.base.sha }}"
4445
HEAD_REF="${{ github.sha }}"
4546
46-
# Find all changed files in infra/
47-
changed_files=$(git diff --name-only "$BASE_REF" "$HEAD_REF" -- infra/ || echo "")
47+
# Find all changed files in infra/ and the shared modules they consume
48+
changed_files=$(git diff --name-only "$BASE_REF" "$HEAD_REF" -- infra/ modules/ || echo "")
4849
4950
if [ -z "$changed_files" ]; then
50-
echo "No changes detected in infra/"
51+
echo "No changes detected in infra/ or modules/"
5152
echo "has_changes=false" >> $GITHUB_OUTPUT
5253
echo "changed_dirs=[]" >> $GITHUB_OUTPUT
5354
exit 0
@@ -495,10 +496,10 @@ jobs:
495496
echo "$changed_dirs" | jq -r '.[]' | sed 's/^/ - /'
496497
echo ""
497498
elif [ "$has_changes" = "true" ]; then
498-
echo "Changes detected in infra/ but not in terragrunt module directories"
499+
echo "Changes detected in infra/ or modules/ but not in a specific terragrunt directory - validating everything"
499500
echo ""
500501
else
501-
echo "No changes detected in infra/"
502+
echo "No changes detected in infra/ or modules/"
502503
echo ""
503504
fi
504505
@@ -549,9 +550,9 @@ jobs:
549550
});
550551
body += '\n';
551552
} else if (hasChanges) {
552-
body += '### ℹ️ Changes detected in `infra/` but not in terragrunt module directories\n\n';
553+
body += '### ℹ️ Changes detected in `infra/` or `modules/` but not in a specific terragrunt directory - everything was validated\n\n';
553554
} else {
554-
body += '### ℹ️ No changes detected in `infra/`\n\n';
555+
body += '### ℹ️ No changes detected in `infra/` or `modules/`\n\n';
555556
}
556557
557558
const allPassed = validateResult === 'success' && planResult === 'success';

0 commit comments

Comments
 (0)