# Homework Backend - Docker & Jenkins CI/CD Setup This project includes complete CI/CD setup with Jenkins pipeline and Docker containerization. ## 🏗️ Build & Deploy ### Local Development 1. **Build and run locally:** ```bash # Build the application ./build.sh # Run with Docker Compose docker-compose up --build ``` 2. **Access the application:** - API: http://localhost:8080 - Swagger UI: http://localhost:8080/swagger ### Jenkins Pipeline The Jenkins pipeline includes the following stages: 1. **Checkout** - Clone the repository 2. **Restore Dependencies** - Install NuGet packages 3. **Build** - Compile the application 4. **Test** - Run unit tests 5. **Publish** - Create deployment artifacts 6. **Build Docker Image** - Create Docker image 7. **Push Docker Image** - Push to registry 8. **Deploy to Staging** - Deploy to staging environment 9. **Integration Tests** - Run tests against staging 10. **Deploy to Production** - Manual approval then production deploy ### Environment Variables Set these environment variables for Jenkins: ```bash DOCKER_REGISTRY=your-registry.com DB_PASSWORD=your-database-password ``` ### Docker Registry Credentials Configure Docker registry credentials in Jenkins: - Credential ID: `docker-registry-credentials` - Type: Username with password ## 📁 File Structure ``` ├── Dockerfile # Multi-stage Docker build ├── Jenkinsfile # Jenkins pipeline definition ├── docker-compose.yml # Local development setup ├── docker-compose.staging.yml # Staging environment ├── docker-compose.prod.yml # Production environment ├── .dockerignore # Docker build exclusions ├── build.sh # Local build script ├── deploy.sh # Deployment script ├── init.sql # Database initialization ├── nginx/ │ └── nginx.conf # Nginx reverse proxy config └── Homework/ ├── appsettings.json # Application configuration └── ... # Application code ``` ## 🚀 Deployment Environments ### Development - Single container setup with PostgreSQL - Hot reload enabled - Logs mounted to host ### Staging - Load balanced (2 replicas) - Resource limits applied - Integration testing environment ### Production - Load balanced (3 replicas) - Nginx reverse proxy with SSL - Resource limits and health checks - Database persistence ## 🔧 Configuration ### Application Settings Update `Homework/appsettings.json` for different environments: ```json { "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "JwtSettings": { "Key": "your-jwt-secret-key", "Issuer": "your-issuer", "Audience": "your-audience" }, "ConnectionStrings": { "DefaultConnection": "Host=postgres;Database=homework;Username=homework_user;Password=homework_password" } } ``` ### Jenkins Pipeline Customization Modify the `Jenkinsfile` to: - Change Docker registry URL - Add additional test stages - Modify deployment targets - Add notification channels ## 🏥 Health Checks The application includes health check endpoints: - `/health` - Basic health check - Container health checks configured in Docker ## 🔒 Security - Non-root user in containers - Security headers in Nginx - Resource limits applied - Secrets management via environment variables ## 📊 Monitoring - Application logs are written to files and console - Nginx access logs - Docker container logs - Health check monitoring ## 🐛 Troubleshooting ### Common Issues 1. **Build fails**: Check .NET SDK version in Dockerfile 2. **Database connection**: Verify PostgreSQL credentials 3. **Port conflicts**: Ensure ports 8080, 5432 are available 4. **Registry push fails**: Check Docker registry credentials ### Logs ```bash # View application logs docker-compose logs homework-backend # View database logs docker-compose logs postgres # View all logs docker-compose logs ```