How to reuse workflows in GitHub Actions?
· Category: DevOps & CI/CD
Short answer
Reusable workflows let you define a workflow once and call it from other workflows. They support inputs, secrets, and outputs, enabling modular CI/CD design.
Steps
- Create a workflow with
workflow_callin theonsection. - Define
inputsandsecrets. - Call the reusable workflow from another workflow with
uses.
Example
# .github/workflows/reusable-build.yml
name: Reusable Build
on:
workflow_call:
inputs:
node-version:
required: true
type: string
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- run: npm ci && npm run build
Caller:
jobs:
call-build:
uses: ./.github/workflows/reusable-build.yml
with:
node-version: '18'
Tips
- Store reusable workflows in a central repository for organization-wide use.
- Document inputs and outputs clearly.
- Version reusable workflows with Git tags.
Common issues
- Reusable workflows cannot call other reusable workflows.
- Secrets must be explicitly passed with
secrets. - Relative paths in
usesare resolved from the caller repository.