Add a preview deployment workflow
Add a GitHub Actions workflow that builds every pull request and posts a preview link, using pull_request events and per-branch deploy targets.
A preview deployment gives every pull request its own URL, so reviewers click instead of checking out the branch.
name: Preview
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 24, cache: npm }
- run: npm ci
- run: npm run build
- id: deploy
run: npx wrangler pages deploy dist --branch=pr-${{ github.event.number }}
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}synchronize fires on each new push, so the preview updates with the branch. The --branch value gives each pull request a stable address. The permissions block gives the token only what it needs. See [the pull_request event docs](https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request).
Secrets are not passed to workflows triggered from forks, so a preview for a fork's pull request needs a separate, careful setup. Swap the deploy command for your host's CLI.