Sub topic 15
Reusing per-project workflows
This chapter assumes you can write a change-detection job, use if conditions on its outputs, and have a gate job in place. Here you move the per-project steps into one reusable workflow, so a new project needs a filter entry and not a copied job.
Why reuse
Each project in a monorepo usually runs the same steps: check out the code, install dependencies, test. If you copy those steps into every job, they drift apart. A reusable workflow is a workflow file that other workflows call like a function. You define the steps once and pass the project name as an input.
Define the reusable workflow
A workflow becomes callable when its on block contains workflow_call. Inputs are declared there.
# .github/workflows/service-test.yml
name: service-test
on:
workflow_call:
inputs:
package:
required: true
type: string
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make -C services/${{ inputs.package }} testCall it from the matrix
The caller keeps the changes job from the previous chapter. The test job now uses uses: in place of runs-on and steps. Matrix and if both work on a job that calls a reusable workflow.
test:
needs: changes
if: needs.changes.outputs.packages != '[]'
strategy:
matrix:
package: ${{ fromJSON(needs.changes.outputs.packages) }}
uses: ./.github/workflows/service-test.yml
with:
package: ${{ matrix.package }}
ci-ok:
if: always()
needs: [test]
runs-on: ubuntu-latest
steps:
- name: Fail if any needed job failed or was cancelled
run: |
if [ "${{ contains(needs.*.result, 'failure') }}" = "true" ] || [ "${{ contains(needs.*.result, 'cancelled') }}" = "true" ]; then
exit 1
fiThe ./ path points to a workflow in the same repository, at the same commit as the caller. That means a pull request that edits service-test.yml tests its own edit.
Things that go wrong
- Filter blind spot. Editing
service-test.ymlchanges how every project is tested. Add its path to every filter in thechangesjob, as you did for the caller workflow file. - Secrets are not inherited. A called workflow sees only the secrets you pass. Pass them by name under
secrets:, or writesecrets: inheritwhen the caller and callee are in the same organization. - Permissions only shrink. The called workflow cannot get more
GITHUB_TOKENpermissions than the caller grants. - Skipped calls look like skipped jobs. When the
ifis false, the caller job shows as skipped. Your gate job already treats that as a pass. - Check names change. Checks from a called workflow appear as
caller job / called job, with the matrix value included. This is why branch protection should require onlyci-ok.
Limits
Reusable workflows can be nested, but only to a limited depth, and a single workflow can call a limited number of other workflows. Check the current limits in the GitHub documentation before building deep chains. A flat design, with one caller and one reusable workflow per kind of project, stays inside them.
Exercise
Your reusable workflow service-test.yml is edited in a pull request that touches no services/ files. The filters in the changes job list only services/api/** and services/billing/**. What happens, and how do you fix it?
Show answer
The filters match nothing, so packages is [], the test job is skipped, and ci-ok passes without running any tests. The edit to the shared workflow goes untested. Add .github/workflows/service-test.yml to the pattern list of each filter, so that editing it selects every project.