Set Up Cloudflare PR Previews
Create a reusable deployment topology while adapting names, build output, migrations, runtime URL variables, and explicitly shared resources to the target project.
Route the task
- Inspect repository instructions, package manager, Wrangler/build config, generated Worker output, migrations, bindings, CI, and deployment docs. Inspect Better Auth and Prisma configuration when present.
- Read only the references needed:
references/topology-and-lifecycle.md for naming, stable/branch topology, aliases, isolation, or environment selection.
references/d1-and-deployment.md for provisioning, migrations, binding injection, deploy commands, or target-repository integration.
references/cleanup-and-operations.md for PR-close cleanup, GitHub trust boundaries, dashboard configuration, access, or credentials.
references/vite-plugin-integration.md when the repository uses @cloudflare/vite-plugin.
Read all three core references for a full setup; use only the affected reference for a focused audit or repair.
- Resolve the resource prefix, Worker names, production branches, D1 binding/migrations, deployable config path, stable URLs, origin-dependent variables, and isolated versus shared bindings before rendering.
- Render the bundled templates into scratch space, never over the target repository:
python3 <skill-dir>/scripts/render_templates.py \
--app-name <app> \
--preview-worker-name <app-staging> \
--d1-binding DB \
--migrations-dir prisma/migrations \
--built-config dist/wrangler.json \
--url-var BETTER_AUTH_URL \
--url-var VITE_SITE_URL \
--output .context/cloudflare-pr-previews
- Integrate rendered files deliberately. Preserve repository behavior and package-manager conventions; never replace Wrangler, package, build, migration, or workflow files blindly.
Implementation order
- Confirm the topology, branch identity, resource-isolation map, and stable URLs.
- Configure explicit development/preview/production inputs and select the environment before bundling.
- Provision or resolve D1, apply migrations, patch the actual deploy config by binding name, and upload with active-origin variables.
- Add trusted PR-close cleanup for only lifecycle-owned branch resources.
- Adapt the rendered
CLOUDFLARE_SETUP.md as the single dashboard and operations guide.
Verification
Run checks that do not mutate Cloudflare first:
bash -n scripts/resolve-cloudflare-env.sh
bash -n scripts/preview-resources.sh
bash -n scripts/deploy.sh
bash -n scripts/cleanup-preview-db.sh
Then verify environment resolution, deterministic deploy/cleanup naming, the real generated config path, binding-specific D1 patching, migrations, package-manager consistency, and secret absence. For Vite builds, confirm flattened-config deployment commands omit --env.
If credentials and authorization are available, finish with one real non-production preview deployment, redeploy the same branch to prove reuse, then run manual cleanup for that test branch and confirm a second cleanup is a no-op. Do not exercise production merely to validate the setup.
Guardrails
- Do not auto-create production databases or production buckets.
- Do not delete stable preview or production resources.
- Do not expose secrets through
--var, source config, logs, or committed dotenv files.
- Do not claim full per-PR isolation when any mutable binding is shared; list shared bindings explicitly.
- Do not use this preview-URL topology for Durable Object Workers or derive an alias from a custom hostname.
- Do not feed commented JSONC to generated
jq; keep it JSON-compatible or use a JSONC-aware parser.
- Do not treat migration as deployment; record history only after Worker upload succeeds.
- Fail visibly on D1/authentication errors and do not add unnecessary GitHub write permissions.
Resources
references/topology-and-lifecycle.md: deployment modes, environment resolution, naming, aliases, and binding isolation.
references/d1-and-deployment.md: provisioning, migrations, binding patching, upload modes, and repository integration.
references/cleanup-and-operations.md: cleanup trust boundary, Workers Builds setup, credentials, and public access.
references/vite-plugin-integration.md: dual-config patching and build-time/runtime URL handling.
scripts/render_templates.py: render parameterized orchestration files into a scratch directory.
assets/*.tmpl: source templates copied by the renderer; integrate them rather than editing the skill in place.
1---2name: setup-cloudflare-pr-previews3description: Set up, audit, or repair Cloudflare Workers PR previews with aliased URLs, branch-isolated D1 databases, migrations, binding injection, stable environments, and PR-close cleanup.4---5
6# Set Up Cloudflare PR Previews
7
8Create a reusable deployment topology while adapting names, build output, migrations, runtime URL variables, and explicitly shared resources to the target project.
9
10## Route the task
11
121. Inspect repository instructions, package manager, Wrangler/build config, generated Worker output, migrations, bindings, CI, and deployment docs. Inspect Better Auth and Prisma configuration when present.
132. Read only the references needed:
14 - `references/topology-and-lifecycle.md` for naming, stable/branch topology, aliases, isolation, or environment selection.
15 - `references/d1-and-deployment.md` for provisioning, migrations, binding injection, deploy commands, or target-repository integration.
16 - `references/cleanup-and-operations.md` for PR-close cleanup, GitHub trust boundaries, dashboard configuration, access, or credentials.
17 - `references/vite-plugin-integration.md` when the repository uses `@cloudflare/vite-plugin`.
18 Read all three core references for a full setup; use only the affected reference for a focused audit or repair.
193. Resolve the resource prefix, Worker names, production branches, D1 binding/migrations, deployable config path, stable URLs, origin-dependent variables, and isolated versus shared bindings before rendering.
204. Render the bundled templates into scratch space, never over the target repository:
21
22```bash
23python3 <skill-dir>/scripts/render_templates.py \
24 --app-name <app> \
25 --preview-worker-name <app-staging> \
26 --d1-binding DB \
27 --migrations-dir prisma/migrations \
28 --built-config dist/wrangler.json \
29 --url-var BETTER_AUTH_URL \
30 --url-var VITE_SITE_URL \
31 --output .context/cloudflare-pr-previews
32```
33
345. Integrate rendered files deliberately. Preserve repository behavior and package-manager conventions; never replace Wrangler, package, build, migration, or workflow files blindly.
35
36## Implementation order
37
381. Confirm the topology, branch identity, resource-isolation map, and stable URLs.
392. Configure explicit development/preview/production inputs and select the environment before bundling.
403. Provision or resolve D1, apply migrations, patch the actual deploy config by binding name, and upload with active-origin variables.
414. Add trusted PR-close cleanup for only lifecycle-owned branch resources.
425. Adapt the rendered `CLOUDFLARE_SETUP.md` as the single dashboard and operations guide.
43
44## Verification
45
46Run checks that do not mutate Cloudflare first:
47
48```bash
49bash -n scripts/resolve-cloudflare-env.sh
50bash -n scripts/preview-resources.sh
51bash -n scripts/deploy.sh
52bash -n scripts/cleanup-preview-db.sh
53```
54
55Then verify environment resolution, deterministic deploy/cleanup naming, the real generated config path, binding-specific D1 patching, migrations, package-manager consistency, and secret absence. For Vite builds, confirm flattened-config deployment commands omit `--env`.
56
57If credentials and authorization are available, finish with one real non-production preview deployment, redeploy the same branch to prove reuse, then run manual cleanup for that test branch and confirm a second cleanup is a no-op. Do not exercise production merely to validate the setup.
58
59## Guardrails
60
61- Do not auto-create production databases or production buckets.
62- Do not delete stable preview or production resources.
63- Do not expose secrets through `--var`, source config, logs, or committed dotenv files.
64- Do not claim full per-PR isolation when any mutable binding is shared; list shared bindings explicitly.
65- Do not use this preview-URL topology for Durable Object Workers or derive an alias from a custom hostname.
66- Do not feed commented JSONC to generated `jq`; keep it JSON-compatible or use a JSONC-aware parser.
67- Do not treat migration as deployment; record history only after Worker upload succeeds.
68- Fail visibly on D1/authentication errors and do not add unnecessary GitHub write permissions.
69
70## Resources
71
72- `references/topology-and-lifecycle.md`: deployment modes, environment resolution, naming, aliases, and binding isolation.
73- `references/d1-and-deployment.md`: provisioning, migrations, binding patching, upload modes, and repository integration.
74- `references/cleanup-and-operations.md`: cleanup trust boundary, Workers Builds setup, credentials, and public access.
75- `references/vite-plugin-integration.md`: dual-config patching and build-time/runtime URL handling.
76- `scripts/render_templates.py`: render parameterized orchestration files into a scratch directory.
77- `assets/*.tmpl`: source templates copied by the renderer; integrate them rather than editing the skill in place.