Sub topic 1
How a workflow is built: workflows, jobs and steps
To find slow parts of a pipeline, you first need to know which units GitHub Actions measures and schedules. This chapter covers those units: the workflow, the job and the step. Later chapters read their timings and speed them up.
Prerequisites
This chapter assumes you have:
- Basic YAML syntax: mappings, lists and indentation.
- Written or edited a GitHub Actions workflow file at least once.
- Used git and pull requests.
- Basic shell skills, such as running commands and reading their exit codes.
The three levels
A workflow is a YAML file in .github/workflows/. It defines when to run (the trigger) and which jobs to run. Each trigger event produces one workflow run.
A job is a group of steps that executes on one runner. A runner is a machine, usually a fresh virtual machine, that GitHub provides or that you host yourself. Each job gets its own runner.
A step is a single unit of work inside a job. It is either a shell command (run) or a reusable action (uses). Steps in a job run one after another on the same runner.
| Level | Runs where | Relation to siblings | Can fail independently |
|---|---|---|---|
| Workflow run | Many runners | Separate runs are independent | Yes |
| Job | One runner each | Parallel unless `needs` is set | Yes |
| Step | The job's runner | Sequential | Stops the job unless told otherwise |
A small example
This workflow has three jobs. lint and test have no dependency on each other, so they start together. deploy uses needs and waits for both.
name: ci
on:
pull_request:
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 test
deploy:
needs: [lint, test]
runs-on: ubuntu-latest
steps:
- run: echo "deploying"Notice that lint and test both run actions/checkout and npm ci. That repetition is deliberate. Nothing carries over between jobs unless you pass it explicitly.
Why the structure matters for timing
The structure decides what you can measure and what you can change.
- Steps add up. A job's execution time is roughly the sum of its steps plus a little setup and cleanup. To shorten a job, find the slow step.
- Jobs overlap. Jobs without
needsrun at the same time, so they do not add to each other. Jobs linked byneedsrun in sequence. - The run's duration follows the longest chain. If
linttakes 1 minute andtesttakes 4,deploystarts after about 4 minutes. Speeding uplintalone changes nothing. The chapter on the critical path covers this idea in detail. - Jobs may wait before they start. A job is created when its dependencies are met, but it begins only when a runner is free. That wait is not part of any step, so it never shows up in step timings.
Common errors
- Expecting files to persist between jobs. A later job starts on a clean runner. Files from
npm ciintestdo not exist indeploy. Use caching or artifacts, which later chapters cover. - Assuming order without
needs. Jobs are listed top to bottom in YAML, but that order means nothing. Withoutneeds, they run in parallel. - Expecting later steps to run after a failure. A failed step skips the remaining steps unless they use
if: always()or the failing step setscontinue-on-error: true. - Setting an environment variable in one step and expecting it in the next. Exports inside a
runblock end with that step. AppendNAME=valueto the file$GITHUB_ENVto pass a value to later steps in the same job. - Treating a job as a process. A job is a runner session. Every
runstep starts its own shell, so directory changes withcddo not carry over. Useworking-directoryinstead.
Where to look next
In the Actions UI, a run page shows each job as a box and each step as a line inside it. Those durations are the raw data for the rest of this tutorial. The next chapter shows how to read them.
Exercise
A workflow has jobs build (3 minutes), lint (1 minute) and test (5 minutes). test has needs: build. lint has no needs. Ignoring queue time, roughly how long does the run take, and does halving lint help?
Show answer
About 8 minutes. build and then test form a chain of 3 + 5 minutes, and lint runs alongside it. Halving lint does not shorten the run, because lint is not on the longest chain.
Exercise
A deploy job needs a dist/ folder created by the build job, but dist/ is missing when deploy runs. Why, and which common error is this?
Show answer
Each job runs on its own fresh runner, so files from build are not present in deploy. This is the error of expecting files to persist between jobs. Pass the folder explicitly, for example as an artifact, or rebuild it in deploy.