Sub topic 3
Defining a reusable workflow with workflow_call
A reusable workflow is an ordinary workflow file that also listens for the workflow_call event. This chapter shows how to declare that trigger, define typed inputs and secrets, and consume them in steps. It assumes you can write a basic workflow with events, jobs and steps, and that you know what GitHub secrets and the GITHUB_TOKEN are.
What workflow_call does
Most workflows start on events such as push or pull_request. The workflow_call event is different. It runs the workflow only when another workflow's job calls it. A workflow can list workflow_call next to other triggers, so one file can run on its own and also be reused.
Reusable workflows must live in the .github/workflows directory of a repository. Subdirectories are not supported.
A complete example
The following file declares one input and one optional secret, then uses both.
# .github/workflows/node-test.yml
name: Node test
on:
workflow_call:
inputs:
node-version:
description: Node.js version to use
type: string
required: false
default: '20'
secrets:
npm_token:
required: false
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- run: npm ci
env:
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
- run: npm testDeclaring inputs
Each key under inputs is an input name. An input accepts these fields:
| Field | Purpose |
|---|---|
| `description` | Text that documents the input. |
| `type` | Required. One of `string`, `number` or `boolean`. |
| `required` | If `true`, the caller must supply a value. |
| `default` | Value used when the caller omits the input. |
Inside the workflow, read an input with the inputs context, as in ${{ inputs.node-version }}. The type field matters because the value keeps its type. A boolean input can be used directly in an if: condition, for example if: ${{ inputs.run-lint }}.
Declaring secrets
Secrets are declared under secrets. Each entry has an optional description and a required field. Unlike inputs, secrets have no type and no default. Read them with the secrets context, as in ${{ secrets.npm_token }}.
If a secret is optional and the caller does not pass it, the expression evaluates to an empty string. In the example above, npm ci then runs with an empty NODE_AUTH_TOKEN, which is fine for public packages.
How the pieces fit together
The caller supplies values, and the called workflow reads them through contexts.
Common mistakes
- Missing
type. An input without atypeis rejected as invalid. - Using
envfrom the caller. Environment variables set in the caller'senvare not available in the called workflow. Pass a value as an input instead. - Forgetting the
requiredsecret. If you mark a secretrequired: trueand the caller omits it, the run fails. - Wrong location. A file outside
.github/workflowscannot be called.
Next step
You now have a workflow that others can call. The next chapter shows how a job in another workflow calls it with jobs.<job_id>.uses.
Exercise
A reusable workflow declares inputs.retries without a type field and secrets.deploy_key with required: true. Name the two problems you would expect, and say where each value is read inside the workflow.
Show answer
The input is invalid because every workflow_call input needs a type (string, number or boolean). The secret will make the run fail if the caller does not pass it. Read the input with ${{ inputs.retries }} and the secret with ${{ secrets.deploy_key }}.
Exercise
You want a workflow to run on pushes to main and also be callable by other workflows. How do you configure the trigger?
Show answer
List both events under on:, for example push with branches: [main] and workflow_call: with its inputs. The file then runs on its own for pushes and can also be called.