Skip to content

Commit 582fbec

Browse files
committed
fix(scripts): exit 0 for plan changes so Make deps aren't broken; synthesize exit in tf-all.sh
1 parent fe8fb64 commit 582fbec

3 files changed

Lines changed: 40 additions & 16 deletions

File tree

.github/workflows/terraform-drift.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
- name: Detect Drift
3535
run: |
3636
set +e
37-
make ci-plan CMD=plan
37+
scripts/tf-all.sh plan ci-plan
3838
EXIT=$?
3939
set -e
4040
if [[ $EXIT -eq 2 ]]; then

scripts/tf-all.sh

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
#!/usr/bin/env bash
2-
# Usage: scripts/tf-all.sh <plan|apply> [--auto-approve]
2+
# Usage: scripts/tf-all.sh <plan|apply> [--auto-approve|<make-target>]
33
#
4-
# plan Preview changes across all modules in dependency order
5-
# apply Apply changes, prompting per module
6-
# apply --auto-approve Apply all changes without prompting
4+
# plan Preview all modules (make target: all)
5+
# plan ci-plan Preview CI-scoped modules only (make target: ci-plan)
6+
# apply Apply all modules, prompting per module
7+
# apply --auto-approve Apply all modules without prompting
78
set -euo pipefail
89

910
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
10-
TOTAL_MODULES=15 # keep in sync with number of targets called in Makefile `all` chain
1111

1212
# ── Parse args ────────────────────────────────────────────────────────────────
1313
CMD="${1:-}"
1414
AUTO_APPROVE=0
15+
MAKE_TARGET="all"
1516

1617
if [[ "$CMD" == "apply" && "${2:-}" == "--auto-approve" ]]; then
1718
AUTO_APPROVE=1
19+
elif [[ "$CMD" == "plan" && -n "${2:-}" ]]; then
20+
MAKE_TARGET="${2}"
1821
fi
1922

2023
if [[ "$CMD" != "plan" && "$CMD" != "apply" ]]; then
21-
echo "Usage: $0 <plan|apply> [--auto-approve]"
24+
echo "Usage: $0 <plan|apply> [--auto-approve|<make-target>]"
2225
echo ""
23-
echo " plan Preview changes across all modules"
24-
echo " apply Apply changes, prompting per module"
25-
echo " apply --auto-approve Apply all changes without prompting"
26+
echo " plan Preview all modules"
27+
echo " plan ci-plan Preview CI-scoped modules only"
28+
echo " apply Apply all modules, prompting per module"
29+
echo " apply --auto-approve Apply all modules without prompting"
2630
exit 1
2731
fi
2832

2933
# ── Progress tracking ─────────────────────────────────────────────────────────
3034
PROGRESS_DIR=$(mktemp -d /tmp/tf-progress.XXXXXX)
3135
trap 'rm -rf "$PROGRESS_DIR"' EXIT
3236

33-
printf '0' > "$PROGRESS_DIR/counter"
37+
# Derive total dynamically so it stays correct for any make target
38+
TOTAL_MODULES=$(make --dry-run "$MAKE_TARGET" CMD=plan 2>/dev/null | grep -c 'tf-module\.sh' || echo 0)
39+
40+
printf '0' > "$PROGRESS_DIR/counter"
3441
printf '%s' "$TOTAL_MODULES" > "$PROGRESS_DIR/total"
3542
touch "$PROGRESS_DIR/results"
3643

@@ -39,17 +46,30 @@ export TF_PROGRESS_DIR="$PROGRESS_DIR"
3946
# ── Run ───────────────────────────────────────────────────────────────────────
4047
cd "$REPO_ROOT"
4148
set +e
42-
# -k (keep going) in plan mode: continue past individual module failures so all
43-
# modules are planned and the summary shows the full picture. Apply mode stays
49+
# -k (keep going) in plan mode: tf-module.sh exits 0 for "changes detected" so
50+
# Make prerequisites are never broken by pending diffs. Apply mode stays
4451
# fail-fast — downstream modules depend on upstream state being applied first.
4552
if [[ "$CMD" == "plan" ]]; then
46-
make -k all CMD="$CMD" AUTO_APPROVE="$AUTO_APPROVE"
53+
make -k "$MAKE_TARGET" CMD="$CMD" AUTO_APPROVE="$AUTO_APPROVE"
4754
else
48-
make all CMD="$CMD" AUTO_APPROVE="$AUTO_APPROVE"
55+
make "$MAKE_TARGET" CMD="$CMD" AUTO_APPROVE="$AUTO_APPROVE"
4956
fi
5057
MAKE_EXIT=$?
5158
set -e
5259

60+
# ── Synthesize exit from results (plan mode only) ─────────────────────────────
61+
# tf-module.sh exits 0 for both clean and drift to keep Make deps intact.
62+
# Re-derive the correct signal here: 1=error, 2=drift, 0=all clean.
63+
if [[ "$CMD" == "plan" ]]; then
64+
if grep -q '^✗' "$PROGRESS_DIR/results" 2>/dev/null; then
65+
MAKE_EXIT=1
66+
elif grep -q '^~' "$PROGRESS_DIR/results" 2>/dev/null; then
67+
MAKE_EXIT=2
68+
else
69+
MAKE_EXIT=0
70+
fi
71+
fi
72+
5373
# ── Summary ───────────────────────────────────────────────────────────────────
5474
echo ""
5575
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

scripts/tf-module.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ if [[ "$CMD" == "plan" ]]; then
6363
1) echo "✗ error $MODULE" >> "$PROGRESS_DIR/results" ;;
6464
esac
6565
fi
66-
exit $PLAN_EXIT
66+
# Exit 0 for both "no changes" (0) and "changes detected" (2) so Make does not
67+
# treat a module with pending changes as a failed prerequisite — that would skip
68+
# all downstream modules. tf-all.sh synthesizes the real exit from the results
69+
# file. Only exit 1 on actual Terraform errors.
70+
[[ $PLAN_EXIT -eq 1 ]] && exit 1 || exit 0
6771
fi
6872

6973
# ── Apply ─────────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)