Sub topic 4
Jobs: units of work
This chapter assumes you can read YAML, know what a branch and pull request are, and have seen a basic workflow file with a trigger block. It covers what a job is, how jobs run, and the mistakes people commonly make with them.
What a job is
A job is a named group of steps that runs on one runner. A workflow contains one or more jobs under the jobs key. Each job ID (such as test) is a key in that map.
name: CI
on:
push:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lint
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm testThis workflow has two jobs, lint and test. Because neither declares needs, they start at the same time on separate machines.
How jobs execute
Each job gets a fresh runner. Nothing carries over from one job to another: not files, not installed tools, and not environment variables. This is why both jobs above run actions/checkout and npm ci themselves.
Steps inside one job share the same filesystem and run in order. If a step fails, the remaining steps are skipped by default and the job fails.
The table below summarizes the difference.
| Property | Job | Step |
|---|---|---|
| Runs on | Its own runner | The job's runner |
| Default ordering | Parallel | Sequential |
| Shares filesystem with siblings | No | Yes |
| Can be skipped with `if` | Yes | Yes |
Naming and identifying jobs
The job ID must start with a letter or underscore and contain only letters, digits, -, and _. The optional name key sets the label shown in the GitHub UI. Use the ID when you refer to a job elsewhere, such as in needs.
jobs:
unit-tests:
name: Unit tests (Node 20)
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- run: echo "Running tests"The timeout-minutes key stops a stuck job early. Without it, a job can run for up to 360 minutes, which wastes runner time.
Common errors
- Assuming shared state. A file created in one job does not exist in another. Use artifacts to pass files between jobs.
- Missing
runs-on. Every job needs it, unless the job calls a reusable workflow. - Invalid job IDs. An ID that starts with a digit or contains spaces fails validation.
- Expecting order without
needs. Jobs run in parallel unless you declare a dependency. Ordering is covered in a later chapter. - Duplicated setup. Repeating checkout and install in every job is correct, but it costs time. Caching reduces that cost.
Exercise
A workflow has two jobs, build and test. The build job writes dist/app.js. The test job runs cat dist/app.js and fails with "No such file". Why, and what is the general fix?
Show answer
Each job runs on a separate fresh runner, so the file written in build does not exist in test. Either run the build inside the test job, or upload dist/ as an artifact in build and download it in test (with needs: build so that the order is guaranteed).