Sub topic 1
Jobs and runners: a quick refresher
This chapter reviews how jobs and runners work in GitHub Actions. Later chapters build on it to control the order in which jobs run with needs.
Prerequisites
This chapter assumes you already have:
- Basic YAML syntax, including maps, lists, and indentation.
- A repository on GitHub with a workflow file in
.github/workflows. - Familiarity with the
on,jobs,runs-on, andstepskeys of a workflow.
The pieces of a workflow
A workflow is a YAML file that GitHub runs in response to an event, such as a push. A workflow contains one or more jobs. A job is a group of steps that run together. A step is a single task. It either runs a shell command with run or calls a reusable action with uses.
The runs-on key selects the runner for a job. A runner is the machine that executes the job. With a GitHub-hosted runner, you pick an image such as ubuntu-latest, windows-latest, or macos-latest. GitHub starts a virtual machine from that image for the job.
Steps share a machine, jobs do not
The key rule is this: steps in one job run one after another on the same runner. Jobs each get their own fresh runner. Two consequences follow.
- Steps in the same job can share files on disk and environment variables set for the job.
- Jobs cannot see each other's files, installed tools, or environment. Anything one job creates is gone when its runner is cleaned up, unless you pass it on explicitly.
That is why the examples in this tutorial repeat actions/checkout and npm ci in every job. A new runner starts with an empty workspace.
name: Refresher
on: [push]
jobs:
first:
runs-on: ubuntu-latest
steps:
- run: echo "hello" > note.txt
- run: cat note.txt
second:
runs-on: ubuntu-latest
steps:
- run: cat note.txtIn first, the second step finds note.txt because both steps share one runner. In second, cat note.txt fails with "No such file or directory". That job runs on a different runner, where the file never existed.
What a fresh runner costs you
Each job pays a start-up cost. The runner must be provisioned, and the job must check out code and install dependencies again. This has two practical effects:
- Splitting work into many small jobs adds repeated setup time.
- Splitting work lets independent parts run at the same time and makes failures easier to locate.
Choosing where to cut a workflow into jobs is a design decision. The rest of this tutorial gives you the tools to make it: ordering, parallelism, and ways to pass data and files across the job boundary.
Common errors at this level
| Symptom | Likely cause |
|---|---|
| A file created in one job is missing in another | Jobs run on separate runners. Use an artifact. |
| A tool installed in one job is "command not found" in another | The install happened on a different runner. Install it in each job that needs it. |
| An environment variable set with `export` in one job is empty in another | Environment is per job. Pass values with job outputs. |
| `ls` shows an empty directory in a new job | Checkout is not automatic. Add `actions/checkout`. |
Where this leads
By default, GitHub Actions starts every job in a workflow at the same time, each on its own runner. The next chapter looks at this default. The following chapters then show how needs orders jobs, and how outputs and artifacts carry results across runners.
Exercise
A workflow has two jobs, build and package. The build job runs npm run build, which writes files to dist/. The package job runs ls dist and fails with "No such file or directory". Why does it fail, and what is the first thing you should check about how data moves between the jobs?
Show answer
The two jobs run on separate fresh runners, so the dist/ directory created in build does not exist in package. Nothing carries files across the job boundary by default. Check whether build uploads dist/ as an artifact and whether package downloads it. Passing files between jobs needs that explicit step.