fix(apply-terraform-plan): don't fail a green apply on cleanup-PR merge - #330
Open
jlauzy wants to merge 1 commit into
Open
fix(apply-terraform-plan): don't fail a green apply on cleanup-PR merge#330jlauzy wants to merge 1 commit into
jlauzy wants to merge 1 commit into
Conversation
The action opens a plan-file cleanup PR after a successful apply and merges it with the GITHUB_TOKEN, retrying 5x before exit 1. On a base branch that requires an approving review that merge can never succeed — a bot cannot approve its own PR — so the job goes red on a deploy that completed fine. Fall back to auto-merge and warn instead of exiting non-zero. Gate the GCS plan deletion on the merge step's new `merged` output, since reaching that step no longer implies the pointer left the base branch. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 761b1c1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
khan-actions-bot
requested review from
a team,
jeresig and
somewhatabstract
and removed request for
a team
August 7, 2026 17:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After a successful
terraform apply, the action opens a plan-file cleanup PR and merges it with theGITHUB_TOKEN, retrying five times beforeexit 1. On a repo whose base branch requires an approving review, that merge can never succeed — the token acts as the actions bot, and a bot cannot approve its own PR:The apply had already succeeded in that same job:
So the job reports a completed deploy as a failed one, and the real signal is buried under a cleanup failure. The 2.2.4 release that introduced the cleanup PR assumed a base branch that "requires PRs but no approvals or required status checks" — repos that also require a review have never had a green apply. In
Khan/khan-monitoringthis is 5 of its 6apply slosfailures, and a human has had to merge every cleanup PR by hand.It also cascades: that release's other assumption — "a GITHUB_TOKEN merge doesn't trigger further push workflows" — silently stops holding, because the human merge that unblocks things does re-trigger the apply workflow, which then hits
Error: Saved plan is staleon a sibling stack.Fix
The retry loop now falls back to enabling auto-merge (a no-op where auto-merge is disabled) and emits a
::warning::with the PR URL instead of exiting non-zero. An unmerged cleanup PR only leaves a stale plan pointer on the base branch, which the next run's staleness check already catches. A failingterraform applystill fails the job — the trailingMark job as failedstep keys onsteps.apply.outcome, unchanged.The one non-obvious bit
Delete uploaded plan from GCSwas gated only on reaching it, which was safe when an unmergeable cleanup PR failed the job. Now that it doesn't, "we got here" no longer implies "the pointer is gone from the base branch" — so the step is gated on the merge step's newmergedoutput. This preserves the 3.0.0 invariant: delete the uploaded plan only once the cleanup PR has actually landed, or the base branch keeps a pointer to a missing object and every re-run hard-fails at download.Alternatives considered
Fixing this consumer-side, in
khan-monitoring, was worse in every variant: a ruleset bypass for the actions app grants the bot blanket push-bypass onmainrather than just its own cleanup PRs;continue-on-erroron theuses:step masks non-apply failures (apply_failedisfalsewhen apply never ran, e.g. GCP auth or plan download); and an auto-approve workflow forci/terraform-cleanup-*races this action's ~25s retry loop.Patch-level: no input surface changes, and the new behavior only replaces a path that previously always ended in
exit 1. All four other consumers (beep-boop,culture-cron,internal-services,internal-webserver) are onv3.0.0and merge their cleanup PRs successfully today, so they never enter this path.Testing
action.ymlparses;utils/terraform-plan-object-key.test.tsis untouched (no object-key changes). The end-to-end proof iskhan-monitoring's next apply once it is migrated to v3 — it is the only consumer still on v2 and the only one with a review-requiring base branch, so it's the repo that reproduces this.🤖 Generated with Claude Code