Sub topic 7
Returning outputs to the caller
This chapter assumes you can write a basic workflow, you know how a workflow_call workflow takes inputs, and you know that steps can write values to GITHUB_OUTPUT. Here you will pass data back from a called workflow to the workflow that called it.
Why outputs need three hops
A called workflow does not return values the way a function does. A value has to be passed through three levels before the caller can read it:
- A step writes the value to the
GITHUB_OUTPUTfile. - The job maps that step output to a job output.
- The
on.workflow_call.outputsblock maps the job output to a workflow output.
If any hop is missing, the caller sees an empty string.
Defining the outputs in the called workflow
This workflow builds an artifact name and exposes it.
# .github/workflows/build.yml
name: Build
on:
workflow_call:
outputs:
artifact-name:
description: Name of the build artifact
value: ${{ jobs.build.outputs.artifact-name }}
jobs:
build:
runs-on: ubuntu-latest
outputs:
artifact-name: ${{ steps.name.outputs.value }}
steps:
- id: name
run: echo "value=build-${{ github.sha }}" >> "$GITHUB_OUTPUT"Each hop uses a different context:
| Level | Where it is declared | How it reads the previous level |
|---|---|---|
| Step output | `echo "value=..." >> "$GITHUB_OUTPUT"` | Written by the shell command |
| Job output | `jobs.build.outputs` | `steps.name.outputs.value` |
| Workflow output | `on.workflow_call.outputs` | `jobs.build.outputs.artifact-name` |
The step needs an id, because the job output refers to it by that id. The workflow output's value must point at a job output, not directly at a step.
Reading the outputs in the caller
The caller reads the outputs through the needs context of the job that used the workflow. The calling job is the one with uses:, so you name that job in needs.
# .github/workflows/release.yml
name: Release
on:
push:
branches: [main]
jobs:
build:
uses: ./.github/workflows/build.yml
announce:
needs: build
runs-on: ubuntu-latest
steps:
- run: echo "Built ${{ needs.build.outputs.artifact-name }}"Here build is the caller's job ID, so the expression is needs.build.outputs.artifact-name. The needs line also makes announce wait until the called workflow finishes.
Common errors
- Empty output. One of the three hops is missing or misspelled. Check the step
id, the joboutputskey, and thevalueexpression inworkflow_call.outputs. - Output name mismatch. The name under
workflow_call.outputsis the name the caller uses. It does not have to match the job output name, but keeping them the same avoids confusion. - Forgetting
needs. Withoutneeds: build, theannouncejob cannot readneeds.build.outputsand may run before the build is done. - Secrets in outputs. Do not put secret values in outputs. Outputs are passed between jobs as plain data, and GitHub may drop an output that contains a registered secret.
- Writing with the old syntax. The
::set-outputcommand is deprecated. Always appendname=valueto the file named byGITHUB_OUTPUT.
Exercise
A caller job with ID package uses a reusable workflow that declares a workflow output named version. A later job needs this value. Write the needs line and the expression that read it, and name one thing that makes the expression evaluate to an empty string.
Show answer
The later job declares needs: package and reads ${{ needs.package.outputs.version }}. It is empty if any hop is missing: the step did not write to GITHUB_OUTPUT, the job did not map the step output under jobs.<id>.outputs, or on.workflow_call.outputs.version does not point at that job output.
Exercise
In the called workflow, why does the step that writes the value need an id?
Show answer
The job output refers to the step's output as steps.<id>.outputs.<name>. Without an id, the job has no way to reference that step.