Sub topic 6
Passing Values Between Jobs with Outputs
This chapter assumes you can read a workflow file and already know how needs orders jobs. You should also know that each job runs on its own runner.
Why jobs need a way to share values
Each job starts on a fresh runner. A shell variable, an environment variable, or a file written in one job does not exist in the next. If a later job needs a version number, a commit hash, or a flag, the first job has to hand it over explicitly.
Job outputs are the mechanism for small string values. For files, use artifacts, which a later chapter covers.
The three links in the chain
A value travels through three steps:
- A step writes
name=valueto the file whose path is in$GITHUB_OUTPUT. - The job maps that step output to a job-level
outputsentry. - A downstream job lists the producer in
needsand readsneeds.<job_id>.outputs.<name>.
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 }}"The step has id: ver, so the job can refer to it as steps.ver.outputs.version. The job then publishes that value under the name version. The publish job reads it through the needs context.
Rules that cause most errors
- The job must be in
needs. You can only read outputs from jobs you depend on, directly or through theneedslist. Ifpublishomittedneeds: prepare, the expression would be empty, because the job might not have finished. - Outputs are strings. A value like
trueor5arrives as the text"true"or"5". Compare it as text, for exampleneeds.prepare.outputs.flag == 'true'. - Keep them small. GitHub limits each output to 1 MB and all outputs of a job to 50 MB in total. Use artifacts for anything large.
- Do not put secrets in outputs. GitHub skips or masks outputs that contain secret values, so the downstream job may receive an empty string.
- A step ID is required. Without
id, there is nothing to reference from theoutputsmapping.
Multi-line values
The name=value form works for one line. For several lines, use a delimiter:
jobs:
notes:
runs-on: ubuntu-latest
outputs:
changelog: ${{ steps.log.outputs.changelog }}
steps:
- id: log
run: |
{
echo "changelog<<EOF"
echo "- Fixed login bug"
echo "- Updated docs"
echo "EOF"
} >> "$GITHUB_OUTPUT"The text between changelog<<EOF and the closing EOF becomes the value. Choose a delimiter that cannot appear in the content itself.
Using outputs in conditions
Outputs work in if expressions on a job, so a producer can decide whether later jobs run:
jobs:
check:
runs-on: ubuntu-latest
outputs:
docs_only: ${{ steps.diff.outputs.docs_only }}
steps:
- id: diff
run: echo "docs_only=true" >> "$GITHUB_OUTPUT"
build:
needs: check
if: ${{ needs.check.outputs.docs_only != 'true' }}
runs-on: ubuntu-latest
steps:
- run: echo "Building"Here build is skipped when check reports a docs-only change. Remember that a skipped job also causes its own dependents to be skipped, unless they override that with a status function.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Value is empty in the downstream job | Producer missing from `needs`, wrong step `id`, or a typo in the output name |
| Output exists in the step but not in `needs` | The job-level `outputs` mapping is missing |
| `if` comparison always false | Comparing a string output to a boolean, such as `== true` |
| Value is blank when it should hold a secret | GitHub withheld the secret-bearing output |
Exercise
A job build has a step with id: meta that runs echo "tag=v2" >> "$GITHUB_OUTPUT". The job has no outputs section. A job deploy with needs: build prints ${{ needs.build.outputs.tag }}. What does it print, and how do you fix it?
Show answer
It prints an empty string, because a step output is not visible to other jobs until the job maps it. Add outputs: to build with tag: ${{ steps.meta.outputs.tag }}. Then deploy prints v2.