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
Publishing a Docker Image to GHCR from GitHub ActionsBuild and push a Docker image to GitHub Container Registry from GitHub Actions with Buildx, login, metadata t…
Automating GitHub Releases with GitHub ActionsAutomate GitHub Releases on tag push: generate notes, attach build artifacts, and publish with softprops/acti…
Adding Slack Notifications on Failure to GitHub ActionsPost a Slack message when a GitHub Actions workflow fails using the official slack-github-action and an incom…