Sub topic 1
Workflow building blocks: jobs, steps, and runners
Caching and build matrices both change how jobs and steps behave. This chapter sets out the vocabulary and the execution model they rely on. A cache only makes sense once you know why a job starts with nothing on disk. A matrix only makes sense once you know what a job is.
Prerequisites
This chapter assumes you can do the following:
- Use Git and GitHub: push a branch, open a pull request, and read the Actions tab of a repository.
- Read and write YAML, including nested mappings, lists, and multi-line strings.
- Add a file under
.github/workflows/and know that GitHub runs it automatically. - Run a package manager such as npm, pip, or Cargo and tell an install command from a test command.
You do not need to know anything about caching or matrices yet.
The four layers
A GitHub Actions setup has four layers that nest inside one another:
| Layer | What it is | Where it is defined |
|---|---|---|
| Workflow | One YAML file that describes an automated process | `.github/workflows/*.yml` |
| Job | A group of steps that run together on one machine | Under the `jobs:` key |
| Step | A single command or a reusable action | Under a job's `steps:` list |
| Runner | The machine that executes one job | Chosen by the job's `runs-on:` key |
A repository can hold many workflows. Each workflow has one or more jobs. Each job has an ordered list of steps and runs on exactly one runner.
A minimal workflow, annotated
The following workflow has two jobs. The second job waits for the first one.
name: CI
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run lint
test:
needs: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testThe pieces work as follows:
nameis the label shown in the Actions tab.onlists the events that start the workflow. Here, any push and any pull request activity.jobsis a mapping. The keyslintandtestare job IDs. Other parts of the file refer to a job by its ID.runs-onselects the runner type.needs: lintmakestestwait untillintsucceeds.stepsis an ordered list. Steps within a job run one after another, never in parallel.
Jobs
By default, jobs in a workflow run in parallel. Each job is independent unless you say otherwise. The needs key creates an ordering. You can give it a single job ID or a list of IDs.
If a job listed in needs fails or is cancelled, the dependent job is skipped. This is the default behavior. A job can override it with an if: condition, such as if: always().
Parallel jobs finish sooner overall, but they also multiply the setup work. If both lint and test run npm ci, the dependencies are downloaded twice, on two machines. This duplication is the main reason to cache dependencies, and later chapters return to it.
Runners
A runner is a machine that picks up one job, runs it, and then is discarded. The runs-on key chooses the type. GitHub-hosted runners come in three operating system families:
ubuntu-latestfor Linuxwindows-latestfor Windowsmacos-latestfor macOS
The -latest labels move over time as GitHub updates its images. If your build needs a specific operating system version, use an explicit label such as ubuntu-22.04 instead. You can also register your own machines as self-hosted runners and target them by label. This tutorial uses GitHub-hosted runners.
> Every GitHub-hosted job starts on a clean virtual machine.
The runner has common tools preinstalled, but it has none of your project's files and none of your project's dependencies. Whatever one job downloads or builds is gone when the job ends. The next job, or the next run of the same workflow, starts from the same clean image.
This is the central fact for the rest of the tutorial. A cache is a way to carry selected files from one clean machine to the next on purpose.
Steps
A step does one of two things, and it must do exactly one of them:
run:executes a shell command on the runner.uses:runs a reusable action, which is a packaged unit of logic that someone else, or you, wrote and published.
The reference after uses: follows the form owner/repo@version. In actions/checkout@v4, the owner is actions, the repository is checkout, and v4 is a tag that selects a major version.
Actions take inputs through with:. In actions/setup-node@v4, the input node-version picks the Node.js release to install.
Common step keys are listed below:
| Key | Purpose |
|---|---|
| `name` | A readable label for the log |
| `id` | A handle so later steps can read this step's outputs |
| `if` | A condition that decides whether the step runs |
| `env` | Environment variables for this step |
| `working-directory` | The folder in which a `run:` command executes |
| `with` | Inputs passed to an action |
Checking out the code
A new runner does not contain your repository. The first step of almost every job is actions/checkout@v4. Without it, commands such as npm ci fail because there is no package.json in the working directory. This is the most common error in a first workflow, and the message usually points at a missing file.
Passing data between steps and jobs
Steps in one job can pass small values to each other through the file named by the GITHUB_OUTPUT environment variable. Writing name=value into that file sets an output on the step. A later step reads it through the steps context, using the step's id.
Jobs cannot see each other's filesystems, so they use job-level outputs instead. Those outputs are strings, and they are meant for small values.
name: Outputs demo
on: [push]
jobs:
version:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.read.outputs.tag }}
steps:
- id: read
run: echo "tag=v1.2.3" >> "$GITHUB_OUTPUT"
use-it:
needs: version
runs-on: ubuntu-latest
steps:
- run: echo "Building ${{ needs.version.outputs.tag }}"The flow has three parts:
- The step with
id: readwritestag=v1.2.3into$GITHUB_OUTPUT. - The
versionjob exposes that value asoutputs.tag. - The
use-itjob declaresneeds: version, so it can readneeds.version.outputs.tag.
The same mechanism lets a job produce a matrix as a JSON string. A later chapter builds on this idea.
Expressions and contexts
The ${{ ... }} syntax evaluates an expression before the step runs. Inside it you can read contexts, which are named bundles of data. Four contexts are used throughout this tutorial:
githubholds event and repository details, such asgithub.ref.runnerdescribes the machine, for examplerunner.os, which isLinux,Windows, ormacOS.stepsholds the outputs of earlier steps in the same job.needsholds the outputs of jobs this job depends on.
Later chapters use runner.os and hashFiles(...) to build cache keys, and matrix to vary a job.
Common errors
These mistakes come up repeatedly when people write their first multi-job workflow:
- Forgetting checkout. The job starts empty, so project files are missing.
- Expecting files to cross jobs. A build output from one job does not exist in another. Use job outputs for small values. Use artifacts for files, which a later chapter explains.
- Assuming jobs run in order. Without
needs, jobs run at the same time and in no guaranteed order. - Mixing
runandusesin one step. A step takes one or the other. - Indentation mistakes. A step key such as
with:placed at the wrong level is read as a different key, and the workflow fails validation or ignores your input. - Relying on
-latest. A runner image update can change tool versions between two runs of the same commit.
Why this matters for caching and matrices
Two properties of this model drive the rest of the tutorial:
- A runner starts clean, so every job pays for installing dependencies unless you restore them from somewhere. That is the case for caching.
- A job is the unit that gets a runner. If you want the same steps to run on several operating systems or language versions, you need several jobs. Writing each by hand is repetitive. A matrix generates them from one definition.
Exercise
A workflow has jobs build and test, with no needs key on either. The build job runs npm run build, which writes a dist/ folder. The test job then tries to read dist/. Why does it fail, and what are two separate problems in this setup?
Show answer
The two jobs run on different runners, and each runner starts clean, so dist/ created in build does not exist in test. Problem one is ordering. Without needs: build, the jobs run in parallel, so test may start before build has produced anything. Problem two is isolation. Even with needs: build, the filesystem is not shared. To fix it, either run the build and test steps in the same job, or upload dist/ as an artifact in build and download it in test.
Exercise
What does ${{ needs.version.outputs.tag }} require in the job where it is used, and in the job named version?
Show answer
The using job must list version in its needs key. The version job must declare outputs: tag: ... at the job level, pointing at a step output such as ${{ steps.read.outputs.tag }}. The step itself needs an id and must write tag=... to the file named by $GITHUB_OUTPUT.