Sub topic 3
Running jobs in sequence with needs
This chapter assumes you can read a workflow file and know the on, jobs, runs-on, and steps keys. It also assumes basic YAML. Here you will learn how needs orders jobs and how to combine parallel and sequential jobs in one workflow.
Jobs run in parallel by default
Each job in a workflow runs on its own fresh runner. A runner is the machine that executes the job. Because jobs share nothing, GitHub Actions starts every job at the same time unless you add ordering rules.
That default is fast, but it fails when one job depends on another. A deploy job should not start until the build is done. The needs keyword expresses that dependency.
Ordering jobs with needs
Add needs to a job and give it the ID of the job that must finish first. The job waits until that job completes successfully.
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "Building"
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Deploying"Use the job ID, which is the key under jobs. Do not use the optional name: field. For several dependencies, use a list such as needs: [lint, test].
Fan-out and fan-in
Jobs with no needs start together. This is fan-out. A job that lists several jobs in needs waits for all of them. This is fan-in.
name: CI
on: [push]
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
build:
needs: [lint, test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run build
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Deploying"lint and test have no needs, so they run at the same time. build waits for both. deploy waits for build. Total time is the slowest of lint and test, plus build, plus deploy.
Each job repeats actions/checkout and npm ci. That is required, because a fresh runner starts empty.
What happens when a job fails
By default, a job runs only if every job in its needs succeeded. If lint fails in the example above, build is skipped, and so is deploy. A skipped job also blocks the jobs that need it, so the skip spreads down the chain.
The job test still runs to completion, because it does not depend on lint. Failure stops only the jobs downstream of the failed one.
Common errors
| Symptom | Likely cause |
|---|---|
| Workflow rejected before it starts | `needs` names a job ID that does not exist, often because you used the display `name:` instead |
| Workflow rejected with a cycle error | Job A needs B and B needs A, directly or through other jobs |
| Job shows as skipped | An upstream job failed, was skipped, or was cancelled |
| Job cannot find files from an earlier job | Jobs run on separate runners, so files are not shared automatically |
The dependency graph must be acyclic, meaning no chain of needs may lead back to the job that started it.
Sharing data between jobs
Because runners are separate, needs orders jobs but does not copy anything between them. To pass a small value, write it to $GITHUB_OUTPUT in a step, expose it under the job's outputs, and read it through the needs context.
jobs:
prepare:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.ver.outputs.version }}
steps:
- id: ver
run: echo "version=1.2.3" >> "$GITHUB_OUTPUT"
publish:
needs: prepare
runs-on: ubuntu-latest
steps:
- run: echo "Publishing version ${{ needs.prepare.outputs.version }}"For files, use actions/upload-artifact in the first job and actions/download-artifact in the later one.
Exercise
A workflow has jobs a, b, and c. Job c has needs: [a, b]. Job a fails while b succeeds. Which jobs run, and what is the state of c?
Show answer
Jobs a and b both start at once, since neither has needs, and b finishes successfully. Job c is skipped, because by default a job runs only when all jobs in its needs succeeded.
Exercise
You write needs: Build App where Build App is the name: of a job whose ID is build. What happens, and how do you fix it?
Show answer
The workflow is rejected because no job has the ID Build App. Change it to needs: build, using the key under jobs.