--- name: deployment-patterns description: CI/CD pipeline patterns, Docker containerization, health checks, blue-green deployment, canary releases, rollback strategies. Use when setting up deployment pipelines, containerizing apps, or planning release strategies. --- # Deployment Patterns Based on ECC deployment-patterns skill. ## CI/CD Pipeline Structure ``` lint → test → build → security-scan → staging-deploy → e2e-test → prod-deploy ``` ### Essential Steps 1. **Lint**: Code style, static analysis 2. **Test**: Unit + integration tests 3. **Build**: Docker image, compile 4. **Security**: Dependency scan, SAST 5. **Staging**: Deploy to staging environment 6. **E2E**: Run end-to-end tests on staging 7. **Production**: Deploy to production (with rollback plan) ## Health Checks ### Liveness Probe (Am I alive?) ```yaml livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 3 periodSeconds: 10 ``` ### Readiness Probe (Am I ready to serve?) ```yaml readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 10 ``` ## Deployment Strategies ### Blue-Green (Zero Downtime) 1. Run current version on "blue" 2. Deploy new version to "green" 3. Test green thoroughly 4. Switch DNS/load-balancer to green 5. Keep blue for rollback ### Canary (Gradual Rollout) 1. Deploy to small % of users (5%) 2. Monitor error rates, latency 3. If OK → increase to 25%, then 50%, then 100% 4. If problems → rollback immediately ### Rolling (Default for Kubernetes) - Replace pods one by one - Built-in to Kubernetes - Can have mixed versions briefly ## Docker Patterns ### Multi-Stage Build ```dockerfile FROM node:20-alpine AS builder COPY . . RUN npm ci --production=false RUN npm run build FROM node:20-alpine AS runtime COPY --from=builder /app/dist ./dist COPY --from=builder /app/package.json ./ RUN npm ci --only=production EXPOSE 3000 CMD ["node", "dist/main.js"] ``` ### Docker Compose ```yaml services: app: build: . ports: ["3000:3000"] depends_on: [db, redis] healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/healthz"] db: image: postgres:16 volumes: [pgdata:/var/lib/postgresql/data] environment: POSTGRES_PASSWORD: ${DB_PASSWORD} ``` ## Rollback Strategy ### Pre-deployment checklist - [ ] Health checks configured - [ ] Logs being captured - [ ] Metrics dashboards ready - [ ] Rollback procedure documented - [ ] Last known good version tagged ### Quick rollback commands ```bash # Kubernetes kubectl rollout undo deployment/app # Docker Compose docker compose down && docker compose up -d --no-build # use previous image # Nginx ln -sfn /var/www/blue /var/www/current && nginx -s reload ``` ## Environment Configuration - Use environment variables for all config - No hardcoded secrets, URLs, ports - Separate configs: dev, staging, production - Use .env files for local, vault/K8s secrets for production ## Checklist - [ ] CI pipeline covers lint → test → build → scan → deploy - [ ] Health checks (liveness + readiness) defined - [ ] Deployment strategy chosen (blue-green, canary, rolling) - [ ] Rollback procedure tested - [ ] Environment variables managed securely - [ ] Docker image is multi-stage (small final image) - [ ] Logs structured (JSON format with correlation IDs)