Sub topic 5
Anatomy of a pipeline: stages, jobs, and triggers
This chapter assumes you know basic Git (commits, branches, pull requests), can build and test a small application from the command line, and can read simple YAML. It explains the parts of a pipeline and how they fit together. The earlier chapters on building and testing supply the individual steps. Here you will see how those steps are organized.
The working model
A pipeline is an automated sequence of work that runs when something happens in your repository. It has four building blocks:
- A trigger is the event that starts a run, such as a push or a pull request.
- A stage is a phase of the process, such as build, test, or deploy.
- A job is a unit of work that runs on one machine. A job contains steps.
- A step is a single command or a reusable action inside a job.
A runner is the machine that executes a job. Each job usually gets a fresh runner, so nothing carries over from one job to the next unless you pass it on explicitly.
Triggers
A trigger decides which events start a 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.
Choose triggers on purpose. Running the full test suite on every pull request gives fast feedback before code is merged. Deploying only on pushes to main keeps unreviewed branches out of production.
Stages and jobs
Stages run in order. Jobs inside the same stage can run in parallel. If any job in a stage fails, later stages normally do not start. This is how a pipeline stops a bad change from reaching production.
Some tools, such as GitLab CI, have stages as an explicit keyword. GitHub Actions has no stage keyword. You get the same ordering by declaring dependencies between jobs with needs.
Reading a real pipeline
This workflow from the topic's examples builds and tests in one job, then deploys in a second job.
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/Map it to the model:
onis the trigger. Only pushes tomainstart this run.buildanddeployare jobs.runs-onpicks the runner.needs: buildmakesdeploywait forbuildand run only if it succeeds.- Each
usesorrunline is a step. environment: productionties the job to a named environment, which can require manual approval.
Because deploy runs on a new runner, it has no files from build. The upload and download artifact steps pass dist/ between them.
Common errors
- Assuming files persist between jobs. They do not. Pass them as artifacts.
- Forgetting
needs. Without it, jobs start in parallel, sodeploycould run before tests finish. - Using overly broad triggers. Deploying on every branch push wastes resources and risks releasing untested code.
- Putting everything in one long job. A single failure then hides which phase broke, and you cannot rerun just one part.
Exercise
In the workflow above, what happens to the deploy job if npm test fails in the build job, and why does deploy need the artifact steps?
Show answer
The deploy job never runs, because needs: build requires the build job to succeed. It needs the artifact steps because it runs on a fresh runner that has no files from the build job, so dist/ must be uploaded by build and downloaded by deploy.