Sub topic 3
Workflow anatomy: events, jobs, and steps
This chapter explains the three building blocks of a GitHub Actions workflow file. It assumes you can read basic YAML, know the Git and pull request flow, and have a project with a linter and formatter. Later chapters fill in the triggers and the full lint workflow. Here you learn how the pieces fit together.
The workflow file
A workflow is a YAML file stored in .github/workflows/ in your repository. GitHub reads every .yml or .yaml file in that folder. Each file is one workflow, and each workflow can run many times.
A workflow has three layers:
- An event starts the workflow. Examples are a pull request being opened or a push to a branch.
- A job is a group of steps that runs on one fresh virtual machine, called a runner.
- A step is a single task inside a job. It is either a shell command or a reusable action.
A minimal workflow
This file has all three layers. Save it as .github/workflows/hello.yml.
name: Hello
on:
pull_request:
jobs:
greet:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Files in this repo:"
- run: lsEach key has a job to do:
| Key | Meaning |
|---|---|
| `name` | The label shown in the Actions tab. Optional, but it helps. |
| `on` | The events that start the workflow. |
| `jobs` | A map of jobs. Each key (here `greet`) is the job ID. |
| `runs-on` | The type of runner machine for the job. |
| `steps` | An ordered list of steps in the job. |
| `uses` | Runs a reusable action, such as `actions/checkout@v4`. |
| `run` | Runs a shell command on the runner. |
How jobs and steps behave
Steps in a job run one after another, in the order you list them. They share the same machine and file system. A file that step 2 creates is visible to step 3.
Jobs are different. Each job gets its own clean runner. Files from one job do not carry over to another. By default, jobs run in parallel. You can force an order with needs, but lint workflows rarely need it.
This isolation explains a common first mistake. Every job that runs your linter must check out the code and install dependencies itself. Nothing is inherited.
jobs:
first:
runs-on: ubuntu-latest
steps:
- run: echo "hello" > note.txt
second:
runs-on: ubuntu-latest
steps:
- run: cat note.txtThe second job fails because note.txt does not exist on its runner.
Why checkout comes first
A runner starts empty. It does not contain your code. The actions/checkout action copies your repository onto the runner. Without it, npm run lint has no files to check. Put checkout as the first step in nearly every job.
After checkout, a lint job usually follows this order:
- Set up the language runtime, such as Node.js.
- Install dependencies.
- Run the lint command.
- Run the format check command.
Common errors
- Wrong folder. A file outside
.github/workflows/is ignored, and no error appears. - Bad indentation. YAML uses spaces, not tabs. A misaligned key can make a step part of the wrong job, or make the whole file invalid. Invalid files show an error in the Actions tab.
- Missing checkout. Commands fail with "file not found" or run against nothing.
- Assuming shared files between jobs. Each job starts clean.
Exercise
A workflow has two jobs, build and lint, with no needs key. The build job installs dependencies with npm ci. The lint job runs npm run lint but fails with "eslint: not found". What is the cause, and how do you fix it?
Show answer
Jobs run on separate, clean runners, so the lint job never received the dependencies installed in build. Fix it by adding the checkout, setup-node, and npm ci steps to the lint job itself, before npm run lint.