Sub topic 15
Passing data between jobs
This chapter assumes you can read a workflow file, know that each job runs on its own runner, and understand how needs orders jobs. It builds on step outputs, which you saw in the previous chapter.
Why jobs cannot share variables
Every job starts on a fresh runner. Environment variables, files, and shell state from one job do not exist in another. To move a small value, such as a version tag, you declare a job output. To move files, you use artifacts, which a later chapter covers.
The three links in the chain
A value travels through three links:
- A step writes
name=valueto the file named by$GITHUB_OUTPUT. - The job declares an
outputsentry that reads that step output through thestepscontext. - A dependent job reads the value through the
needscontext.
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 has id: meta, so the job can refer to it as steps.meta.outputs.tag. The show job lists version in needs. Without that, the needs context has no entry for version, and the expression evaluates to an empty string.
Common errors
- Missing
needs. The consuming job must list the producing job, directly or through another job in the chain. - Missing
idon the step. Without anid, the job output expression has nothing to reference. - Forgetting the job-level
outputsblock. Step outputs are not visible outside their job until you republish them. - Using
$GITHUB_ENVinstead. That file sets environment variables for later steps in the same job only. - Passing multiline or large values. Outputs are strings with size limits. For multiline values, use the heredoc syntax with a delimiter. For files or large data, use an artifact.
- id: notes
run: |
{
echo "text<<EOF"
git log -3 --pretty=%s
echo "EOF"
} >> "$GITHUB_OUTPUT"Security notes
Outputs are not secrets. If a job output contains a value that a secret masks, GitHub skips that output and shows a warning. Do not pass credentials this way. Also avoid putting untrusted values, such as a pull request title, directly into a run script through ${{ }}. Assign them to an environment variable and quote the variable in the script instead.
Matrix jobs
If a job uses a matrix, every matrix leg writes the same output name. The needs context sees only one value, from whichever leg finished last. Give each leg a distinct output name or use artifacts when you need per-leg results.
Exercise
A job named build has a step with id: meta that writes tag=v2 to $GITHUB_OUTPUT. A job named publish has needs: build and prints ${{ needs.build.outputs.tag }}, but it prints an empty line. What is missing, and how do you fix it?
Show answer
The build job has no outputs block, so the step output is not exposed outside the job. Add outputs: with tag: ${{ steps.meta.outputs.tag }} under the build job. The publish job then receives v2.