Sub topic 1
How a workflow run is organized
When a workflow fails, the first job is to know which part of the run broke. This chapter gives you the working model of how GitHub Actions structures a run, so the failed-run page and its logs make sense in later chapters.
Prerequisites
This chapter assumes you have:
- Basic Git and GitHub usage: commits, pushes, and pull requests.
- The ability to read YAML.
- Some experience running commands in a shell.
- A repository with at least one GitHub Actions workflow.
The four levels: workflow, run, job, step
A workflow is a YAML file in .github/workflows/. It describes what to automate and when. An event, such as a push or a pull request, triggers it. Each trigger creates a run, which is one execution of the workflow.
A run contains one or more jobs. A job contains an ordered list of steps. A step is either a shell command (run:) or a reusable action (uses:).
Each job runs on a runner, which is a fresh virtual machine or container that GitHub provides. The runs-on key picks the runner type. Jobs do not share a file system. Steps inside one job do share it, so a file written in one step is visible in the next.
Here is a small workflow with one job and four steps. It is the baseline for the rest of this tutorial.
name: CI
on: [push, 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 testHow the pieces behave
Three rules explain most of what you will see on a failed run.
- Steps run in order. If a step fails, the later steps in that job are skipped by default.
- Jobs run in parallel by default. A job with
needs:waits for the jobs it names. If a needed job fails, the dependent job is skipped. - Each job gets its own runner. Nothing installed in one job exists in another unless you pass it along, for example through artifacts or the cache.
The table maps each level to what you will see in the interface.
| Level | Defined by | What you see when it fails |
|---|---|---|
| Workflow | A file in `.github/workflows/` | A red check on the commit or pull request |
| Run | One trigger event | A run page with a failed status |
| Job | A key under `jobs:` | A red entry in the job list |
| Step | An item under `steps:` | A red step with its log expanded |
A first look at a failure
In the baseline workflow, npm ci needs package.json and package-lock.json in the working directory. If your project keeps them in a subdirectory such as app/, the npm ci step fails. The npm test step is then skipped.
The skipped step is a symptom, not a second problem. This pattern repeats throughout the tutorial: one failing step often hides several downstream effects.
The fix tells the shell where to run the commands. It uses defaults.run.working-directory, which applies to run: steps only. It also points the cache at the lock file in that directory.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: app
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: app/package-lock.json
- run: npm ci
- run: npm testWhat to carry forward
- A run is one execution of a workflow, triggered by an event.
- A run holds jobs, and a job holds steps. Steps share a runner. Jobs do not.
- The first failed step stops its job, and later steps are skipped.
- A failed job can cause the jobs that
needs:it to be skipped.
Exercise
A workflow has two jobs, build and deploy, and deploy declares needs: build. The third step of build fails. What happens to the fourth step of build and to the deploy job, and why?
Show answer
The fourth step of build is skipped, because a step failure stops the remaining steps in that job by default. The deploy job is also skipped, because it needs build and build did not succeed.
Exercise
Your repository keeps its code in app/. You add defaults.run.working-directory: app, but a uses: actions/setup-node@v4 step with cache: npm still cannot find the lock file. Why, and what setting fixes it?
Show answer
working-directory applies only to run: steps, not to uses: steps. Set cache-dependency-path: app/package-lock.json on the setup-node step so the cache looks in the right place.