fix(ci): make PR-preview Neon branch creation idempotent (stop the 422 flake)#439
Conversation
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>
📝 WalkthroughWalkthroughThis PR adds a workflow step to the PR preview pipeline that deletes stale Neon preview branches before creating a new one. The step runs ChangesNeon Preview Branch Idempotency
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
agent-paste PR preview is ready. API: https://agent-paste-api-pr-439.isaac-a46.workers.dev |
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
.github/workflows/pr-preview.yml
|
agent-paste PR preview resources were cleaned up. The shared Preview GitHub Environment is retained for future preview deploys. |
Summary
The
Deploy PR Previewcheck intermittently failed at "Create Neon PR branch" with a422from the Neon API. Root cause for the recurring case: the preceding "Reconcile stale PR preview resources" step runscleanup-stale-pr-previews.mjs --exclude-pr "$PR_NUMBER", so it deliberately does not delete the current PR's ownpreview/pr-Nbranch. On any re-run of an open PR (a new commit pushed), that branch still exists andcreate-branch-action422s 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-actionkeeps handling create +db_urlextraction. 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.mjsis already covered bydelete-neon-pr-branch.test.mjspnpm verify+pnpm test:coveragegreen; pushed through the pre-push hook with no--no-verify🤖 Generated with Claude Code
Summary by CodeRabbit