Prompt
How do I set up local dev, staging, and production environments for a SaaS team?
Latest observation
A solid SaaS environment setup usually has three layers:
- Local dev: each engineer runs the app on their machine
- Staging: a production-like environment for QA, demos, and release validation
- Production: the real customer-facing system
The key is to make them similar enough that bugs don’t hide, but isolated enough that mistakes don’t leak into prod.
1) Core principles
Keep environments separate
Use separate:
- cloud accounts/subscriptions/projects if possible
- databases
- object storage buckets
- message queues
- third-party API credentials
- OAuth callback URLs
- secrets
This prevents one environment from accidentally affecting another.
Keep config externalized
Use environment variables and secrets management, not hardcoded config.
Typical config differences:
- API base URLs
- DB connection strings
- feature flags
- logging level
- Sentry/monitoring DSNs
- payment provider mode
- email delivery mode
Make staging close to production
Staging should match prod as much as practical:
- same app version
- same infrastructure shape
- same auth flow
- same integrations, but usually with sandbox/test credentials
- same deployment process
Automate everything
Use CI/CD to deploy:
- merge to main → deploy to staging
- tagged release or approved promotion → deploy to production
2) Recommended environment design
Local dev
Purpose:
- feature development
- debugging
- unit/integration testing
Typical setup:
- run the app via Docker Compose or a local dev script
- local database or ephemeral dev database
- mock or sandbox external services
- hot reload / watch mode
Good practices:
- use sample
.env.local - provide seeded test data
- make startup one-command if possible
- support resetting the dev DB quickly
Staging
Purpose:
- QA
- UAT
- pre-release validation
- demos
Typical setup:
- deployed automatically from main branch
- same architecture as prod where possible
- separate DB and storage
- production-like observability
- limited access
Good practices:
- use test/sandbox credentials
- protect with authentication or allowlist
- reset data periodically if needed
- keep test data realistic but non-sensitive
Production
Purpose:
- serve customers reliably
Typical setup:
- strict access controls
- secrets in managed secret store
- blue/green or rolling deploys
- backups, monitoring, alerting
- audited changes and access
Good practices:
- least-privilege IAM
- database backups and recovery drills
- error tracking and tracing
- rate limiting, WAF, and security controls
- strong rollback plan
3) A practical stack pattern
A common setup:
App deployment
- Local: Docker Compose
- Staging: Kubernetes / ECS / Render / Fly / Heroku-like environment
- Prod: same platform as staging
Data
- Local: Postgres via Docker
- Staging: separate managed Postgres instance
- Prod: separate managed Postgres instance with backups and read replicas if needed
Secrets
- Local:
.env.localor.env.development - Staging/Prod: AWS Secrets Manager, GCP Secret Manager, Vault, Doppler, or similar
CI/CD
- GitHub Actions / GitLab CI / CircleCI
- on PR: run tests, lint, build
- on merge to main: deploy to staging
- on release tag/manual approval: deploy to production
4) Environment variables to define
At minimum, standardize these across environments:
APP_ENV=local|staging|productionDATABASE_URLREDIS_URLJWT_SECRETor auth configSESSION_SECRETAPP_BASE_URLAPI_BASE_URLEMAIL_PROVIDEREMAIL_FROMSTRIPE_MODE/ payment keysSENTRY_DSNLOG_LEVELFEATURE_FLAGS_*
Keep a versioned template like:
.env.example.env.local.example
5) Database strategy
Best practice
Use separate databases for each environment.
Options:
- separate DB servers/instances
- separate schemas inside one server for local dev only
Recommended:
- Local: disposable DB
- Staging: persistent but non-prod data
- Prod: isolated, backed up, monitored
Migrations
Use migrations for all schema changes:
- run automatically in CI or deployment
- keep backward compatibility in mind
- avoid breaking prod while old app instances still run
If you have multiple app instances:
- deploy schema changes in a compatible way first
- then deploy code that uses the new schema
- then clean up old columns later
6) Handling external services
For things like:
- Stripe
- Twilio
- SendGrid
- Slack
- OAuth providers
- webhooks
Use:
- sandbox/test accounts in dev/staging
- real credentials only in prod
Also:
- ensure callback URLs are environment-specific
- isolate webhook endpoints
- label test emails/messages clearly
- avoid sending real customer notifications from staging
7) Access control
Local
- developer has full control on their machine
Staging
- team access only
- possibly SSO-protected
- admin-only access to data
- audit logs if possible
Production
- restricted by role
- break-glass access only for emergencies
- every privileged action logged
8) Branching and deploy flow
A simple, effective model:
feature/*branches → PR- PR checks run in CI
- merge to
main→ auto-deploy to staging - release tag or manual approval → deploy to production
Alternative:
- trunk-based development with short-lived branches
Important:
- don’t deploy unreviewed code to prod
- keep staging continuously updated
9) Testing strategy by environment
Local
- unit tests
- component tests
- some integration tests
CI
- full automated suite
- build verification
- contract tests
- linting and security scanning
Staging
- smoke tests
- end-to-end tests
- release validation
- manual QA
Production
- post-deploy smoke checks
- monitoring-based validation
- canary or gradual rollout if possible
10) A good default implementation
If you want a simple setup that works well for a small SaaS team:
-
Local
- Docker Compose
.env.local- local Postgres
- mocked/sandbox integrations
-
Staging
- auto-deployed from
main - separate cloud resources
- sandbox third-party credentials
- protected access
- auto-deployed from
-
Production
- deploy from tagged releases
- production secrets from secret manager
- rolling or blue/green deploys
- monitoring, alerts, backups
-
CI/CD
- PR: test/lint/build
- main: deploy staging
- release: deploy prod
11) Common mistakes to avoid
- using the same database for staging and prod
- sharing secrets between environments
- making staging too different from prod
- manual deploys without rollback
- not testing migrations
- allowing staging to send real emails/SMS
- keeping “temporary” prod configs forever
- letting developers use prod data in local dev
12) A concise recommendation
If you’re starting fresh, do this:
- Local: Docker Compose +
.env.local - Staging: production-like infrastructure, separate account/project, auto-deploy from
main - Prod: separate account/project, secret manager, monitored, backed up, restricted access
- CI/CD: test on PRs, deploy main to staging, promote tagged releases to prod
- External services: sandbox in dev/staging, real in prod
- Data: never share prod DB with any other environment
If you want, I can also give you:
- a sample folder structure and
.envsetup, - a CI/CD pipeline example, or
- a cloud-specific version for AWS/GCP/Azure.