Skip to content
jshacks.dev
Tooling

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.

1 min read
Summarize in AI

A preview deployment gives every pull request its own URL, so reviewers click instead of checking out the branch.

.github/workflows/preview.ymlYAML
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.

Share

More snippets

Tooling

Create a Vite project from a template

Create a Vite project from a template with npm create vite, choose the vanilla-ts or react-ts template and see which files it generates.

1 min read
Tooling

Configure VS Code settings for a team

Share VS Code settings and recommended extensions with a team by committing a .vscode folder with settings.json and extensions.json.

1 min read
Tooling

Share an ESLint config across packages

Share one ESLint 9 flat config across workspace packages by exporting an array from a package and spreading it into each package's eslint.config.js.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.