Prompt
How do I create staging and preview deploys for a SaaS team?
Latest observation
A good staging/preview setup for a SaaS team usually has two layers:
- Staging: one stable environment that mirrors production as closely as possible.
- Preview deploys: short-lived environments created per branch/PR for review and testing.
1) Define the purpose of each environment
Staging
Use staging for:
- final QA before production
- integration testing
- demoing upcoming features
- validating migrations/config changes
Properties:
- close to production
- shared by the team
- refreshed frequently from production-like data
- stable and predictable
Preview deploys
Use preview environments for:
- reviewing a specific PR
- product/design feedback
- validating feature-specific flows
- testing against real backend integrations if safe
Properties:
- ephemeral
- one per PR/branch
- auto-created and auto-destroyed
- isolated from other work
2) Core architecture
A common pattern:
- main branch → production
- release branch or tagged commit → staging
- pull request branch → preview deploy
Typical flow:
- Developer opens PR
- CI builds app
- A preview environment is deployed with a unique URL
- Team reviews and tests
- PR is merged
- Staging is updated from main/release
- Production deploy happens after approval
3) Infrastructure setup
You need each environment to be independently deployable:
App deployment
Use one of:
- Kubernetes namespaces
- ECS services
- Vercel/Netlify preview deployments
- Fly.io apps
- Heroku review apps
- Docker + your cloud of choice
Data layer
Prefer:
- separate database per environment
- separate object storage buckets
- separate queues/topics
- separate cache namespaces
- separate third-party credentials where possible
For preview deploys, you can choose:
- fully isolated DB per preview if you need real integration testing
- shared read-only fixtures if you only need UI review
- database schema clone/snapshot if supported and cost-effective
4) CI/CD pipeline
A typical pipeline:
On pull request
- Run lint, tests, type checks
- Build container/artifacts
- Provision preview resources
- Deploy app
- Comment preview URL on PR
- Tear down on PR close/merge
On merge to main
- Run full test suite
- Build and deploy to staging
- Optionally run smoke tests
- Promote to production after approval
On release tag
- Deploy exact tagged artifact to production
- Keep staging updated from same artifact when possible
Important: build once, promote the same artifact through environments when you can.
5) Preview environment naming and routing
Use deterministic naming so it’s easy to manage:
pr-123.preview.yourapp.comfeature-login-xyz.preview.yourapp.com
Best practices:
- unique subdomain per preview
- TLS certificates handled automatically
- route by PR number
- clean up DNS records on teardown if needed
6) Handling secrets and config
Each environment should have:
- its own env vars
- its own API keys
- feature flags set appropriately
Recommendations:
- never reuse production secrets in preview/staging
- limit preview access to test-safe integrations
- use secret managers like AWS Secrets Manager, Vault, Doppler, 1Password Secrets Automation, etc.
For preview deploys, consider:
- mock payment providers
- sandbox email/SMS
- disabled destructive jobs
- rate-limited integrations
7) Data strategy
This is usually the hardest part.
Staging data
Options:
- sanitized production snapshot
- seeded test data
- partial production data with PII removed
Good staging data should:
- represent realistic scenarios
- avoid sensitive information
- be refreshed regularly
- be stable enough for QA reproducibility
Preview data
Options:
- empty database + seed script
- cloned staging database snapshot
- per-PR ephemeral fixtures
Common approach:
- seed a minimal dataset for preview
- allow testers to create data freely
- discard on teardown
8) Access control
Staging and preview environments often need protection:
- basic auth or SSO for staging
- restrict preview access to the team or PR author
- separate admin roles from customer-facing access
- audit logs for sensitive actions
If previews expose real integrations, make sure:
- they cannot send real customer emails/texts
- they cannot charge real cards
- they cannot mutate production data
9) Automation and cleanup
Automate everything:
- preview creation on PR open/update
- preview destruction on PR close/merge
- staging deploy on merge to main/release
- database migrations applied safely
- stale previews cleaned up periodically
Add a scheduled cleanup job for orphaned previews in case webhooks fail.
10) Observability
Each environment should have:
- separate logs
- separate metrics
- separate error tracking project or environment tag
- health checks
- smoke tests after deploy
This helps you answer:
- Did deployment succeed?
- Is the app healthy?
- Which preview is failing?
- Did a migration break staging only?
11) Suggested team workflow
A practical workflow for a SaaS team:
- Developers use local dev and test containers
- PRs automatically get previews
- Product/QA uses preview URLs for feedback
- Staging is used for final verification of merged changes
- Production deploys only after staging passes checks
If your team is small, start with:
- one staging environment
- preview deploys for only high-impact PRs
- simple seed data and mock integrations
12) Common mistakes to avoid
- Using production for staging
- Sharing one database across preview environments
- Letting preview environments send real customer emails or webhooks
- Not cleaning up old previews
- Deploying different code than what was tested
- Having staging data that is too fake to be useful
- Making staging too different from production
13) A simple implementation template
If you want a minimal version:
- Production:
mainbranch - Staging: auto-deploy
mainafter merge - Preview: auto-deploy PR branches
- Databases:
- prod DB
- staging DB
- ephemeral preview DBs or shared seed DB
- Config:
- separate env vars for each
- Cleanup:
- delete preview env on PR close
14) Tooling options
Depending on stack:
- Frontend-focused: Vercel, Netlify, Cloudflare Pages
- Full-stack on cloud: Kubernetes, ECS, Fly.io, Render, Railway
- GitOps: Argo CD, Flux
- CI: GitHub Actions, GitLab CI, CircleCI, Buildkite
If you want the easiest preview setup, platforms like Vercel/Netlify handle it well for frontend apps. For full SaaS stacks, GitHub Actions + cloud infra is usually more flexible.
If you want, I can also give you:
- a reference architecture diagram,
- a GitHub Actions example, or
- a setup tailored to your stack (e.g. Next.js, Rails, Django, Node, Kubernetes).