Sub topic 4
Jobs, steps, actions and runners
This chapter assumes you can read basic YAML, know what a pull request is, and have already seen a workflow file under .github/workflows. It explains how the pieces inside that file relate to each other, because most early mistakes come from mixing them up.
The four building blocks
A workflow is one YAML file. It declares when it runs (the triggers) and what work to do. The work is split into jobs. Each job is made of steps. A step either runs a shell command or calls an action, which is a reusable unit of work published by someone else (or by you). Every job runs on a runner, which is the machine that executes it.
Jobs
Jobs are the top-level units under the jobs: key. By default, jobs run in parallel, and each one starts on a fresh runner. Nothing carries over between jobs: not files, not installed packages, not environment variables. If a later job needs the output of an earlier one, you must declare the order with needs and pass files through artifacts (covered in a later chapter).
jobs:
lint:
runs-on: ubuntu-latest
steps:
- run: echo "linting"
test:
needs: lint
runs-on: ubuntu-latest
steps:
- run: echo "testing"Here test waits for lint. If lint fails, test is skipped.
Steps
Steps inside one job run in order, on the same runner, and share its file system. That is why a checkout in step one makes the code available to the test command in step four. If a step fails, the remaining steps are skipped by default, unless they carry an if: condition that says otherwise.
A step uses one of two keys:
| Key | Meaning | Example |
|---|---|---|
| `run` | Executes a shell command | `run: npm test` |
| `uses` | Calls an action | `uses: actions/checkout@v4` |
Actions
An action is a packaged step. Instead of writing the shell commands to download and install Node.js yourself, you write uses: actions/setup-node@v4. The part after @ is a version reference: a tag, a branch, or a commit SHA. Settings go under with:.
- uses: actions/setup-node@v4
with:
node-version: 20Pin to a version you trust. A tag such as v4 can move to newer code within that major version. A full commit SHA cannot move, which makes it the safest choice for third-party actions.
Runners
runs-on selects the runner. GitHub-hosted runners are virtual machines that GitHub creates for a job and discards afterwards. Common labels are ubuntu-latest, windows-latest and macos-latest. You can also register your own machine as a self-hosted runner, but then you are responsible for its security and cleanup.
Because each job gets a clean machine, a job always has to check out the code and install its tools. Forgetting this is the most common reason for a "command not found" or "no such file" error in a new workflow.
Putting it together
name: Tests
on:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testOne workflow, one job, four steps on one Ubuntu runner. Two steps use actions and two run shell commands.
Exercise
Exercise
A workflow has two jobs, build and test, with no needs key. build runs npm ci. test runs only npm test after checkout, and it fails with "jest: not found". Why, and what are two ways to fix it?
Show answer
The jobs run in parallel on separate fresh runners, so the dependencies installed by build do not exist on the test runner. Fix it by adding npm ci (with the Node.js setup) to the test job, or by merging both into one job with the steps in order. Adding needs: build alone would not help, because the files still do not carry over between jobs.