Sub topic 1
A quick recap of workflows, jobs and steps
This chapter refreshes the three building blocks of GitHub Actions. Every later chapter on reusable workflows relies on them, so it is worth checking that your mental model matches how GitHub runs them.
Prerequisites
This chapter assumes you have the following:
- Basic YAML syntax: mappings, lists and indentation.
- Familiarity with GitHub repositories and pull requests.
- Experience writing a simple GitHub Actions workflow with events, jobs and steps.
- Awareness of GitHub secrets and the
GITHUB_TOKEN.
The three levels
A workflow is a YAML file in .github/workflows/. It describes an automated process and the events that start it. A job is a group of steps that runs on one runner, which is a fresh virtual machine or container. A step is a single task inside a job. It is either a shell command (run:) or an action (uses:).
The levels nest like this:
A worked example
The workflow below has two jobs. The test job waits for build because of needs.
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- run: npm run build
test:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- run: npm testThe on: block lists the events that trigger the run. Here a push to main or any pull request starts it.
Rules that matter for reuse
Three facts from this recap explain most of what follows in the tutorial.
- Jobs are isolated. Each job gets its own runner. The
testjob above has to check out the code and install dependencies again, because the files frombuildare not there. Data moves between jobs only through job outputs, artifacts or caches. - Steps share one runner. Steps in the same job see the same file system and environment. A step can pass a small value to later steps by writing
name=valueto the file named byGITHUB_OUTPUT. - Jobs run in parallel by default. Only
needscreates an order. It also lets a job read the outputs of the jobs it depends on.
The duplicated checkout and setup steps in the example show the problem that reuse solves. When ten repositories copy the same ci.yml, a fix has to be made ten times.
Contexts you will see again
Expressions in ${{ ... }} read from contexts. These are the ones you will meet in the next chapters:
| Context | What it holds | Example |
|---|---|---|
| `github` | Event and repository data | `github.sha` |
| `secrets` | Secrets available to the run | `secrets.NPM_TOKEN` |
| `needs` | Outputs of jobs this job depends on | `needs.build.outputs.name` |
| `matrix` | The current matrix values | `matrix.node` |
Check your understanding
Exercise
In the example, the test job repeats actions/checkout and npm ci. Why can't it reuse the files that the build job already created?
Show answer
Each job runs on its own fresh runner, so the file system of the build job is not available to the test job. Files have to be shared through artifacts or a cache, and values through job outputs.
Exercise
What single keyword makes the test job wait for the build job?
Show answer
needs. Without it, the two jobs run in parallel.