Skip to content
Latchkey

Adding Deploy Previews to GitHub Actions

Spin up a per-PR preview deployment and post the URL back as a comment.

Deploy previews give reviewers a live URL for each PR. The pattern is: build, deploy to a preview environment (Cloudflare Pages, a namespaced bucket, a PR-scoped container), then comment the URL on the PR using the GitHub script or a sticky-comment action.

What you need

  • A host that supports per-PR/preview deploys.
  • A deploy step that returns a preview URL.
  • pull-requests: write to post the comment.

The workflow

Deploy then comment the preview URL on the PR.

.github/workflows/preview.yml
permissions:
  contents: read
  pull-requests: write
steps:
  - uses: actions/checkout@v4
  - run: npm ci && npm run build
  - id: deploy
    run: echo "url=https://pr-${{ github.event.number }}.preview.example.com" >> "$GITHUB_OUTPUT"
  - uses: marocchino/sticky-pull-request-comment@v2
    with:
      message: "Preview ready: ${{ steps.deploy.outputs.url }}"

Common gotchas

  • Forked PRs lack deploy secrets - use pull_request_target carefully or skip previews on forks.
  • Tear down previews on PR close, or stale environments and costs pile up.
  • Every PR builds and deploys, which adds minutes; faster cheaper managed runners (Latchkey) keep preview spend in check.

Key takeaways

  • Build, deploy to a preview env, then comment the URL on the PR.
  • Use a sticky comment so the URL updates in place.
  • Tear down previews on PR close to control cost.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →