Sub topic 1
Workflow building blocks: jobs, steps, and runners
Every caching and matrix feature in later chapters sits on four ideas: workflows, jobs, steps, and runners. This chapter gives the working model and the errors people hit most often.
Prerequisites
This chapter assumes you have:
- Basic Git and GitHub usage.
- Familiarity with YAML syntax, including indentation and lists.
- Some idea of what a CI workflow is, and how to add a file under
.github/workflows. - Basic use of a package manager such as npm, pip, or Cargo.
The four building blocks
A workflow is a YAML file in .github/workflows/. It starts when an event occurs, such as a push or a pull request.
A job is a group of steps that run together on one runner. A workflow has one or more jobs.
A step is a single task inside a job. It is either a shell command (run) or a reusable action (uses).
A runner is the machine that executes a job. runs-on selects it, for example ubuntu-latest or windows-latest. GitHub-hosted runners are fresh virtual machines. When the job ends, the machine is discarded.
A minimal workflow
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm testReading it from the top:
onlists the events that trigger the workflow.jobs.testdefines one job namedtest.runs-onasks for an Ubuntu runner.- The first step uses
actions/checkout@v4to copy your repository onto the runner. Without it, the runner has no code. - The last two steps run shell commands in order.
How jobs and steps behave
- Steps run in order, on the same machine. They share the file system, so a step can use files an earlier step created.
- Jobs run in parallel by default, each on its own machine. One job cannot see another job's files. To order jobs, add
needs: other-job. - A failing step stops the job. Later steps are skipped unless they use
if: always()or a similar condition.
The second point matters most for this tutorial. A fresh runner starts empty each time. Your npm ci downloads every dependency again on every run. Caching exists to fix that. A job also cannot pass files to a different job through the disk, which is why the tutorial later separates caches from artifacts.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `package.json` not found | The `actions/checkout` step is missing | Add it as the first step |
| Job B cannot find files job A built | Jobs run on separate runners | Use artifacts, or merge the work into one job |
| Job B starts before job A finishes | No dependency declared | Add `needs: job-a` |
| Workflow never starts | File is outside `.github/workflows`, or has a YAML error | Check the path and indentation |
Exercise
A workflow has two jobs, build and test, with no needs setting. build writes a file out.txt, and test tries to read it. Why does test fail, and what two things could you change?
Show answer
The jobs run in parallel on separate runners, so test has neither the file nor any guarantee that build has finished. You could merge both into one job so the steps share a file system, or add needs: build to test and pass the file between jobs as an artifact.