Sub topic 2
Reusable workflows, composite actions and templates compared
GitHub Actions offers three ways to avoid copying YAML between repositories. They look similar but work differently. This chapter assumes you can write a basic workflow with events, jobs and steps, and that you know what secrets and the GITHUB_TOKEN are.
Three mechanisms at a glance
- Reusable workflow: a complete workflow file that other workflows call. It can contain several jobs, and each job picks its own runner.
- Composite action: a bundle of steps packaged as one action. It runs as a single step inside a job of the caller.
- Workflow template: a starter file that GitHub offers when someone creates a new workflow. It is copied into the repository, so nothing links the copy to the original.
The key question is what unit you want to share. If you share a whole job or a set of jobs, use a reusable workflow. If you share a few steps, use a composite action. If you share a starting point that each repository will edit, use a template.
Reusable workflows
A reusable workflow declares on: workflow_call. A caller uses it at job level with jobs.<job_id>.uses. The called workflow runs its own jobs on its own runners. It can use runs-on, services, environment, needs and strategy like any other workflow. The caller cannot add steps to a called job. The only inputs are the ones the called workflow declares.
jobs:
call-test:
uses: ./.github/workflows/node-test.yml
with:
node-version: '22'Later chapters cover the workflow_call syntax in detail.
Composite actions
A composite action lives in a directory with an action.yml file and declares runs.using: composite. Its steps run inside the caller's job, on the caller's runner and in the caller's workspace.
# .github/actions/setup-deps/action.yml
name: Setup dependencies
description: Install Node.js and project dependencies
inputs:
node-version:
description: Node.js version to use
required: false
default: '20'
runs:
using: composite
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- run: npm ci
shell: bashA step calls it with uses. The repository must be checked out first when the action is in the same repository.
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup-deps
with:
node-version: '22'Every run step in a composite action must set shell. A composite action cannot read the secrets context. The caller has to pass secrets in as inputs. It also cannot choose a runner or define services, because it is only part of a job.
Workflow templates
Workflow templates are stored in the .github/workflow-templates directory of an organization's .github repository. Each template is a workflow file with a .properties.json file that describes it. Members see them on the Actions tab when they create a new workflow. Once someone picks a template, GitHub copies the file. Later changes to the template do not reach repositories that already used it. Templates help with onboarding, not with keeping many repositories in sync.
Comparison
| Aspect | Reusable workflow | Composite action | Workflow template |
|---|---|---|---|
| Unit shared | Jobs | Steps | Whole file, copied |
| Called from | A job, with `uses` | A step, with `uses` | Not called |
| Runner | Its own, per job | The caller's job | Chosen after copying |
| Secrets | Passed with `secrets:` or `inherit` | Passed as inputs | Not applicable |
| Updates reach users | Yes, by ref | Yes, by ref | No |
Combining them
The mechanisms work together. A reusable workflow can use composite actions in its steps. A template can contain a job that calls a reusable workflow. This gives new repositories a short starting file that stays current with the shared logic.
Exercise
A team wants every repository to run lint, then test on Linux, then test on Windows, with one shared definition that they can update in one place. Should they use a reusable workflow or a composite action? Why?
Show answer
A reusable workflow. The shared logic spans several jobs that run on different runners, and a composite action can only supply steps inside one job on the caller's runner. A reusable workflow referenced by tag also lets the team update every repository by moving the tag.