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

  1. Create a workflow with workflow_call in the on section.
  2. Define inputs and secrets.
  3. 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 uses are resolved from the caller repository.