{"title": "Deployment", "content": "# Deployment Guide\n\n## CI/CD Pipeline Setup\n\n### Prerequisites\n\n1. **Azure VM Setup**\n   - VM IP: `20.191.108.105`\n   - User: `azureuser`\n   - Application path: `/home/azureuser/qapp/q/`\n\n2. **GitHub Secrets Configuration**\n\n   Add these secrets to your GitHub repository (`Settings > Secrets and Variables > Actions`):\n\n   - `AZURE_HOST`: `20.191.108.105`\n   - `AZURE_SSH_PRIVATE_KEY`: Your private SSH key for Azure VM access\n\n### Setting up SSH Key\n\n```bash\n# Generate SSH key pair (if you don't have one)\nssh-keygen -t rsa -b 4096 -C \"your-email@example.com\"\n\n# Copy public key to Azure VM\nssh-copy-id azureuser@20.191.108.105\n\n# Add private key to GitHub secrets\ncat ~/.ssh/id_rsa  # Copy this content to AZURE_SSH_PRIVATE_KEY secret\n```\n\n### Automated Deployment\n\nThe CI/CD pipeline automatically triggers on:\n- Push to `main` branch\n- Manual workflow dispatch from GitHub Actions tab\n\n**Deployment Process:**\n1. Checks out latest code\n2. Sets up SSH connection to Azure VM\n3. Pulls latest changes on the server\n4. Builds new Docker image\n5. Creates backup of current deployment\n6. Gracefully shuts down current containers\n7. Starts new containers with updated code\n8. Performs health checks\n9. Rollback on failure\n\n### Manual Deployment\n\nIf you need to deploy manually:\n\n```bash\n# SSH to Azure VM\nssh azureuser@20.191.108.105\n\n# Navigate to application directory\ncd qapp/q/\n\n# Pull latest changes\ngit pull origin main\n\n# Build and deploy\ndocker build -t q_app .\ndocker-compose down --timeout 30\ndocker-compose up -d\n\n# Check logs\ndocker-compose logs --follow\n```\n\n### Health Check\n\nThe application includes a health check endpoint at `/api/health/` that verifies:\n- Database connectivity\n- Redis connectivity (if configured)\n- Application status\n\nHealth check response:\n```json\n{\n  \"status\": \"healthy\",\n  \"checks\": {\n    \"database\": true,\n    \"redis\": true,\n    \"app\": true\n  },\n  \"version\": \"1.0.0\",\n  \"environment\": \"production\"\n}\n```\n\n### Rollback Procedure\n\nIf deployment fails, the pipeline automatically attempts rollback. For manual rollback:\n\n```bash\n# SSH to Azure VM\nssh azureuser@20.191.108.105\ncd qapp/q/\n\n# Find previous commit\ngit log --oneline -10\n\n# Reset to previous commit\ngit reset --hard <previous-commit-hash>\n\n# Rebuild and deploy\ndocker build -t q_app .\ndocker-compose down\ndocker-compose up -d\n```\n\n### Monitoring\n\n- **Application Logs**: `docker-compose logs -f`\n- **System Health**: `/api/health/`\n- **Container Status**: `docker-compose ps`\n- **Resource Usage**: `docker stats`\n\n### Troubleshooting\n\n**Common Issues:**\n\n1. **SSH Connection Failed**\n   - Verify SSH key is correctly added to GitHub secrets\n   - Ensure Azure VM allows SSH access\n   - Check if SSH key is authorized on the VM\n\n2. **Git Pull Fails**\n   - Check if repository access is configured on Azure VM\n   - Verify Git credentials or SSH keys for repository access\n\n3. **Docker Build Fails**\n   - Check Dockerfile syntax\n   - Verify all dependencies are available\n   - Review build logs in GitHub Actions\n\n4. **Health Check Fails**\n   - Check database connectivity\n   - Verify Redis configuration\n   - Review application logs\n\n5. **Container Start Fails**\n   - Check docker-compose.yml configuration\n   - Verify environment variables\n   - Review container logs\n\n### Environment Variables\n\nEnsure these environment variables are configured on the Azure VM:\n\n```bash\n# In your .env file or environment\nDEBUG=False\nENVIRONMENT=production\nAPP_VERSION=1.0.0\nDATABASE_URL=...\nBROKER_URL=...\n```\n\n### Security Considerations\n\n- SSH keys should be rotated regularly\n- GitHub secrets are encrypted and only accessible during workflow execution\n- Health check endpoint doesn't expose sensitive information\n- Database credentials should be secured\n- Consider using Azure Key Vault for sensitive configuration\n\n### Performance Optimization\n\n- **Docker Layer Caching**: Optimize Dockerfile for better build times\n- **Health Check Frequency**: Monitor but don't overwhelm the system\n- **Graceful Shutdown**: 30-second timeout allows clean shutdown\n- **Resource Limits**: Consider setting Docker resource limits in production\n\n### Maintenance\n\n- **Regular Updates**: Keep dependencies updated\n- **Log Rotation**: Configure log rotation to manage disk space\n- **Backup Strategy**: Regular database and application backups\n- **Monitoring**: Set up proper monitoring and alerting\n- **SSL Certificates**: Keep SSL certificates updated\n"}