Deployment Guide
How to deploy Almafrica to staging and production environments.
Environments
| Environment | URL | Purpose |
|---|---|---|
| Development | localhost | Local development |
| Staging | staging.almafrica.com | Testing before production |
| Production | almafrica.com | Live system |
Prerequisites
- Access to deployment servers or cloud platform
- Docker installed locally for building images
- Access to container registry
- Database migration permissions
Deployment Process
1. Pre-Deployment Checklist
- All tests pass locally
- PR reviewed and approved
- CHANGELOG.md updated
- Database migrations reviewed (especially Down() methods)
- Environment variables configured in target environment
- Backup of database taken (production)
2. Build and Push Images
# Build backend image
docker build -t almafrica-api:latest ./backend
# Build web image
docker build -t almafrica-web:latest ./web/almafrica-web
# Tag for registry
docker tag almafrica-api:latest registry.example.com/almafrica-api:v1.2.3
docker tag almafrica-web:latest registry.example.com/almafrica-web:v1.2.3
# Push to registry
docker push registry.example.com/almafrica-api:v1.2.3
docker push registry.example.com/almafrica-web:v1.2.3
3. Deploy to Staging
# SSH into staging server
ssh deploy@staging.almafrica.com
# Pull latest images
docker-compose -f docker-compose.staging.yml pull
# Run database migrations
docker-compose -f docker-compose.staging.yml run api dotnet ef database update
# Deploy services
docker-compose -f docker-compose.staging.yml up -d
# Check health
curl https://staging-api.almafrica.com/health
4. Verify Staging
- Health check returns 200 OK
- Login works
- Critical user flows work
- No errors in logs
5. Deploy to Production
# Create database backup
pg_dump almafrica > backup_$(date +%Y%m%d).sql
# Deploy (same as staging but with production compose file)
docker-compose -f docker-compose.production.yml pull
docker-compose -f docker-compose.production.yml run api dotnet ef database update
docker-compose -f docker-compose.production.yml up -d
6. Post-Deployment Verification
- Health check returns 200 OK
- Monitor logs for errors
- Verify key functionality
- Check performance metrics
Database Migrations
Running Migrations
# Check pending migrations
dotnet ef migrations list
# Apply migrations
dotnet ef database update
# Apply to specific migration
dotnet ef database update MigrationName
Rolling Back Migrations
# Rollback to previous migration
dotnet ef database update PreviousMigrationName
# In production, always test rollback in staging first
Migration Best Practices
- Always include Down() method - Required for rollbacks
- Test both directions - Apply and rollback in staging
- Avoid data loss - Don't drop columns with data
- Use transactions - Wrap destructive operations
- Plan for large tables - Add indexes concurrently
Rollback Procedure
If deployment fails or causes issues:
1. Application Rollback
# Revert to previous image version
docker-compose -f docker-compose.production.yml down
docker tag almafrica-api:v1.2.2 almafrica-api:latest
docker-compose -f docker-compose.production.yml up -d
2. Database Rollback
# Rollback database migration
dotnet ef database update PreviousMigrationName
See Database Rollback Runbook for detailed procedures.
Monitoring
Health Endpoints
| Service | Endpoint |
|---|---|
| Backend API | /health |
| Web | /api/health |
Log Aggregation
- Centralized logging via [logging platform]
- Error tracking via Sentry
- Performance monitoring via [APM tool]
Alerts
Configure alerts for:
- Health check failures
- High error rates
- Database connection issues
- Memory/CPU thresholds
CI/CD Pipeline
The deployment is automated via GitHub Actions:
Push to main → Tests → Build Images → Deploy Staging → Manual Approval → Deploy Production
Pipeline Stages
- Test: Run all test suites
- Build: Create Docker images
- Staging Deploy: Automatic on main branch
- Production Deploy: Requires manual approval
Troubleshooting
Deployment fails
- Check CI/CD logs
- Verify environment variables
- Check Docker image builds
- Review database migration logs
Service unhealthy after deploy
- Check container logs:
docker logs <container> - Verify environment configuration
- Check database connectivity
- Review recent changes
Migration fails
- Check migration SQL for errors
- Verify database permissions
- Check for locks:
SELECT * FROM pg_locks - May need manual intervention