Skip to content

Commit d4d6f10

Browse files
rfayclaude
andauthored
build: implement stable URLs for forked PR previews (#432)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 570966d commit d4d6f10

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

.github/FORK_PREVIEW_SETUP.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ The workflow is triggered automatically for:
9494
### Deployment Process
9595

9696
1. Downloads build artifact from Stage 1
97-
2. Deploys to Cloudflare Pages using API
98-
3. Creates stable preview URL: `https://{hash}.ddev-com-fork-previews.pages.dev`
97+
2. Deploys to Cloudflare Pages using wrangler-action
98+
3. Creates stable branch URL for consistent preview access
9999
4. Comments preview URL on the PR
100100
5. Updates comment on subsequent pushes
101101

@@ -154,25 +154,28 @@ To test the workflow:
154154

155155
### Updates
156156

157-
- Keep `cloudflare/pages-action` version current
157+
- Keep `cloudflare/wrangler-action` version current
158158
- Monitor Cloudflare API changes
159159
- Update content validation rules as needed
160160

161-
## Known Issues & Future Improvements
161+
## Stable URL Implementation
162162

163-
### URL Stability Problem
163+
### Solution Implemented
164164

165-
Currently, Cloudflare's Direct Upload API creates a new deployment hash with each push, resulting in changing preview URLs like:
165+
The workflow now uses `cloudflare/wrangler-action@v3` which provides stable branch URLs through the `pages-deployment-alias-url` output. This ensures consistent preview URLs for each PR:
166166

167-
- First push: `https://2e1a74f4.ddev-com-fork-previews.pages.dev`
168-
- Second push: `https://e98fa6ff.ddev-com-fork-previews.pages.dev`
167+
- **Branch URL**: Stable per PR (e.g., `https://pr-123.project-name.pages.dev`)
168+
- **Deployment URL**: Commit-specific for debugging if needed
169169

170-
This breaks the ability to bookmark or share stable preview URLs for review purposes.
170+
### Benefits
171171

172-
**Potential Solutions to Investigate:**
172+
1. **Stable bookmarking**: Preview URLs remain constant across pushes to the same PR
173+
2. **Better collaboration**: Team members can bookmark and share stable URLs
174+
3. **Future-proof**: Uses the recommended, actively maintained Cloudflare action
175+
4. **Enhanced debugging**: Both stable and commit-specific URLs available
173176

174-
1. **Force predictable branch naming**: Try using branch names like `pr${{ github.event.pull_request.number }}` and construct expected URLs manually
175-
2. **Track and reuse first URL**: Store the initial deployment URL and attempt to update the same deployment rather than creating new ones
176-
3. **Use Cloudflare API directly**: Bypass the GitHub action and use Wrangler CLI or direct API calls for more control over deployment naming
177+
### Migration Notes
177178

178-
**Current Status**: Comment updating works correctly (single comment per PR), but URLs change with each deployment. This is the main remaining issue to solve for optimal user experience.
179+
- Migrated from deprecated `cloudflare/pages-action@v1` to `cloudflare/wrangler-action@v3`
180+
- Updated output variable handling (`url``pages-deployment-alias-url`)
181+
- Maintained backward compatibility with existing project configuration

.github/workflows/cloudflare-preview-forks.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,33 +314,30 @@ jobs:
314314

315315
- name: Publish to Cloudflare Pages (preview)
316316
id: pages
317-
uses: cloudflare/pages-action@v1
317+
uses: cloudflare/wrangler-action@v3
318318
with:
319319
# Required repo secrets and variables (GitHub > Settings > Secrets and variables > Actions)
320320
# CF_PAGES_PROJECT should be a Pages project created as "Direct Upload" (no Git integration).
321321
apiToken: ${{ env.CF_API_TOKEN }}
322322
accountId: ${{ vars.CF_ACCOUNT_ID }}
323-
projectName: ${{ vars.CF_PAGES_PROJECT }}
324-
directory: site-dist
325-
# Stable per-PR preview
326-
branch: pr-${{ github.event.pull_request.number }}
327-
wranglerVersion: "3"
323+
command: pages deploy site-dist --project-name=${{ vars.CF_PAGES_PROJECT }} --branch=pr-${{ github.event.pull_request.number }}
324+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
328325

329326
- name: Comment preview URL
330327
if: ${{ always() }}
331328
uses: actions/github-script@v7
332329
env:
333330
DEPLOYMENT_URL: ${{ steps.pages.outputs.deployment-url }}
334-
ALT_URL: ${{ steps.pages.outputs.url }}
331+
BRANCH_URL: ${{ steps.pages.outputs.pages-deployment-alias-url }}
335332
PR_NUMBER: ${{ github.event.pull_request.number }}
336333
with:
337334
script: |
338335
// Log all available outputs for debugging
339336
core.info(`Cloudflare deployment-url: ${process.env.DEPLOYMENT_URL}`);
340-
core.info(`Cloudflare url: ${process.env.ALT_URL}`);
337+
core.info(`Cloudflare branch-url: ${process.env.BRANCH_URL}`);
341338
342339
// Prefer stable branch URL over commit-specific URL
343-
const branchUrl = process.env.ALT_URL;
340+
const branchUrl = process.env.BRANCH_URL;
344341
const commitUrl = process.env.DEPLOYMENT_URL;
345342
let url = branchUrl || commitUrl || '';
346343
@@ -364,7 +361,7 @@ jobs:
364361
365362
// If there's an existing comment, check if we should reuse its URL for stability
366363
if (existing) {
367-
const existingUrl = existing.body.match(/https:\/\/[a-f0-9]+\.ddev-com-fork-previews\.pages\.dev/);
364+
const existingUrl = existing.body.match(/https:\/\/[a-f0-9-]+\.[a-zA-Z0-9-]+\.pages\.dev/);
368365
if (existingUrl && existingUrl[0]) {
369366
core.info(`Found existing URL: ${existingUrl[0]}, new URL: ${url}`);
370367
if (existingUrl[0] !== url) {

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.ddev/
2+
.github/workflows/

0 commit comments

Comments
 (0)