Share pipelines with reusable workflows
In this task you'll write a pipeline once and call it from many workflows.
Copying the same 60 lines of YAML into every repository means fixing every copy when something changes. A reusable workflow has on: workflow_call with typed inputs and secrets, and other workflows call it with uses: at job level. A composite action packs several steps into one step you can drop into any job.
Use a reusable workflow for a whole standard pipeline (test, build, scan) and a composite action for a few steps you repeat (set up the language and install packages). Call shared workflows by a tag or commit, not a moving branch, so a change in the shared file can't break every team at once.
Example. Fernhill Cloud's platform team keeps one python-ci.yml reusable workflow in a shared repository. Thirty services call it with uses: fernhill/pipelines/.github/workflows/python-ci.yml@v3 and pass only their Python version.
Before you start
You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 30 minutes.
Where to look
- Repository
- .github/workflows
- Actions tab
Steps
- Add
.github/workflows/python-ci.ymlwithon: workflow_calland an inputpython-version(see Commands). - Add
.github/workflows/caller.ymlthat runs on push and calls it withuses: ./.github/workflows/python-ci.ymlandpython-version: '3.13'. - Push and open the run. The graph shows the caller's job expanding into the reusable workflow's job.
- Add a second caller job passing
'3.12', and see both run from the same definition. - Move the setup and install steps into
.github/actions/setup/action.ymlas a composite action and use it withuses: ./.github/actions/setup.
Commands
# .github/workflows/python-ci.yml
on:
workflow_call:
inputs:
python-version:
type: string
default: '3.13'
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with: { python-version: '${{ inputs.python-version }}' }
- run: pip install -r requirements-dev.txt && pytest -q
# .github/workflows/caller.yml
on: push
jobs:
ci:
uses: ./.github/workflows/python-ci.yml
with:
python-version: '3.13'
Watch out for
- Cost
- Free in public repositories.
- Security
- Pass only the secrets a reusable workflow needs instead of
secrets: inherit, and pin shared workflows from other repositories to a tag or SHA. - Common mistakes
- Calling a reusable workflow as a step (it must be a job), and nesting reusable workflows more than the allowed depth.
Clean up
Delete the two workflow files when you've finished, or keep them for the final project.
Words to know
- Reusable workflow
- A workflow with workflow_call that other workflows run as a job.
- Composite action
- An action made of several steps, defined in action.yml.
- inputs
- Typed values a caller passes in.
- secrets: inherit
- Passes all the caller's secrets to the reusable workflow.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On