Sub topic 8
Ordering jobs with needs
This chapter assumes you can read YAML and know how a workflow file defines jobs, runners, and steps. It covers how to control the order in which jobs run and how to pass data between them.
Jobs run in parallel by default
Jobs in a workflow start at the same time unless you say otherwise. Each job gets its own fresh runner, so a job cannot see files or variables created by another job. The needs key adds an ordering rule: a job with needs waits until the listed jobs finish successfully.
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "building"
test:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "testing"needs accepts one job ID or a list, such as needs: [build, lint]. The job waits for every job in the list.
Fan-out and fan-in
A common shape runs independent jobs in parallel, then joins them in a final job.
jobs:
lint:
runs-on: ubuntu-latest
steps:
- run: echo "lint"
test:
runs-on: ubuntu-latest
steps:
- run: echo "test"
deploy:
needs: [lint, test]
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- run: echo "deploy"If a workflow uses a matrix, a job that depends on it waits for every matrix variation to finish.
What happens when a job fails
By default, a job is skipped if any job it needs failed or was skipped. Skipped jobs do not run their steps. The if key can change this. Use always() to run a job regardless of earlier results, or failure() to run only after a failure.
notify:
needs: [lint, test]
if: ${{ failure() }}
runs-on: ubuntu-latest
steps:
- run: echo "Something failed"Without a status function in the if, GitHub adds an implicit success() check. That is why a plain if: github.ref == 'refs/heads/main' still does not run after a failed dependency.
Passing data with job outputs
Because each job has its own runner, data must be passed explicitly. A step writes name=value to the file named by GITHUB_OUTPUT. The job maps that step output to a job output under outputs. A dependent job reads it through the needs context.
jobs:
version:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.meta.outputs.tag }}
steps:
- id: meta
run: echo "tag=v1.${{ github.run_number }}" >> "$GITHUB_OUTPUT"
show:
needs: version
runs-on: ubuntu-latest
steps:
- run: echo "Building ${{ needs.version.outputs.tag }}"The step needs an id so the job can refer to it. The show job can read needs.version.outputs.tag only because it lists version in needs.
Common errors
- Misspelled job ID.
needsuses the job key, not thename:display text. An unknown ID makes the workflow invalid. - Circular dependencies. If
aneedsbandbneedsa, the workflow fails validation. - Reading outputs without
needs. The value comes back empty, with no error. - Expecting files to carry over. Outputs hold small strings. Files need artifacts, which a later chapter covers.
- Skipped deploys after a failure. The implicit
success()is usually the cause. Add a status function only when you want that behavior to change.
Exercise
A workflow has jobs build, test, and deploy. You want deploy to run only after both build and test pass. You also want to use a value that build sets as a job output named tag. What do you write in deploy to wait for both jobs and to read the value?
Show answer
Add needs: [build, test] to deploy. Read the value with ${{ needs.build.outputs.tag }}. The build job must define outputs.tag, mapped from a step output written to GITHUB_OUTPUT.