Sub topic 1
Anatomy of a workflow file
Prerequisites
This chapter assumes you can read YAML, use Git branches and pull requests, and run commands in a shell. It also assumes you have used a build or test tool such as npm, pytest, or make. You don't need any prior GitHub Actions experience.
What a workflow file is
A workflow is a YAML file stored in the .github/workflows/ directory of a repository. GitHub reads every .yml or .yaml file in that directory and treats each one as a separate workflow. A file outside that directory is ignored.
A workflow answers three questions:
- When should it run? This is the trigger, set with
on. - Where should it run? This is the runner, set with
runs-on. - What should it do? This is the list of jobs, each made of steps.
A minimal example
Save this file as .github/workflows/ci.yml:
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testReading the file from top to bottom
| Key | Level | Purpose |
|---|---|---|
| `name` | Workflow | Label shown in the Actions tab. Optional. If you omit it, GitHub uses the file path. |
| `on` | Workflow | The events that start a run. Required. |
| `jobs` | Workflow | A map of job IDs to job definitions. Required. |
| `runs-on` | Job | The type of machine that runs the job. Required. |
| `steps` | Job | The ordered list of tasks in the job. Required. |
| `uses` | Step | Runs a reusable action. |
| `with` | Step | Passes inputs to an action. |
| `run` | Step | Runs a command in a shell. |
In the example, test is the job ID. The ID is the key you later use in other places, such as needs. A step can also have a name, which only changes how it appears in the logs.
How the pieces relate
One event starts one workflow run. A run contains one or more jobs. Each job runs on its own fresh runner, and the steps in a job run one after another on that same runner. Because steps share a runner, they share its file system. Jobs do not share one, so data must be passed between them explicitly. Later chapters cover how.
Common errors
- The workflow never runs. The file is in the wrong directory, or it has a different extension from
.ymlor.yaml. Check the path first. - The workflow appears in the list but shows a syntax error. This is usually bad indentation. YAML uses spaces, not tabs.
- Tests fail with "file not found". The job did not check out the code. A runner starts empty, so you need
actions/checkout. - The run does not start on a push. The branch does not match the
branchesfilter underpush.
Exercise
In the example workflow, which key identifies the job, and what happens if you remove the actions/checkout step before npm ci?
Show answer
The job ID is test, the key directly under jobs. Without actions/checkout, the runner has no copy of the repository, so npm ci fails because it cannot find package.json.