CI/CD
Continuous integration, delivery and deployment — pipeline stages, a GitHub Actions example, fast and reliable builds, artefacts, environments and promotion, deployment strategies (rolling, blue-green, canary), feature flags, GitOps, and DORA metrics.
CI/CD automates the path from a commit to production, so changes ship small, often and safely. It's the backbone of modern software delivery.
| Practice | Meaning |
|---|---|
| Continuous integration (CI) | Everyone merges to the main branch frequently (at least daily); every change is automatically built and tested |
| Continuous delivery | Every change that passes the pipeline is releasable — deploying is a button press |
| Continuous deployment | Every passing change is deployed to production automatically |
A typical pipeline
Build once, deploy many: build an immutable artefact (a container image tagged with the commit SHA) once, then promote the same artefact through environments. Rebuilding per environment risks deploying something different from what you tested.
GitHub Actions example
# .github/workflows/ci.yml
name: CI
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
id-token: write # OIDC to the cloud — no long-lived secrets
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env: { POSTGRES_PASSWORD: test }
ports: ["5432:5432"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: 22, cache: npm }
- run: npm ci
- run: npm run lint
- run: npm test -- --coverage
env: { DATABASE_URL: "postgres://postgres:test@localhost:5432/postgres" }
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
environment: production # protection rules: required reviewers, wait timers
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with: { role-to-assume: "arn:aws:iam::123456789012:role/deploy", aws-region: ap-south-1 }
- run: docker build -t "$REGISTRY/api:${GITHUB_SHA}" . && docker push "$REGISTRY/api:${GITHUB_SHA}"
- run: ./scripts/deploy.sh "${GITHUB_SHA}"Other popular systems: GitLab CI, Jenkins, CircleCI, Buildkite, Azure Pipelines, Argo Workflows/Tekton.
Making pipelines fast and trustworthy
A slow or flaky pipeline gets bypassed. Aim for feedback on a PR within ~10 minutes.
- Cache dependencies and build outputs (npm/Maven/pip caches, Docker layer cache, remote build caches like Bazel/Nx/Turborepo).
- Parallelise test suites and jobs; run only affected projects in monorepos.
- Follow the test pyramid: many fast unit tests, fewer integration tests, a handful of end-to-end tests (Testing).
- Quarantine and fix flaky tests — one flaky test teaches everyone to ignore red builds.
- Fail fast: lint and type-check before slow tests.
- Make
mainalways green: required status checks and branch protection; revert quickly if broken.
Security in the pipeline (DevSecOps)
- Dependency and licence scanning (Dependabot, Renovate, Snyk), static analysis (CodeQL, Semgrep), secret scanning, container image scanning.
- Least-privilege pipeline credentials; OIDC federation instead of stored cloud keys; pin third-party actions to commit SHAs.
- Signed artefacts and provenance (SLSA, Sigstore) to protect the software supply chain.
Environments and promotion
PR preview env → staging (prod-like) → production
per branch automatic deploy manual approval or automaticKeep environments as similar as possible (same artefact, same infrastructure code, different config). Database migrations must be backward-compatible with the running version (expand → migrate → contract), so you can deploy and roll back code independently of schema changes.
Deployment strategies
| Strategy | How | Pros | Cons |
|---|---|---|---|
| Recreate | Stop old, start new | Simple | Downtime |
| Rolling | Replace instances a few at a time | No downtime, no extra capacity | Two versions live at once; slow rollback |
| Blue-green | Run the new version (green) beside the old (blue), switch traffic at once | Instant switch and rollback | Double capacity during the switch |
| Canary | Send a small % of traffic (1% → 10% → 50% → 100%) to the new version while watching metrics | Limits blast radius; data-driven | Needs good metrics and traffic routing |
| Shadow | Mirror real traffic to the new version, discard its responses | Zero user risk | Side effects must be suppressed |
Automated canary analysis (Argo Rollouts, Flagger, Spinnaker) compares error rates and latency between canary and baseline and rolls back automatically on regression.
Feature flags
Decouple deploy from release: ship code to production turned off, then enable it for internal users, a percentage of users, or a region — and turn it off instantly without a deploy.
if (flags.isEnabled("new-checkout", { userId, country })) {
return renderNewCheckout();
}
return renderOldCheckout();Flags enable trunk-based development (merge unfinished work behind a flag), gradual rollouts, A/B tests and kill switches. Remove stale flags — they're technical debt. Tools: LaunchDarkly, Unleash, Flagsmith, OpenFeature.
GitOps
Store the desired state of environments (Kubernetes manifests, Helm values) in Git. An agent in the cluster (Argo CD, Flux) continuously reconciles the cluster to match the repository:
- Deploys are pull requests: reviewed, audited, revertible with
git revert. - Drift (manual
kubectledits) is detected and corrected. - CI builds and pushes images; a bot bumps the image tag in the config repo; the GitOps agent deploys it.
Measuring delivery: DORA metrics
| Metric | Elite performers |
|---|---|
| Deployment frequency | On demand (multiple times per day) |
| Lead time for changes (commit → production) | Less than a day |
| Change failure rate | Low (roughly 0–15%) |
| Time to restore service | Less than an hour |
Research behind these metrics (the Accelerate book and annual DORA reports) shows speed and stability go together: small, frequent changes are easier to test, review and roll back.
- CI: merge often, build and test every change; CD: always releasable; continuous deployment: ship automatically.
- Build one immutable artefact and promote it through environments.
- Keep pipelines fast (caching, parallelism, test pyramid) and reliable (fix flaky tests, protected main).
- Secure the pipeline: scanning, least privilege, OIDC, pinned actions, signed artefacts.
- Rolling, blue-green and canary deployments reduce risk; automate rollback on bad metrics.
- Feature flags separate deploy from release; GitOps makes Git the source of truth for environments.
- Track DORA metrics: deployment frequency, lead time, change failure rate, time to restore.
QCompare blue-green and canary deployments.medium
Blue-green runs two complete environments; the new version is deployed to the idle one, verified, and then all traffic switches at once, with instant rollback by switching back. It needs double capacity, and every user moves at once. Canary gradually shifts a small percentage of real traffic to the new version while comparing its metrics with the old one, increasing the share if healthy and rolling back if not. Canary limits the blast radius of bugs that only appear under real traffic, but needs fine-grained traffic routing and good observability.