Skip to content

ci: delete a branch's Cloudflare Pages previews when the branch is deleted - #263

Merged
MathiasVDA merged 4 commits into
mainfrom
claude/project-thread-dvin92
Nov 26, 2026
Merged

MathiasVDA merged 4 commits into
mainfrom
claude/project-thread-dvin92

Conversation

@MathiasVDA

@MathiasVDA MathiasVDA commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

What

Adds .github/workflows/cleanup-cloudflare-previews.yml. Cloudflare Pages keeps preview deployments after their branch is gone, so their URLs stay reachable. On a GitHub delete event for a branch, this workflow:

  • lists the preview deployments of the repository's Pages project (same lowercase name as the deploy workflow) through the Cloudflare API,
  • keeps those whose deployment_trigger.metadata.branch matches the deleted branch,
  • deletes them with force=true, which also removes the branch alias.

Production deployments (env=production, i.e. main) are never listed, so they're never touched. It uses the existing CLOUDFLARE_API_TOKEN / CLOUDFLARE_ACCOUNT_ID secrets, skips with a notice when they are missing or the project doesn't exist, and needs no GITHUB_TOKEN permissions.

delete workflows run from the default branch, so this takes effect for branches deleted after it is merged. Previews of branches deleted earlier can be removed in the Cloudflare dashboard.

Adds a workflow that builds the site on every branch push and deploys it
with wrangler to the Cloudflare Pages project named after the repository.
main is the production deployment, other branches get a preview alias.
The job skips itself while the Cloudflare secrets are missing. The GitHub
Pages workflow stays in place until the switch is confirmed.
The Cloudflare API can fail the create call with code 8000000, which
left the project missing and the deploy failing.

@MathiasVDA MathiasVDA left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: OK to merge once the build check is green.

Checked the step script against a stubbed Cloudflare API (30 previews over two pages, mixed branches): it deletes exactly the deployments of the deleted branch, reports 0 for a branch without previews, exits cleanly when the project is missing or the secrets are absent, and fails on any other API error.

  • Only env=preview deployments are listed, so production is out of reach.
  • The branch name comes in through env and jq --arg, not through expression interpolation, and the job has permissions: {}.
  • Collecting ids before deleting avoids skipping entries while paging.

Nit, not blocking: the paging loop has no upper bound; a timeout-minutes: 10 on the job would cap it if the API ever ignored page.

The branch also carries the earlier, already merged Cloudflare commits and a merge commit, so this goes in as a squash (the net diff is the one new file).


Generated by Claude Code

@MathiasVDA
MathiasVDA merged commit 440713e into main Nov 26, 2026
3 checks passed

This branch was successfully deployed

1 active deployment
preview — fc883cb7 Deployed Nov 26, 2026 by github-actions[bot]
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