Skip to content

fix(ci): make PR-preview Neon branch creation idempotent (stop the 422 flake)#439

Merged
isuttell merged 1 commit into
mainfrom
fix/neon-pr-branch-idempotent
Jun 8, 2026
Merged

fix(ci): make PR-preview Neon branch creation idempotent (stop the 422 flake)#439
isuttell merged 1 commit into
mainfrom
fix/neon-pr-branch-idempotent

Conversation

@isuttell

@isuttell isuttell commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Summary

The Deploy PR Preview check intermittently failed at "Create Neon PR branch" with a 422 from the Neon API. Root cause for the recurring case: the preceding "Reconcile stale PR preview resources" step runs cleanup-stale-pr-previews.mjs --exclude-pr "$PR_NUMBER", so it deliberately does not delete the current PR's own preview/pr-N branch. On any re-run of an open PR (a new commit pushed), that branch still exists and create-branch-action 422s on the duplicate name.

Fix: delete this PR's Neon branch immediately before creating it, so the create is idempotent. Reuses the already-tested scripts/delete-neon-pr-branch.mjs, which no-ops when the branch is absent (first run). Project branch quota is already handled by the capacity check + stale-preview cleanup that run just before this step, so this closes the remaining (collision) cause.

Why not a retry wrapper / hand-rolled create

The proven neondatabase/create-branch-action keeps handling create + db_url extraction. Reimplementing that path just to add retries would add a new, hand-rolled Neon-API surface that could itself become flaky — the opposite of the goal. The deterministic collision is the recurring failure; deleting first removes it outright.

Risk

Low. One added CI step that reuses an existing, tested script. No production code changes. If the branch doesn't exist (first run), it's a no-op.

Test plan

  • delete-neon-pr-branch.mjs is already covered by delete-neon-pr-branch.test.mjs
  • pnpm verify + pnpm test:coverage green; pushed through the pre-push hook with no --no-verify
  • On merge: re-running a PR's preview (push a second commit) no longer 422s on "Create Neon PR branch"

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Enhanced stability of PR preview deployments by automatically cleaning up stale preview branches before creating new ones, ensuring more reliable preview environment setup.

The stale-preview reconcile step runs with --exclude-pr for the current
PR, so re-running the workflow on an open PR (new commit pushed) leaves
that PR's own preview/pr-N Neon branch in place. "Create Neon PR branch"
then 422s on the existing branch name -- the recurring "Deploy PR
Preview" flake.

Delete this PR's branch before creating it (reusing the already-tested
delete-neon-pr-branch.mjs, which no-ops when the branch is absent), so
the create is idempotent. Project branch quota is already handled by the
capacity check + stale-preview cleanup that run just before.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds a workflow step to the PR preview pipeline that deletes stale Neon preview branches before creating a new one. The step runs scripts/delete-neon-pr-branch.mjs with the current PR number and Neon project credentials, ensuring idempotent branch creation.

Changes

Neon Preview Branch Idempotency

Layer / File(s) Summary
Stale Neon branch cleanup step
.github/workflows/pr-preview.yml
A new workflow step is inserted after stale-preview reconcile that deletes any existing preview/pr-<PR_NUMBER> Neon branch using the delete-neon-pr-branch script and Neon project credentials.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

Poem

🐰 A branch once created, now cleaned up with care,
Delete the old stale ones floating in air,
Before making new ones, sweep the slate clean—
Idempotent previews in your Neon machine! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: making Neon PR branch creation idempotent by addressing the 422 API error flake.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/neon-pr-branch-idempotent

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.github/workflows/pr-preview.yml:
- Around line 77-88: Ensure the GitHub Action step invoking
scripts/delete-neon-pr-branch.mjs sets PR_NUMBER in env (PR_NUMBER: ${{
github.event.pull_request.number }}) and includes NEON_PROJECT_ID and
NEON_API_KEY as shown, do not add continue-on-error: true, and keep calling the
script without CLI args so the script’s process.env.PR_NUMBER fallback behavior
and no-op-on-missing-branch idempotency remain intact; confirm the script still
fails on real Neon API errors (e.g., 401) so the action fails fast on real
problems.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8955b420-dc2f-40b6-b968-82b68849750a

📥 Commits

Reviewing files that changed from the base of the PR and between 890e76a and f792d58.

📒 Files selected for processing (1)
  • .github/workflows/pr-preview.yml

Comment thread .github/workflows/pr-preview.yml
@isuttell isuttell merged commit 01fee20 into main Jun 8, 2026
6 checks passed
@isuttell isuttell deleted the fix/neon-pr-branch-idempotent branch June 8, 2026 17:29
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

agent-paste PR preview resources were cleaned up. The shared Preview GitHub Environment is retained for future preview deploys.

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.

1 participant