Design a pipeline that fails fast
In this task you'll arrange a pipeline so the quickest checks run first and a failure stops the rest.
A pipeline is a set of jobs joined by needs. Jobs without needs run at the same time; a job with needs: test waits for test and is skipped if test fails. Put the cheapest, most likely to fail checks first (lint, unit tests), the slow ones later (build, integration tests), and deployment last, behind an if: that limits it to the main branch.
Two settings stop waste. concurrency with cancel-in-progress: true cancels the older run when you push again to the same branch, and timeout-minutes stops a stuck job long before GitHub's six-hour limit. Fast feedback matters more than a clever pipeline: aim for an answer on a pull request within about ten minutes.
Example. SS Kaiser's shop pipeline used to run lint, tests, an image build and a deployment one after another in a single 25-minute job. Splitting it into lint and test side by side, then build, then deploy on main only, gave developers an answer in four minutes.
Before you start
You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 30 minutes.
Where to look
- Repository
- .github/workflows/pipeline.yml
- Actions tab
Steps
- Create a public repository with a tiny Python app:
app.pywith anadd(a, b)function,test_app.pywith a pytest test for it, andrequirements-dev.txtcontainingpytest. - Add
.github/workflows/pipeline.ymlfrom Commands: lint and test side by side, build after both, deploy after build on main only. - Push to a branch and open a pull request. In the run's graph, see lint and test start together and deploy shown as skipped.Skipped is correct: the if: limits deploy to main.
- Break the test on purpose and push. See build and deploy skipped because a job they need failed.
- Push twice within a few seconds and see the first run cancelled by the second.That's concurrency with cancel-in-progress.
- Fix the test and merge. On main, deploy runs.
Commands
name: pipeline
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v5
- run: python -m py_compile app.py test_app.py
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with: { python-version: '3.13' }
- run: pip install -r requirements-dev.txt && pytest -q
build:
needs: [lint, test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- run: tar czf app.tgz app.py
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "Deploying ${{ github.sha }}"
Watch out for
- Cost
- Free in a public repository. Private repositories use a monthly allowance of minutes, so fast pipelines also save money.
- Security
- Set
permissions: contents: readat the top and widen it only on the jobs that need more. - Common mistakes
- One giant job that hides which stage failed, and cancelling runs on main, where every commit's result matters.
Clean up
Keep the repository for the next tasks in this section; delete it at the end.
Words to know
- Job
- A group of steps that runs on one runner.
- needs
- Makes a job wait for, and depend on, other jobs.
- concurrency
- A group name that allows only one run at a time, optionally cancelling the older one.
- Fail fast
- Running quick checks first so a broken change stops early and cheaply.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On