The idea: every commit takes the same road to production

A CI/CD pipeline is a fixed list of checks and steps that a server runs for every change. Each push to the main branch starts a new run of the pipeline for that commit. The run builds the code, tests it, packs it into an artifact, puts that artifact on a test environment, and, after a last check, on production. CI (continuous integration) is the first half: merge often and check every merge. CD (continuous delivery) is the second half: every green commit can be released by one click.

A commit c2 enters a row of stages: source, build, test (unit tests and static checks side by side), package, staging, verify, approve and production. Package pushes the image app:c2 to a registry; staging and production both pull that same image.
One commit, one run: the image is built once and the same image moves from staging to production.

On the canvas, the top panel shows the commits on main and on pull requests, coloured by what happened to them. The middle shows the current run: 9 jobs in 8 stages, with arrows for "this job needs that one". The timeline below draws each job as a bar on a clock in minutes, and the right panel shows the registry, the dependency cache, staging, production and a log of the runs.

Stages, jobs and needs

A job is one unit of work on one runner (a fresh machine or container). A stage is a group of jobs with the same purpose. A job starts as soon as every job it needs is green. This is the page's pipeline written in a GitHub Actions-style file; GitLab CI and Jenkins say the same thing in their own words:

on:
  push: { branches: [main] }
  pull_request: { branches: [main] }
jobs:
  build:
    steps:
      - uses: actions/checkout@v4
      - uses: actions/cache@v4          # restore node_modules
        with:
          key: deps-${{ hashFiles('package-lock.json') }}
      - run: npm ci && npm run build
  unit-tests:    { needs: build, steps: [ run: npm test ] }
  static-checks: { needs: build, steps: [ run: npm run lint ] }
  package:       { needs: [unit-tests, static-checks] }  # docker build + push app:${{ github.sha }}
  deploy-staging:    { needs: package, environment: staging }
  integration-tests: { needs: deploy-staging }
  deploy-production: { needs: integration-tests, environment: production }  # needs approval

The jobs are drawn from left to right in the order the needs allow. A job that does not need another one does not wait for it, which is how unit tests and static checks run side by side.

Build once, then promote the same artifact

package image builds a container image and tags it with the commit: app:c2. That image is pushed to a registry. deploy staging and deploy production do not build again; they both pull app:c2. So what runs in production is byte for byte what was tested on staging. If each environment rebuilt from source, a new package version or a different compiler could slip in between the two. How the image itself is stored in layers is on Docker Image Layers.

A red job stops the line

When a job fails, every job that needs it is skipped. Jobs that do not depend on it still finish: in Demo 3 static checks is green at minute 6 while unit tests fails at minute 7. Nothing after the test stage runs, so production keeps app:c1. Nobody "fixes" the failed run: the fix is a new commit, c3, and it goes through the whole pipeline again in 17 minutes. From the bad push to the fix in production took 24 minutes.

Some bugs only show up when the service runs with its real database and neighbours. In Demo 5 unit tests pass, the image reaches staging, and the integration tests fail at minute 14. Staging now runs the broken app:c2, but the approval gate never opens, so production still runs app:c1. That is the point of a staging environment. How unit tests replace those neighbours with stubs and fakes is on Test Doubles.

Making it fast: parallel jobs and a dependency cache

A slow pipeline means people batch many changes into one push, which makes failures harder to trace. Two cheap fixes:

  • Parallel jobs. Unit tests (3 min) and static checks (2 min) both need only build. In parallel the test stage takes max(3, 2) = 3 minutes; in serial it takes 3 + 2 = 5 (Demo 4: lead time 19 instead of 17 minutes).
  • A dependency cache. Installing packages from the internet takes 4 minutes. The cache saves node_modules under a key made from the lockfile, deps-lock1. The next run with the same lockfile restores it in 1 minute: build drops from 6 to 3 minutes and the lead time from 20 to 17 (Demos 1 and 2). A commit that changes the lockfile gets a new key, deps-lock2, misses, and pays the 4 minutes again.
Two timelines of the same run, with a warm cache. Parallel: unit tests minutes 4 to 7 and static checks 4 to 6 overlap, staging is tested by minute 14, lead time 17. Serial: static checks waits until minute 7 and ends at 9, everything after moves 2 minutes later, lead time 19.
The test stage costs the longest job in parallel, and the sum of the jobs in serial.

Lead time here is the time from the push to the commit running in production. It is one of the four DORA metrics, next to deployment frequency, change failure rate and time to restore.

Gates: approval, the merge gate and superseded runs

A gate is a point where a run must wait for a condition. The approval gate waits for a person to approve the production deploy; on the page the reviewer takes 2 minutes. With the gate the pipeline is continuous delivery; without it, every green commit would go straight out, which is continuous deployment.

In trunk-based development everyone merges small changes into main at least once a day. To keep main green, main is protected by a merge gate: a pull request runs the build and test jobs on its branch, and it may merge only when they are green. In Demo 6 PR #1 has a lint error, the gate blocks it, and main stays at c1. PR #2 passes in 7 minutes, merges as c2, and the merged code runs the full pipeline once more. Lead time from opening PR #2 to production: 7 + 14 + 2 + 1 = 24 minutes.

The main branch as a line with commits c1 and c2. PR 1 with a lint error reaches the merge gate and is blocked with a red cross. PR 2 passes the checks with a green tick, merges as c2, and c2 runs the full pipeline to production.
The merge gate runs the checks before the merge, so a red change never lands on main.

If a newer commit arrives while an older run waits at the gate, deploying the older one is pointless: the newer commit already contains it. Pipelines can cancel the waiting run (GitHub Actions concurrency with cancel-in-progress, GitLab "skip outdated deployment jobs"). In Demo 7 c2 waits at minute 14, c3 is pushed, run #2 is cancelled, and c3 reaches production 17 minutes later. c2's change went live 31 minutes after its push, inside c3.

The page stops at "running in production". How the new version then takes over traffic, a little at a time or all at once with a quick way back, is on Canary and Blue/Green Deployments.

The demos in numbers

DemoWhat happensProduction at the endLead time
1Cold cache: build 6 min, staging tested at min 17c120 min
2c2 hits the cache; c3 changes the lockfile and missesc317, then 20 min
3c2 fails unit tests at min 7; the fix c3 goes throughc317 min (24 from the bad push)
4Serial test jobs: test stage 5 min instead of 3c219 min
5c2 fails integration tests on staging at min 14c317 min
6PR #1 blocked by lint; PR #2 merges as c2c224 min from PR open
7c3 supersedes c2 at the gatec317 min (c2: 31)

See also Git Merge vs Rebase for how the branch of a pull request becomes part of main.

See also UML Use Case Diagram for where the behaviour that these tests check is first written down.

What the page leaves out

  • Runners are unlimited here; real pipelines queue for runners, and a busy queue adds minutes to the lead time.
  • Flaky tests, retries, test sharding across many runners, and build matrices (several OS or language versions).
  • Secrets, artifact signing and supply-chain checks, security scans, and database migrations as a pipeline step.
  • Merge queues, which test several pull requests merged together before they land.
  • Rollback and traffic shifting after the deploy, and monitoring that triggers them.
  • Real job times vary; here every job has a fixed time in whole minutes so every demo gives the same numbers.

On the mind map

This page on the Servers & Event Loops mind map · 31 pages in 5 branches · open it to see the whole map and test yourself in recall mode.