Sub topic 5
Anatomy of a pipeline: stages, jobs, and triggers
This chapter assumes you can read simple YAML, use Git branches and pull requests, and run a small application's tests from the command line. It explains how a CI/CD pipeline is structured, using GitHub Actions syntax for the examples. The same ideas apply in GitLab CI, Jenkins, CircleCI, and other tools, though the names differ.
The working model
A pipeline is an automated process that takes a change to the code and moves it through a series of checks and actions. Something starts it (a trigger). It is divided into units of work (jobs). Jobs may be grouped or ordered into phases (stages). Each job runs a list of steps on a machine called a runner.
| Concept | What it is | GitHub Actions term |
|---|---|---|
| Pipeline | The whole automated process for one run | Workflow run |
| Trigger | The event that starts a run | `on:` |
| Stage | A phase such as build, test, or deploy | No keyword. Expressed with `needs:` |
| Job | A set of steps that runs on one runner | `jobs:` |
| Step | One command or reusable action | `steps:` |
| Runner | The machine or container that executes a job | `runs-on:` |
The stage term is the one that varies most. GitLab CI has an explicit stages: list. GitHub Actions has no such keyword, so you create stages by making one job depend on another.
Triggers
A trigger is an event that starts a pipeline run. Common triggers are:
- A push to a branch.
- A pull request being opened or updated.
- A schedule, such as a nightly run.
- A manual start by a person.
- A tag being pushed, often used for releases.
- A call from another workflow or an external system.
The first example from this tutorial's plan shows the two most common triggers:
name: ci
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testThis workflow runs on every push to main and on every pull request. The pull request trigger gives feedback before code is merged. The push trigger verifies the result after the merge.
A trigger can also be narrowed. You can filter by branch, by tag, or by file path, so a documentation change does not run a full build:
on:
push:
branches: [main]
paths:
- "src/**"
- "package*.json"
schedule:
- cron: "0 3 * * *"
workflow_dispatch:Here schedule runs the workflow at 03:00 UTC every day, and workflow_dispatch adds a button to start it by hand.
Jobs and steps
A job is a group of steps that run together on the same runner. Steps in one job share a file system. They can pass files to each other just by writing them to disk.
Each job starts on a fresh runner. Nothing from a previous job is there. This is deliberate. It makes runs repeatable, and it means a leftover file on one machine cannot make a build pass by accident. It also means that to share files between jobs you must pass them explicitly, as artifacts (covered in the next chapter).
A step is either a shell command (run:) or a reusable action (uses:). In the example above, actions/checkout@v4 copies the repository onto the runner. Without it, the runner has no code. This step is the most commonly forgotten one when people write their first workflow.
Pin the version of reusable actions (@v4). An unpinned reference can change under you and break a pipeline that has not changed.
Stages and job ordering
Jobs in a GitHub Actions workflow run in parallel by default. To make one job wait for another, use needs:. This builds a dependency graph. The second example from the plan shows this:
name: ci-cd
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test
- run: npm run build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- run: ./scripts/deploy.sh dist/The deploy job has needs: build, so it starts only after build succeeds. If build fails, deploy is skipped. This is the main safety property of a pipeline: a failure at an early stage stops later stages.
The scripts/deploy.sh file is a placeholder for your own deployment script. The environment: production line links the job to a named environment. In the repository settings you can require manual approval for it, which is the difference between continuous delivery and continuous deployment (covered in later chapters).
Parallel jobs and fan-in
Jobs that do not depend on each other run at the same time. This shortens the total time. A job can wait on several others, which is called fan-in:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "run the linter here"
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "run unit tests here"
package:
needs: [lint, unit]
runs-on: ubuntu-latest
steps:
- run: echo "runs only if lint and unit both passed"The total time is roughly the slowest of lint and unit, plus the time for package, rather than the sum of all three.
A typical stage layout
Most pipelines follow a shape like this, ordered from fast and cheap to slow and expensive:
- Checkout and dependency install.
- Static checks such as linting and type checking.
- Build.
- Unit tests, then slower integration tests.
- Package and publish the artifact.
- Deploy to a test environment, then to production.
Ordering fast checks first is called failing fast. A typo caught by the linter in 20 seconds should not have to wait behind a 15-minute integration suite.
Status, failure, and conditions
Each step and job ends with a status: success, failure, cancelled, or skipped. By default a step that exits with a non-zero code fails its job, and the job's failure skips every job that needs it.
You can override this with conditions. The if: key decides whether a step or job runs:
steps:
- run: npm test
- name: Upload test report on failure
if: failure()
uses: actions/upload-artifact@v4
with:
name: test-report
path: reports/The if: failure() condition makes the upload step run only when an earlier step failed, which is when you need the report. The same pattern works at the job level, for example to deploy only from the main branch:
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "deploying"Common errors
- Forgetting checkout. The job runs on an empty runner and reports that files are missing.
- Expecting files to carry over between jobs. Each job gets a fresh runner. Upload and download an artifact instead.
- Assuming jobs run in order. Without
needs:, jobs start together. A deploy can run before the tests finish. - Triggers that loop. A workflow that pushes a commit can trigger itself. Pushes made with the default
GITHUB_TOKENdo not start new workflow runs, but pushes made with other credentials can. - Duplicate runs. A branch with an open pull request can run once for the
pushand again for thepull_requesttrigger. Limitpushtomain, as the examples do. - Unpinned or stale caches. A cache keyed only on the branch name can serve old dependencies. Key it on the lockfile hash, which
cache: npmdoes for you.
Reading a pipeline run
When a run fails, work from the top down. Find the first failed job, open its first failed step, and read that step's log. Later failures are usually consequences of the first one, and skipped jobs are expected after a failure. Then reproduce the failing command locally, for example npm ci && npm test. A pipeline that cannot be reproduced locally is hard to debug, so keep the real logic in scripts that you can run on your own machine, and keep the pipeline file as a thin layer that calls them.
Exercise
A workflow has two jobs, test and deploy, and no needs: key on either. The tests fail, but the deploy still runs. Why does this happen, and what is the one-line fix?
Show answer
Jobs run in parallel unless they declare a dependency, so deploy does not wait for test or care about its result. Add needs: test to the deploy job. Then deploy starts only after test succeeds and is skipped if it fails.
Exercise
Your build job produces a dist/ folder, and your deploy job cannot find it. Name the cause and describe how to fix it.
Show answer
Each job runs on a fresh runner, so files from the build job are gone when the deploy job starts. In the build job, add an actions/upload-artifact step for dist/. In the deploy job, add an actions/download-artifact step with the same artifact name, and set needs: build so the artifact exists before the download.