Preview Environments
Spin up isolated preview deployments for every pull request with automatic teardown.
Overview
Preview environments let reviewers test changes in a production-like URL before merge. Relay builds your app on each PR, deploys to a unique subdomain, and tears down the environment when the PR closes.
Enable previews in relay.yaml
Add a preview job that runs only on pull requests:
jobs:
preview:
if: relay.event == 'pull_request'
runs-on: relay-linux-medium
environment:
name: preview-${{ relay.pr.number }}
url: https://pr-${{ relay.pr.number }}.preview.example.com
steps:
- uses: relay/checkout@v4
- run: npm ci && npm run build
- uses: relay/preview-deploy@v2
with:
ttl: 7dConfigure routing
Point a wildcard DNS record at Relay's preview ingress, or use Relay-managed domains:
preview:
domain: preview.example.com
pathPrefix: /prEach PR receives a URL like https://pr-142.preview.example.com.
Comment on the PR
Relay posts a status comment with the preview URL, build logs link, and deploy gate status:
- uses: relay/preview-comment@v1
with:
template: |
**Preview ready:** ${{ steps.deploy.outputs.url }}
Commit: ${{ relay.sha }}Configuration options
Lifecycle
Control how long previews stay alive:
- uses: relay/preview-deploy@v2
with:
ttl: 7d
destroy-on-close: true
destroy-on-merge: trueStale previews are garbage-collected nightly if destroy-on-close is disabled.
Environment secrets
Preview environments inherit scoped secrets — never production credentials:
environment:
name: preview-${{ relay.pr.number }}
secrets:
- DATABASE_URL_PREVIEW
- STRIPE_TEST_KEYService dependencies
Provision databases or caches alongside the preview:
- uses: relay/preview-services@v1
with:
postgres: '15'
redis: '7'Connection strings are injected as environment variables for the deploy step.
Monorepo path filters
Deploy only affected apps in a monorepo:
jobs:
preview-web:
if: relay.changes.includes('apps/web/**')
steps:
- uses: relay/preview-deploy@v2
with:
working-directory: apps/webDeploy gates
Require checks to pass before a preview URL is published:
Insights
Track preview usage in the Relay dashboard:
Prop
Type
Best Practices
Performance Tip: Reuse build caches from the base branch to cut preview build times in half.
- Use path filters to skip previews for docs-only changes
- Scope secrets to preview-specific values
- Set
ttlto match your review cadence (typically 3–7 days) - Enable
destroy-on-mergeto avoid orphaned infrastructure