Sub topic 3
Jobs, steps, and runners
This chapter assumes you can read YAML and have a workflow file with a trigger, as covered in the previous two chapters. It explains how a workflow is divided into jobs, how steps run inside a job, and where the work physically happens.
The working model
A workflow contains one or more jobs. A job is a group of steps that runs on one runner. A runner is a machine, usually a fresh virtual machine, that GitHub starts for the job and discards afterward. A step is a single task, either a shell command (run) or a reusable action (uses).
Three rules explain most behavior:
- Steps in one job run in order and share the same filesystem and environment.
- Jobs run in parallel by default, each on its own clean runner.
- Because runners are separate machines, a job cannot see files created by another job unless you pass them explicitly.
Choosing a runner
The runs-on key picks the runner. GitHub-hosted runners use labels such as these:
| Label | Operating system |
|---|---|
| `ubuntu-latest` | Linux |
| `windows-latest` | Windows |
| `macos-latest` | macOS |
Linux is the cheapest and fastest choice, so use it unless your build needs another operating system. A self-hosted runner is a machine you manage yourself. You would choose one to reach a private network or to use special hardware. It also means you must keep that machine patched and secured.
Ordering jobs with needs
Deployment requires ordering. You want to deploy only after the build and tests pass. The needs key makes a job wait for others and skips it if any of them fail.
name: Build then deploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Building and testing"
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Deploying"The deploy job starts only when build succeeds. You can list several jobs, for example needs: [build, lint].
Steps in detail
Each step can have a name, which makes logs easier to read. A step uses either run or uses, not both.
steps:
- name: Get the code
uses: actions/checkout@v4
- name: Show the working directory
run: pwd
- name: Install dependencies
run: npm ci
working-directory: ./appEvery job starts with an empty workspace. The first step is almost always actions/checkout, which copies your repository onto the runner. Without it, commands like npm ci fail because no files exist.
Common errors
- Missing checkout. Commands fail with "file not found" or "no package.json". Add
actions/checkoutas the first step. - Expecting files to carry over between jobs. A file built in
buildis gone indeploy. Pass it as an artifact, which a later chapter covers. - Forgetting
needs. Without it,buildanddeploystart at the same time, and deploy may ship untested code. - Assuming a failed step is harmless. By default a failing step stops the job. Later steps are skipped unless they use
if: always().
Exercise
A workflow has two jobs, test and deploy. The deploy job runs ls dist, but the dist folder was created by test and the command fails. Give two reasons the workflow is wrong, and say what to add to fix each.
Show answer
First, the jobs run on separate runners, so dist does not exist in deploy. Fix it by uploading dist as an artifact in test and downloading it in deploy. Second, without needs: test, the two jobs run in parallel, so deploy may start before dist exists and before tests pass. Fix it by adding needs: test to the deploy job.