Skip to content

Commit 392c571

Browse files
fix(ci): handle GitHub preview environment delete without failing
The cleanup job calls DELETE /repos/.../environments/... which returns 403 for the default GITHUB_TOKEN. PR #70 removed invalid workflow permission environments: write, which did not fix token scope anyway. Catch 403 so PR-close cleanup succeeds after Cloudflare teardown. Use optional secret PREVIEW_ENV_CLEANUP_TOKEN when a repo wants automatic environment removal, and document it in the setup guide. Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
1 parent 8f8f184 commit 392c571

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

.github/workflows/preview.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ jobs:
561561
INPUT_TARGET: ${{ inputs.target }}
562562
INPUT_PR_NUMBER: ${{ inputs.pr_number }}
563563
with:
564+
# The default GITHUB_TOKEN cannot delete deployment environments (403).
565+
# Optional: set repo secret PREVIEW_ENV_CLEANUP_TOKEN to a PAT with repo
566+
# scope (classic) or Administration: write (fine-grained) for this repo.
567+
github-token: ${{ secrets.PREVIEW_ENV_CLEANUP_TOKEN || github.token }}
564568
script: |
565569
const eventName = process.env.EVENT_NAME;
566570
let envName;
@@ -606,6 +610,14 @@ jobs:
606610
core.info(
607611
`GitHub environment not found (already deleted): ${envName}`,
608612
);
613+
} else if (e.status === 403) {
614+
core.warning(
615+
[
616+
`Cannot delete GitHub environment "${envName}" with the current token (403).`,
617+
"The default workflow token is not allowed to delete environments.",
618+
"Add repository secret PREVIEW_ENV_CLEANUP_TOKEN (PAT: classic `repo`, or fine-grained with Administration read/write on this repository) to enable automatic removal.",
619+
].join(" "),
620+
);
609621
} else {
610622
throw e;
611623
}

docs/agents/setup.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ each PR preview is isolated:
121121
When a PR is closed, the cleanup job deletes the preview Worker(s) and these
122122
resources as well.
123123

124+
The same cleanup job removes the matching GitHub deployment environment
125+
(`preview-<pr>`). GitHub’s default `GITHUB_TOKEN` cannot call the delete
126+
environment API (you would see HTTP 403). That step is non-fatal: the workflow
127+
still succeeds and Cloudflare resources are still removed. To delete the GitHub
128+
environment automatically, add an Actions secret named
129+
`PREVIEW_ENV_CLEANUP_TOKEN` whose value is a personal access token with
130+
permission to administer this repository (for example a classic PAT with the
131+
`repo` scope, or a fine-grained PAT with **Administration** read and write on
132+
this repo only).
133+
124134
Cloudflare Workers supports version `preview_urls`, but those preview URLs are
125135
not currently available for Workers that use Durable Objects. The main app
126136
Worker binds `MCP_OBJECT`, so app previews continue to use per-PR Worker names.

0 commit comments

Comments
 (0)