Ordering checks from cheapest to most expensive
Order pipeline checks by cost and by how often they fail, so cheap checks catch simple mistakes before expensive jobs start.
Contentsand lesson path
On this page
- The Fail-Fast Architecture: Minimizing Feedback Latency
- The Cost Hierarchy: From Static Analysis to End-to-End Suites
- Optimizing Pipeline Concurrency: Fan-Out and Caching Gates
- Check your understanding
- Apply it
- Review prompt
Lessons of Build & Test Pipeline
- What makes a build reproducible Concept · 15 min
- One entry point: wrapping your toolchain in Make or a task runner Worked example · 20 min
- Ordering checks from cheapest to most expensive Concept · 15 min
- Lab: lint, test and package a service with reports Lab · 30 min
- Hunting a flaky test and a slow step Worked example · 20 min
- Check: build pipeline review Knowledge check · 6 questions
What you will learn
- Order lint, unit test, integration test and packaging steps so they fail fast and emit machine-readable test reports.
01. The Fail-Fast Architecture: Minimizing Feedback Latency
A CI pipeline exists to tell you quickly whether a change is safe. The time a pull request pipeline takes affects how fast people can work, how often they can deploy and how quickly they can ship a fix. If a pipeline takes thirty minutes to run and fails at minute twenty-eight on a trivial linting whitespace error or misspelled variable name, the engineer has lost half an hour of productive context and momentum.
The fail-fast principle orders checks along two dimensions:
- Cost (time and compute): checks that finish in seconds run before jobs that take minutes or hours of runner time.
- How often a check fails: checks that catch the most frequent slips (syntax errors, missing imports, unformatted files, broken type signatures) run before complex system tests.
When the fast checks run first, a broken change fails within minutes, the developer still remembers the code, and shared runners are not tied up with jobs that were going to fail anyway. Early gates also keep broken pull requests off scarce resources such as GPU runners or end-to-end test environments.
02. The Cost Hierarchy: From Static Analysis to End-to-End Suites
The Test Pyramid, popularised by Mike Cohn and described in "The Practical Test Pyramid" on Martin Fowler's site, has many fast unit tests at the bottom and few slow end-to-end tests at the top. A pipeline can extend the same idea to every kind of check. The durations below are rough orders of magnitude, not measurements; time your own jobs.
- Static analysis, formatting and syntax (seconds): ESLint, ShellCheck, Ruff, Black, Prettier, Markdownlint and compiler syntax checks read the source files without running the program, so they need no database or service.
- Type checking (seconds to tens of seconds): TypeScript
tsc --noEmit, mypy, Gogo vetand Rustcargo checkcheck interfaces, types and references across modules without producing a runnable program. - Unit tests (tens of seconds to a few minutes): Jest, Vitest, pytest, Go
testingand JUnit test functions and logic in isolation, with network, database and disk access replaced by fakes. Usually the most deterministic tier, though shared state, wall-clock time, randomness and test order can still make unit tests flaky (see lesson 05). - Integration tests (minutes): Testcontainers or short-lived PostgreSQL and Redis instances test real schema migrations, SQL queries, serialization and message consumers.
- End-to-end and performance smoke tests (many minutes): Playwright, Cypress or k6 drive the whole system, often in a staging environment. They are the most expensive tier and the most exposed to network delays and flakiness.
name: Staged Fail-Fast Pipeline
# Assumes a Makefile with the targets lint, typecheck, test-unit and
# test-integration (like lesson 02's Makefile, with the tests split in
# two). Each job also needs your toolchain: add a setup step such as
# actions/setup-python and a locked install before the make step.
# Run on pull requests, and on main after merge. Triggering on every
# push as well would run each pull-request commit twice.
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
# Stage 1: quick static checks gate everything else
lint-and-types:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Verify syntax, code formatting and types
run: make lint typecheck
# Stage 2: unit tests
unit-tests:
needs: lint-and-types
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Run fast unit tests (pytest --junitxml=reports/junit-unit.xml)
run: make test-unit
- name: Upload the unit test report
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: junit-unit
path: reports/junit-unit.xml
# Stage 3: slower integration tests, only if unit tests pass
integration-tests:
needs: unit-tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Run integration tests (writes reports/junit-integration.xml)
run: make test-integration
- name: Upload the integration test report
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: junit-integration
path: reports/junit-integration.xml03. Optimizing Pipeline Concurrency: Fan-Out and Caching Gates
Gating keeps expensive tests from running on broken code, but running everything one after another makes the pipeline slower than it needs to be. Combine gates with parallel jobs:
- Fan out within a tier: run independent checks of the same tier, such as JavaScript linting, Python linting and a dependency scan, as parallel jobs.
- Fan in before expensive stages: later stages, such as integration tests or image packaging, list the earlier jobs in
needs: [lint, typecheck, unit-tests], so they start only after all of those jobs succeed. - Selective execution with path filters: Changes that only modify documentation files (
*.md) or frontend assets can skip backend integration test suites using path filters, saving runner time. Take care with required checks: a workflow skipped by a path filter never reports, so its required checks stay pending and block the merge. Filter inside the job (a job skipped by anif:condition reports success) when the check is required. - Cancel outdated pull request runs: Enable concurrency groups that cancel in-flight runs for a pull request when a new commit arrives (
cancel-in-progress: ${{ github.event_name == 'pull_request' }}), freeing runner capacity. Do not cancel builds ofmain, so every commit onmaingets a result.
In a monorepo, tools such as Nx and Turborepo can go further with affected-project analysis (a form of test impact analysis): they compare the changed files with the dependency graph of the projects and run checks only for the projects the change can affect. In large monorepos this can cut cycle times substantially; how much depends on how often a change touches shared code, so measure before and after.
04. Check your understanding
05. Apply it
In your own workflows:
- Gate with static checks first: run formatters, linters and syntax checks as stage 1, so simple mistakes fail within minutes.
- Require type checks and unit tests before integration: start databases and containers for integration tests only after the unit tests have passed.
- Run independent checks in parallel: Parallelize linters, static analyzers, and unit tests within their respective tiers to shorten total wall-clock time.
- Use path filters where it is safe: skip heavy workflows for docs-only changes (
docs/**,*.md), but only workflows whose checks are not required; filter required ones inside the job. - Cancel outdated PR runs: set
cancel-in-progress: ${{ github.event_name == 'pull_request' }}so a new commit cancels the older run of the same pull request, while runs onmainfinish.
06. Review prompt
Explain the fail-fast principle and estimate, for a pipeline you know, how much runner time and waiting a badly ordered check costs. Outline the five tiers of checks in this lesson from cheapest to most expensive, and describe how fan-out and fan-in dependency gating optimizes pipeline throughput.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.