Sub topic 13
Passing Data Between Jobs with Job Outputs and needs
This chapter assumes you know the workflow structure (on, jobs, runs-on, steps), can write basic shell commands, and have seen step outputs written with GITHUB_OUTPUT.
Why jobs need a channel
Each job runs on its own fresh runner. A shell variable, a file in the working directory, or a GITHUB_ENV entry from one job does not exist in another. To hand a value to a later job, you use a job output.
A job output is a named value that a job declares under its outputs key. A dependent job reads it through the needs context. The needs context only contains jobs listed in the needs key of the reading job.
The three parts
- A step writes
name=valueto theGITHUB_OUTPUTfile and has anid. - The job maps that step output to a job output with
outputs. - A later job lists the first job in
needsand readsneeds.<job_id>.outputs.<name>.
name: outputs
on: push
jobs:
build:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.ver.outputs.version }}
steps:
- id: ver
run: echo "version=1.2.3" >> "$GITHUB_OUTPUT"
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Deploying ${{ needs.build.outputs.version }}"The outputs expression in build uses the steps context. The deploy job uses the needs context. Both are evaluated by GitHub before the shell sees the text.
Behavior to know
needsalso sets order.deploywaits forbuildto finish.- Outputs are strings. A number or boolean arrives as text, so compare with care. For example,
needs.build.outputs.ready == 'true'is safer than relying on coercion. - If the output expression refers to a step that did not run, the output is an empty string, not an error.
- Outputs from a matrix job are not merged. Several matrix runs writing the same output name overwrite each other, and the result is unreliable.
- Do not put secrets in job outputs. GitHub redacts values it detects as secrets and skips them.
- Output size is limited, so use artifacts for large data or files.
Common errors
- Empty value in the later job. Typical causes are a missing
idon the step, a mismatch between the step output name and the key read, or a job missing fromneeds. - Using
${{ needs... }}in a job that does not depend on it. The context has no entry for that job, so the result is empty. - Reading
needswhen the dependency was skipped or failed. The job does not run by default. Addif: always()or another status function if you must run anyway, and checkneeds.build.result.
notify:
needs: build
if: always()
runs-on: ubuntu-latest
steps:
- run: echo "build finished with ${{ needs.build.result }}"Exercise
A job test lists needs: [build]. The build job has a step with id: meta that writes tag=v2 to GITHUB_OUTPUT, but build has no outputs key. What does ${{ needs.build.outputs.tag }} give in test, and how do you fix it?
Show answer
It gives an empty string, because step outputs are not visible outside their job unless the job maps them. Add outputs: tag: ${{ steps.meta.outputs.tag }} to build, then read needs.build.outputs.tag in test.