Skip to content

feat(terraform): open a recovery pull request after a failed apply - #398

Merged
tobias0106 merged 1 commit into
mainfrom
terraform-recovery-pr
Nov 10, 2026
Merged

tobias0106 merged 1 commit into
mainfrom
terraform-recovery-pr

Conversation

@tobias0106

Copy link
Copy Markdown
Contributor

Why

After a failed apply, the only way back is command: recover, which plans and applies in one job without anyone reviewing the plan (monta-app/terraform#440). Environment approvals would fix that, but they aren't available for private repos on our GitHub plan.

What

A new optional secret on terraform-stack.yml, recovery-token. When it's set and an apply fails, the workflow:

  • creates terraform-recover/<stack> from the default branch and writes the failed run URL into <stack>/recover.tf. It uses the contents API, so the runner needs no git credentials. If a recovery PR is already open, it adds a commit to that PR and doesn't reset the branch.
  • opens a draft PR. Since the PR changes the stack, discovery selects that stack and the PR gets the normal plan comment, which is the leftover change set. Merging it applies the reviewed plan.
  • links the PR in the terraform-failed issue and changes the issue's "next step" text to point at it.

Any successful apply of the stack closes the open recovery PR.

Callers that don't pass the secret behave exactly as before.

Caveats

  • The token can't be GITHUB_TOKEN, because PRs it opens don't trigger workflows. The token needs contents and pull-request write access on the caller repo.
  • If the apply never started because no reviewed plan was found, no recovery PR is opened and recover is still the way out.
  • This hasn't run end to end yet. The YAML parses and the script passes bash -n. The first real failure in monta-app/terraform will be the actual test.

🤖 Generated with Claude Code

A failed apply pushes terraform-recover/<stack> with the run URL in
<stack>/recover.tf and opens a draft PR, so the leftover changes get a
reviewed plan and apply on merge instead of an unreviewed recover.
Opt-in through the recovery-token secret.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tobias0106
tobias0106 marked this pull request as ready for review November 9, 2026 11:37
@tobias0106
tobias0106 requested a review from a team as a code owner November 9, 2026 11:37
@tobias0106
tobias0106 requested review from chandrashekar-dhar and removed request for a team November 9, 2026 11:37
@tobias0106 tobias0106 closed this Nov 10, 2026
@tobias0106 tobias0106 reopened this Nov 10, 2026
@tobias0106
tobias0106 merged commit bd96e82 into main Nov 10, 2026
2 checks passed
@tobias0106
tobias0106 deleted the terraform-recovery-pr branch November 10, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants