Sub topic 13
Using needs with reusable workflows
This chapter assumes you know how needs orders ordinary jobs, how job outputs work, and the basics of YAML. You don't need prior experience with reusable workflows. This chapter covers the parts that affect job ordering.
A called workflow is one job
A reusable workflow is a workflow file with the workflow_call trigger. Another workflow runs it by setting uses on a job, in place of runs-on and steps. That job is the calling job.
From the caller's point of view, the calling job behaves like any other job:
- It can have
needs, so it waits for other jobs. - Other jobs can list it in their own
needs. - It gets a result of
success,failure,skipped, orcancelled. - It can have an
ifcondition and astrategy.matrix.
The jobs inside the called workflow are hidden from the caller. You cannot write needs: compile to wait for one inner job. You wait for the calling job, which finishes when all the jobs inside it finish.
Ordering inside and outside
Each file has its own needs scope. Inside the reusable workflow, needs can only name jobs in that same file. In the caller, needs can only name jobs in the caller.
Returning outputs
A called workflow does not expose its job outputs automatically. It declares them under on.workflow_call.outputs, and each one points at an inner job's output.
# .github/workflows/build.yml
name: Build
on:
workflow_call:
outputs:
version:
description: Version that was built
value: ${{ jobs.compile.outputs.version }}
jobs:
compile:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.ver.outputs.version }}
steps:
- id: ver
run: echo "version=1.2.3" >> "$GITHUB_OUTPUT"The caller reads that output through the needs context, using the calling job's ID:
# .github/workflows/ci.yml
name: CI
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lint
build:
uses: ./.github/workflows/build.yml
deploy:
needs: [lint, build]
runs-on: ubuntu-latest
steps:
- run: echo "Deploying ${{ needs.build.outputs.version }}"Here lint and build start together. deploy waits for both, then reads needs.build.outputs.version.
Artifacts and the shared run
A called workflow is part of the caller's workflow run. Artifacts are scoped to the run, so a file uploaded by a job inside build.yml can be downloaded by deploy with actions/download-artifact. Each job still gets its own runner, so the usual rule applies: use an artifact to pass files, not the filesystem.
Matrix and failure behavior
You can put a matrix on the calling job. The called workflow runs once per matrix combination, and a job that needs the caller waits for all of them. Failure propagation works as before: if any inner job fails, the calling job fails, and its dependents are skipped unless they use if: ${{ always() }} or a similar status check.
Common errors
| Symptom | Likely cause |
|---|---|
| Workflow file is rejected as invalid | The calling job has `steps` or `runs-on` next to `uses`. Remove them. |
| `needs.build.outputs.version` is empty | The reusable workflow did not declare the output under `workflow_call.outputs`. |
| `needs: compile` is rejected | `compile` is inside the called workflow. Use the calling job's ID instead. |
| Called workflow cannot read a secret | Secrets are not passed automatically. Add `secrets: inherit` or list them under `secrets:`. |
Exercise
A caller has the job package that uses ./.github/workflows/package.yml. Inside that file, the job zip sets an output file. A later job upload in the caller needs that value. Which two things must be in place?
Show answer
The called workflow must declare an output under on.workflow_call.outputs whose value is ${{ jobs.zip.outputs.file }}. The upload job must have needs: package and read ${{ needs.package.outputs.<name> }}, using the name declared under workflow_call.outputs.