Anatomy of a pipeline: stages, jobs and gates
What a CI/CD pipeline is made of: stages that run in order, jobs that run in parallel, gates that stop bad changes, and why fast checks go first.
Anatomy of a pipeline: stages, jobs and gates
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A pipeline is an automated, ordered series of stages that every change passes through on its way to production, and each stage can reject the change.
- Stages run in sequence and stop the pipeline when they fail. The jobs inside a stage can run in parallel on separate machines.
- Put cheap, fast checks first so most failures are reported within minutes, and save slow, expensive checks for changes that passed the fast ones.
- A gate is a condition a change must satisfy to move on: tests passing, a coverage or security threshold, or a human approval before production.
- The pipeline definition lives in the repository as code, so it is reviewed, versioned and reproducible like everything else.
From a checklist to a machine
Continuous integration says every change is verified automatically. Verified how? In a small project the answer is “run the tests”. In a real one it is a list: compile, lint, unit tests, a security scan of dependencies, integration tests against a database, build a package, deploy to a test environment, run end-to-end tests, deploy to production. Done by hand, people skip steps when they are in a hurry, and they are always in a hurry.
A pipeline turns that list into a machine. Every change goes through the same steps, in the same order, every time, and any step can say no. The idea comes from Continuous Delivery (Humble and Farley, 2010), where it is called the deployment pipeline.
Think of an airport. Check-in, security and boarding happen in order. You don’t reach security without a boarding pass, and you don’t board if security stops you. Inside security, several lanes run at once.
Stages, jobs and steps
The vocabulary differs between tools, but the shape is the same.
- A stage is one phase in the ordered sequence: build, test, package, deploy. A stage starts only if the previous one passed.
- A job is a unit of work within a stage, usually on its own fresh machine, called a runner or agent. Jobs in one stage can run in parallel: unit tests and lint at the same time, for example.
- A step is a single command or action within a job.
- An artifact is what a stage produces for later stages to use, such as a compiled package. The artifacts lesson covers these.
A pipeline you can run
To make this concrete without any CI product, I wrote a 25-line bash script with a stage function that runs its jobs in the background, waits for all of them, and exits the whole pipeline if any failed:
stage() { # stage NAME JOBSPEC... : run jobs in parallel, fail the stage if any job fails
local name=$1; shift; local pids=() rc=0
log "[$name]" "start"
for spec in "$@"; do bash -c "$(declare -f job log); job $spec" & pids+=($!); done
for p in "${pids[@]}"; do wait "$p" || rc=1; done
if (( rc )); then log "[$name]" "stage failed, stopping pipeline"; exit 1; fi
}
stage build "compile python3 -m py_compile shop.py test_shop.py"
stage test "unit python3 -m unittest -q test_shop" \
"lint python3 -c 'import ast,sys; ast.parse(open(\"shop.py\").read())'"
stage package "tar tar -czf ../shop-${GIT_SHA:-dev}.tgz shop.py"
if [[ "${APPROVE:-}" != yes ]]; then log "[gate]" "deploy needs APPROVE=yes; stopping here"; exit 0; fi
stage deploy "deploy cp ../shop-${GIT_SHA:-dev}.tgz ../deployed/"
The project is a two-function Python file with two unit tests. On a clean run:
10:58:02 [build] start
10:58:03 compile ok
10:58:03 [test] start
10:58:03 lint ok
10:58:03 unit ok
10:58:03 [package] start
10:58:03 tar ok
10:58:03 [gate] deploy needs APPROVE=yes; stopping here
exit=0
lint finished before unit because they ran in parallel. The pipeline stopped at the approval gate. Then I broke the price function so a test fails:
10:58:06 [build] start
10:58:06 compile ok
10:58:06 [test] start
10:58:06 lint ok
10:58:06 unit FAILED
|
| ----------------------------------------------------------------------
| Ran 2 tests in 0.000s
|
| FAILED (failures=1)
10:58:06 [test] stage failed, stopping pipeline
exit=1
Package and deploy never ran, and the exit code is nonzero, which is what a CI server reads to show a red check. Finally, with the test fixed, APPROVE=yes GIT_SHA=3f9c2ab ran all four stages and copied shop-3f9c2ab.tgz into the deployed directory. Naming the package after the commit is how you later trace what is running back to the source.
A real CI system adds what my script lacks: fresh isolated machines per job, logs kept per run, triggers on push and pull request, secrets handling, and a UI. The structure is the same.
Order matters: fail fast
The most important design decision is the order of checks. A typical pipeline looks like a funnel:
- Commit stage, under about ten minutes: compile, lint, unit tests, a quick dependency scan. Most broken changes die here.
- Acceptance stage: integration and end-to-end tests against a deployed copy of the system. Slower, so it runs only on changes that passed the commit stage.
- Further checks as needed: performance, security, manual exploratory testing.
- Production deploy, automatic or behind an approval.
Cheap checks go first because most failures are cheap to detect. Running a 40-minute browser test suite on code that doesn’t compile wastes 40 minutes of a runner and, worse, 40 minutes of the developer waiting.
Gates
A gate is any condition a change must pass to move forward. Common ones:
- Tests pass: the basic gate.
- Thresholds: coverage not lower than before, no critical vulnerabilities, bundle size under a limit.
- Manual approval: a person clicks approve before production. GitHub calls these environment protection rules.
- Time or change windows: no deploys during a freeze.
Gates are only as good as their signal. A flaky test that fails one run in ten teaches people to click “re-run” without reading, and then the gate stops protecting anything. Tests and quality gates in the pipeline covers that next.
Pipeline as code
Every modern CI tool reads the pipeline from a file in the repository: .github/workflows/*.yml, .gitlab-ci.yml or a Jenkinsfile. That means pipeline changes go through review, history shows who changed a gate and why, and a branch can carry its own pipeline changes. GitHub’s syntax is covered in workflows, triggers, jobs and steps. The three main tools are compared in CI servers compared.
About the video
Be A Better Dev’s “The IDEAL & Practical CI / CD Pipeline - Concepts Overview” (2022, about 22½ minutes) shows, per its description, what an ideal CI/CD pipeline looks like, focusing on concepts rather than specific technologies. Its chapter markers are sparse: intro, production, prod environment, developer experience. I had no transcript, so I can’t say which stages it draws. English was judged from the title, description and channel. The author works mostly with AWS, and the description links an AWS build of the same pipeline. Dave Farley’s two pipeline videos, the explainer and his GOTO 2021 talk, are under deeper and go further into the commit and acceptance stages.
Where this fits
The pipeline is the frame. The next lesson fills its most important stage: tests and quality gates in the pipeline.
Check yourself
What is the difference between a stage and a job?
A stage is a step in the ordered sequence, such as build, test or deploy, and a later stage only starts if earlier ones pass. A job is one unit of work inside a stage, usually on its own runner, and jobs in the same stage can run at the same time.
Why should unit tests and linting come before end-to-end tests?
They are fast and catch most mistakes. Running them first means most broken changes are rejected in a minute or two, and the slow end-to-end suite only runs for changes that are likely to be good.
What does fail fast mean in a pipeline?
When a stage fails, the pipeline stops and reports at once instead of running later stages, which would waste time and could deploy a broken change.
What are two kinds of gate other than tests passing?
A manual approval before deploying to production, and a threshold check such as a minimum code coverage or no critical vulnerabilities in the dependency scan.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.