4.9 KiB
4.9 KiB
🚀 Staging Environment Setup
Overview
You now have two separate Docker stacks:
-
Staging - Deploys automatically on
devormainbranch- Port:
3001 - Container:
portfolio-app-staging - Database:
portfolio_staging_db(port 5433) - Redis:
portfolio-redis-staging(port 6380) - URL:
https://staging.dk0.dev(orhttp://localhost:3001)
- Port:
-
Production - Deploys automatically on
productionbranch- Port:
3000 - Container:
portfolio-app - Database:
portfolio_db(port 5432) - Redis:
portfolio-redis(port 6379) - URL:
https://dk0.dev
- Port:
How It Works
Automatic Staging Deployment
When you push to dev or main branch:
- ✅ Tests run
- ✅ Docker image is built and tagged as
staging - ✅ Staging stack deploys automatically
- ✅ Available on port 3001
Automatic Production Deployment
When you merge to production branch:
- ✅ Tests run
- ✅ Docker image is built and tagged as
production - ✅ Zero-downtime deployment (blue-green)
- ✅ Health checks before switching
- ✅ Rollback if health check fails
- ✅ Available on port 3000
Safety Features
Production Deployment Safety
- ✅ Zero-downtime: New container starts before old one stops
- ✅ Health checks: Verifies new container is healthy before switching
- ✅ Automatic rollback: If health check fails, old container stays running
- ✅ Separate networks: Staging and production are completely isolated
- ✅ Different ports: No port conflicts
- ✅ Separate databases: Staging data doesn't affect production
Staging Deployment
- ✅ Non-blocking: Staging can fail without affecting production
- ✅ Isolated: Completely separate from production
- ✅ Safe to test: Break staging without breaking production
Ports Used
| Service | Staging | Production |
|---|---|---|
| App | 3001 | 3000 |
| PostgreSQL | 5433 | 5432 |
| Redis | 6380 | 6379 |
Workflow
Development Flow
# 1. Work on dev branch
git checkout dev
# ... make changes ...
# 2. Push to dev (triggers staging deployment)
git push origin dev
# → Staging deploys automatically on port 3001
# 3. Test staging
curl http://localhost:3001/api/health
# 4. Merge to main (also triggers staging)
git checkout main
git merge dev
git push origin main
# → Staging updates automatically
# 5. When ready, merge to production
git checkout production
git merge main
git push origin production
# → Production deploys with zero-downtime
Manual Commands
Staging
# Start staging
docker compose -f docker-compose.staging.yml up -d
# Stop staging
docker compose -f docker-compose.staging.yml down
# View staging logs
docker compose -f docker-compose.staging.yml logs -f
# Check staging health
curl http://localhost:3001/api/health
Production
# Start production
docker compose -f docker-compose.production.yml up -d
# Stop production
docker compose -f docker-compose.production.yml down
# View production logs
docker compose -f docker-compose.production.yml logs -f
# Check production health
curl http://localhost:3000/api/health
Environment Variables
Staging
NODE_ENV=stagingNEXT_PUBLIC_BASE_URL=https://staging.dk0.devLOG_LEVEL=debug(more verbose logging)
Production
NODE_ENV=productionNEXT_PUBLIC_BASE_URL=https://dk0.devLOG_LEVEL=info
Database Separation
- Staging DB:
portfolio_staging_db(separate volume) - Production DB:
portfolio_db(separate volume) - No conflicts: Staging can be reset without affecting production
Monitoring
Check Both Environments
# Staging
curl http://localhost:3001/api/health
# Production
curl http://localhost:3000/api/health
View Container Status
# All containers
docker ps
# Staging only
docker ps | grep staging
# Production only
docker ps | grep -v staging
Troubleshooting
Staging Not Deploying
- Check GitHub Actions workflow
- Verify branch is
devormain - Check Docker logs:
docker compose -f docker-compose.staging.yml logs
Production Deployment Issues
- Check health endpoint before deployment
- Verify old container is running
- Check logs:
docker compose -f docker-compose.production.yml logs - Manual rollback: Restart old container if needed
Port Conflicts
- Staging uses 3001, 5433, 6380
- Production uses 3000, 5432, 6379
- If conflicts occur, check what's using the ports:
lsof -i :3001 lsof -i :3000
Benefits
✅ Safe testing: Test on staging without risk ✅ Zero-downtime: Production updates don't interrupt service ✅ Isolation: Staging and production are completely separate ✅ Automatic: Deploys happen automatically on push ✅ Rollback: Automatic rollback if deployment fails
You're all set! Push to dev/main for staging, merge to production for production deployment! 🚀