The Goal
Continuous Integration (CI): Merge code changes frequently. Automated tests verify correctness. Continuous Deployment (CD): Automatically release changes to production if tests pass.
Goal: Reduce Mean Time To Recovery (MTTR) and Increase Deployment Frequency.
Interactive: The Pipeline Flow
CI/CD Pipeline Flow
The Pipeline Anatomy
A modern pipeline isn't just "build and deploy". It's a series of quality gates.
graph LR
Dev[Developer Commit] --> CI[CI: Build & Test]
CI --> Art[Artifact Registry]
Art --> Staging[CD: Deploy to Staging]
Staging --> E2E[E2E Tests]
E2E --> Prod[CD: Deploy to Prod]
Prod --> Monitor[Observability]
1. Build & Test (CI)
Triggers: Pull Request, Git Push. Tasks:
- Linting (ESLint, Pylint)
- Static Analysis (SonarQube)
- Unit Tests (Jest, PyTest)
- Build Container Image (Docker)
[!IMPORTANT] Ideally < 10 minutes. Slow CI kills developer velocity.
2. Artifact Promotion
Don't rebuild for every environment! Build once, deploy everywhere.
- CI builds Docker Image
app:v1.0.0-sha123. - Push to Registry (ECR/DockerHub) as
app:v1.0.0-rc. - Deploy
app:v1.0.0-rcto Staging. - If successful, retag as
app:v1.0.0-stableand deploy to Prod.
Why? Ensures the exact binary tested in Staging goes to Prod.
Deployment Strategies
How do we update production without downtime?
Blue-Green Deployment
- Method: Spin up a complete new environment (Green) alongside old (Blue).
- Cutover: Switch Load Balancer to Green.
- Rollback: Switch LB back to Blue instantly.
- Cost: High (Double infrastructure resources).
graph TD
Traffic[User Traffic] --> LB{Load Balancer}
subgraph Active
LB -->|100%| V1[Blue: v1.0]
end
subgraph Idle
V2[Green: v2.0]
end
Canary Deployment
- Method: Route small % of traffic (1-5%) to new version.
- Validation: Monitor error rates/latency.
- Rollout: Gradually increase traffic (1% -> 10% -> 50% -> 100%).
- Risk: Lowest. Bugs only affect a small subset of users.
Rolling Update (Kubernetes Default)
- Method: Replace instances one by one.
maxUnavailable: 1,maxSurge: 1. - Cost: Low (No extra infra).
- Risk: Harder to rollback instantly.
Pipeline as Code (GitHub Actions)
Defining pipelines in YAML allows version control of the build process itself.
name: Production Build
on:
push:
branches: [ "main" ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '18.x'
- run: npm ci
- run: npm test
build-push:
needs: test
runs-on: ubuntu-latest
steps:
- name: Build Docker Image
run: docker build -t myapp:${{ github.sha }} .
- name: Push to Registry
run: |
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u user --password-stdin
docker push myapp:${{ github.sha }}
deploy-staging:
needs: build-push
environment: staging
steps:
- name: Update K8s Manifest
run: |
sed -i "s|image: .*|image: myapp:${{ github.sha }}|" k8s/deployment.yaml
kubectl apply -f k8s/
Testing Pyramid
Automated tests are the safety net of CI/CD.
| Type | Speed | Cost | Quantity | Example |
|---|---|---|---|---|
| Unit | Fast (ms) | Low | Many | add(1, 2) == 3 |
| Integration | Medium (s) | Med | Some | API calls Database |
| E2E (UI) | Slow (min) | High | Few | Selenium login flow |
Feature Flags
Decouple Deployment (technical) from Release (business).
- Deploy code with feature disabled:
if (features.enableNewUI) { ... } - Enable flag for internal users.
- Enable flag for 10% of users.
- Enable for everyone.
Tools: LaunchDarkly, Split.io, Unleash.
Interview Tips š”
- "How do you handle schema changes?" ā Backward compatible migrations. Add column -> Deploy Code -> Fill Column -> Remove old code.
- "What if the deployment fails?" ā Automated Rollback based on health checks.
- "Immutable Infrastructure" ā Don't patch servers. Replace them (Terraform + Packer).
- "GitOps" ā Infrastructure updates via Pull Request (ArgoCD).
Related Concepts
About ScaleWiki
ScaleWiki is an interactive educational platform dedicated to demystifying distributed systems, software architecture, and system design. Our mission is to provide high-quality, technically accurate resources for software engineers preparing for interviews or solving complex scaling challenges in production.
Read more about our Editorial Guidelines & Authorship.
Educational Disclaimer: The architectural patterns and system designs discussed in this article are based on common industry practices, technical whitepapers, and public engineering blogs. Actual implementations in enterprise environments may vary significantly based on specific product requirements, legacy constraints, and evolving technologies.
Related Articles
Blue-Green Deployment
Zero-downtime deployment strategy using two identical production environments (Blue and Green) to enable instant rollbacks, reduce risk, and allow thorough testing before directing traffic.
Docker Internals
What actually is a container? Just a Linux process with a mask on. Deep dive into Namespaces, Cgroups, and Union Filesystems (OverlayFS).
Kubernetes Architecture Explained
Under the hood of K8s: The Control Plane (API Server, Scheduler, Etcd, Controllers) and Data Plane (Kubelet, Kube-proxy, Container Runtime).