CI/CD
Understand CI/CD: Continuous Integration, Continuous Delivery, and Continuous Deployment, the automated release engine of modern DevOps.
Imagine an automobile factory. Decades ago, workers assembled an entire car by hand, pushed it out into the parking lot, and turned the ignition key. If the engine refused to start, mechanics had to spend days taking the engine apart with wrenches to figure out which wire was loose. Modern car factories operate on an automated assembly line. As the frame moves along a conveyor belt, robotic arms install specific parts. At every station, sensors immediately verify that each bolt was tightened correctly. If something is misaligned, the conveyor stops instantly and alerts the team within seconds. CI/CD is that automated assembly line for software. Instead of developers manually copying files onto servers, running tests on their own laptops, and hoping everything works, a CI/CD pipeline acts as an automated conveyor belt. Every time a developer commits code to Git, the pipeline compiles the code, runs automated tests, checks for security flaws, builds a clean production package, and deploys it to servers with minimal manual effort.
Definition
CI/CD is an automated engineering practice combining Continuous Integration (frequently merging code changes into a shared repository and verifying them with automated builds and tests) with Continuous Delivery or Continuous Deployment (automatically packaging, testing, and releasing validated code to staging and production environments).
Why It Matters
Without CI/CD, releasing software is slow, stressful, and prone to mistakes. Developers spend hours resolving merge conflicts, building zip files, logging into servers via SSH, and trying to figure out why an app works on a laptop but fails on a server. CI/CD turns software releases into a predictable, routine process. It ensures that the main codebase is always ready to deploy. If a bug is introduced, the pipeline catches it within minutes, telling the team exactly which commit and line of code caused the issue so they can fix it right away.
How It Works
A complete CI/CD pipeline typically consists of three stages: 1. Continuous Integration (CI): Developers frequently push small code changes to a shared Git branch. On every push or pull request, an automated CI runner (such as GitHub Actions or GitLab CI) spins up a clean virtual environment, downloads the code, installs dependencies, compiles the application, and runs automated tests and security scans. If any test fails, the build is marked failed and developers are notified immediately. 2. Continuous Delivery (CD: Delivery): Once the code passes all CI tests, the pipeline packages an immutable production artifact (such as a Docker container image tagged with the Git commit hash) and deploys it to a staging environment that mirrors production. In Continuous Delivery, the final move to production is protected by a manual approval step: a team lead or product manager clicks an 'Approve Release' button when the team is ready. 3. Continuous Deployment (CD: Deployment): In this advanced approach, there is no manual approval button. Every commit that successfully passes the automated test suite, security scans, and staging checks is automatically deployed straight to production within minutes, protected by automated health checks. 4. The 'Build Once, Deploy Everywhere' Principle: A key rule of CI/CD is to compile and package your application (like a Docker image) only once during the CI phase. You then promote that exact same image through development, staging, and production environments. Differences between environments (such as database passwords and API URLs) are supplied at runtime through environment variables rather than rebuilding the code.
Automated CI/CD Pipeline Workflow
Key Points
- Continuous Integration verifies code correctness; Continuous Delivery automates shipping code to environments.
- Continuous Delivery includes a human approval step before production; Continuous Deployment is completely automated.
- Pipelines should fail fast: fast syntax and unit checks should run before slower integration tests.
- Artifacts must be immutable: build your Docker container image once in CI, then promote that same image across environments.
- Secrets and credentials (such as API keys and passwords) should be stored in secure pipeline secrets, never hardcoded in scripts.
Steps
Ensure Reliable Local Build Commands
Make sure the application can be built, packaged, and tested locally using standard terminal commands (such as npm run build and npm test).
Create a Pipeline Configuration File
Write a pipeline file (such as .github/workflows/ci.yml) defining the operating system, language runtime, and test commands.
Set Up Branch Protection Rules
Configure repository settings so code cannot be merged into main until all automated tests pass and a teammate reviews the pull request.
Automate Container Image Packaging
Add a pipeline step that builds a Docker image tagged with the Git commit hash and pushes it to a container registry.
Configure Zero-Downtime Deployment
Connect your deployment target (such as Kubernetes or a managed cloud service) to roll out the new image smoothly without dropping connections.
Examples
The Airport Security Analogy
A CI/CD pipeline works much like airport boarding: first your ticket is checked (linting), then you pass through the metal detector (unit tests), your luggage goes through the scanner (security checks), and you wait in the boarding lounge (staging). Only passengers who pass every checkpoint are allowed onto the flight (production).
Real-World Examples
GitHub's Own Deployment Pipeline
GitHub deploys GitHub.com multiple times an hour using an automated pipeline. Every pull request merged to the main branch automatically triggers tests, builds packages, and releases updates to internal staff accounts first, then to a small percentage of public traffic, and finally to all users globally.
Technical Examples
name: Production CI/CD Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
permissions:
contents: read
packages: write
jobs:
# 1. TEST AND VERIFICATION (CI Phase)
test:
name: Lint, Type Check & Unit Tests
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Set up Node.js runtime
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Install dependencies cleanly
run: npm ci
- name: Check code formatting and types
run: |
npm run lint
npx tsc --noEmit
- name: Run automated unit tests
run: npm test -- --coverage --ci
# 2. BUILD IMMUTABLE ARTIFACT
build:
name: Build & Push Container Image
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push container image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
ghcr.io/${{ github.repository }}:${{ github.sha }}
ghcr.io/${{ github.repository }}:latest
# 3. CONTINUOUS DELIVERY (Deploy with Approval Gate)
deploy:
name: Deploy to Production
needs: build
runs-on: ubuntu-latest
environment:
name: production # Requires manual approval in repository settings
steps:
- name: Set Kubernetes context
uses: azure/k8s-set-context@v3
with:
kubeconfig: ${{ secrets.KUBECONFIG }}
- name: Update deployment image
run: |
kubectl set image deployment/web-api \
web-api=ghcr.io/${{ github.repository }}:${{ github.sha }} \
-n production
kubectl rollout status deployment/web-api -n production --timeout=90sContinuous Delivery vs Continuous Deployment
| Aspect | Continuous Delivery | Continuous Deployment |
|---|---|---|
| Automation Level | Automated through staging; human authorizes production deploy | Completely automated from code commit to production |
| Human Approval Gate | Yes: a team lead or product manager clicks 'Approve' | No: deployments proceed automatically once tests pass |
| Prerequisites | Reliable automated unit and integration tests | Comprehensive automated test suites, canary rollouts, and automatic rollbacks |
| Common Use Cases | Regulated environments (finance, healthcare) and enterprise software | Web platforms, consumer mobile APIs, and SaaS products |
| Release Rhythm | Daily or weekly based on business scheduling | Multiple times per day immediately upon merging |
Core Principles of Dependable Pipelines
Advantages
- Fast Issue Detection: Bugs are caught within minutes of writing code while the logic is fresh in your mind.
- Less Manual Work: Eliminates the tedious, error-prone steps of copying files and running manual commands on servers.
- Consistent Quality: Every change goes through the exact same automated testing and security verification.
- Simple Rollbacks: Rolling back to an earlier release is as straightforward as redeploying an earlier image tag.
Limitations
- Test Suite Maintenance: Flaky tests that fail randomly need to be fixed or isolated so they do not block normal releases.
- Initial Setup: Setting up runners, secrets, and cloud permissions requires initial planning and effort.
- Pipeline Delays: If automated test suites grow too slow, developers have to wait longer for feedback.
Interview Questions
What is the difference between Continuous Delivery and Continuous Deployment?
Both approaches automate the building, testing, and staging deployment process. The single key difference is that Continuous Delivery includes a manual decision or approval button before code goes to production, whereas Continuous Deployment pushes every passing commit straight to production automatically.
What does 'Build Once, Deploy Everywhere' mean?
It means you build and package your application (such as a Docker image) a single time during the CI stage. You then promote that exact same package through development, test, and production environments, injecting environment-specific details (like database URLs) using environment variables at runtime. This guarantees that what you tested in staging is identical to what runs in production.
Practice Questions
- In a repository you use, create a simple GitHub Actions workflow that runs your project's test command on every pull request.
- What strategies would you use to speed up a CI pipeline that takes 45 minutes to run?
