Share Workflow Logic with GitHub Actions Reusable Workflows
How to create and consume reusable workflows in GitHub Actions, covering inputs, secrets, conditional jobs, matrix strategy, and organization-wide sharing.
Overview
Reusable workflows let you define a GitHub Actions workflow once and call it from other workflows. Rather than duplicating the same build, test, or deploy YAML across repositories, you maintain one versioned pipeline and pull it in wherever you need it. The caller hands over inputs and secrets, and the called workflow runs as if it were defined inline. That’s the DRY principle applied to CI/CD.
When to Use
- Multiple repos share the same build/test/deploy pipeline
- You want to standardize CI/CD across an organization
- A workflow grows too large and needs splitting into reusable components
- Isolation: the called workflow declares its own
permissionsand only sees the secrets you forward explicitly - You need to enforce compliance steps (e.g., mandatory security scans)
When NOT to Use
- Single repo with a simple pipeline: a standard GitHub Actions workflow is simpler
- When you need dynamic workflow generation: reusable workflows are static YAML
- When the caller needs to override individual steps: reusable workflows are all-or-nothing
- Composite actions are better for small, reusable step sequences
Solution
Basic reusable workflow
A workflow becomes callable when its on: block includes workflow_call. Inputs are typed (string, boolean, number) and can declare defaults:
# .github/workflows/reusable-build.yml
name: Reusable Build
on:
workflow_call: # This makes it callable from other workflows
inputs:
node-version:
type: string
required: false
default: "20"
run-tests:
type: boolean
required: false
default: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: npm
- run: npm ci
- run: npm run build
- name: Run tests
if: ${{ inputs.run-tests }}
run: npm test
Calling a reusable workflow
Call it at the job level with uses: and a relative path for workflows in the same repository:
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
build:
uses: ./.github/workflows/reusable-build.yml
with:
node-version: "22"
run-tests: true
Passing secrets to a reusable workflow
Secrets are declared per-name on the callee side and marked required or optional:
# .github/workflows/reusable-deploy.yml
name: Reusable Deploy
on:
workflow_call:
inputs:
environment:
type: string
required: true
description: "Deployment environment (staging or production)"
secrets:
deploy-token:
required: true
registry-password:
required: false
jobs:
deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
steps:
- uses: actions/checkout@v4
- name: Deploy
run: |
echo "Deploying to ${{ inputs.environment }}"
./deploy.sh --token ${{ secrets.deploy-token }}
Caller passing secrets
Each named secret in the callee must be mapped explicitly in the calling job (or forwarded wholesale with secrets: inherit):
jobs:
deploy-staging:
uses: ./.github/workflows/reusable-deploy.yml
with:
environment: staging
secrets:
deploy-token: ${{ secrets.STAGING_DEPLOY_TOKEN }}
registry-password: ${{ secrets.REGISTRY_PASSWORD }}
deploy-production:
needs: deploy-staging
if: github.ref == 'refs/heads/main'
uses: ./.github/workflows/reusable-deploy.yml
with:
environment: production
secrets:
deploy-token: ${{ secrets.PROD_DEPLOY_TOKEN }}
registry-password: ${{ secrets.REGISTRY_PASSWORD }}
Reusable workflow with matrix strategy
Inputs are scalar, so pass the matrix as a JSON string and expand it with fromJSON, the same technique used in the matrix strategy recipe:
# .github/workflows/reusable-test-matrix.yml
name: Reusable Test Matrix
on:
workflow_call:
inputs:
node-versions:
type: string
required: false
default: '["18", "20", "22"]'
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ${{ fromJSON(inputs.node-versions) }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm test
Calling with custom matrix
jobs:
test:
uses: ./.github/workflows/reusable-test-matrix.yml
with:
node-versions: '["20", "22"]'
Organization-wide reusable workflow
To share a workflow across repos, host it in a central repository (a common convention is the org’s .github repo) and reference it as owner/repo/path@ref:
# In any repo's workflow, reference a workflow from another repo
jobs:
security-scan:
uses: my-org/.github/.github/workflows/security-scan.yml@main
with:
severity: high
secrets:
scan-token: ${{ secrets.SCAN_TOKEN }}
Setting up organization-level workflows
# Repository structure: my-org/.github
# File: .github/workflows/reusable-security-scan.yml
# This workflow is available to all repos in the org
# .github/workflows/reusable-security-scan.yml
name: Security Scan
on:
workflow_call:
inputs:
severity:
type: string
required: false
default: "medium"
secrets:
scan-token:
required: true
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run security scan
run: |
npm install -g @security/scanner
scanner scan --severity ${{ inputs.severity }} --token ${{ secrets.scan-token }}
Conditional jobs in reusable workflow
Boolean inputs work well as feature flags for optional stages inside the callee:
# .github/workflows/reusable-ci.yml
name: Reusable CI
on:
workflow_call:
inputs:
skip-lint:
type: boolean
required: false
default: false
deploy:
type: boolean
required: false
default: false
jobs:
lint:
if: ${{ !inputs.skip-lint }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lint
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm test
deploy:
if: ${{ inputs.deploy }}
needs: [lint, test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh
Nested reusable workflows
A called workflow can itself call other workflows, which lets you compose higher-level building blocks:
# .github/workflows/reusable-quality.yml
name: Reusable Quality Checks
on:
workflow_call:
inputs:
node-version:
type: string
default: "20"
jobs:
lint:
uses: ./.github/workflows/reusable-lint.yml
with:
node-version: ${{ inputs.node-version }}
test:
uses: ./.github/workflows/reusable-test.yml
with:
node-version: ${{ inputs.node-version }}
Outputs from reusable workflows
Data flows back through two hops: the job sets outputs, the workflow_call block re-exposes them:
# .github/workflows/reusable-build-with-output.yml
name: Reusable Build with Output
on:
workflow_call:
inputs:
app-name:
type: string
required: true
outputs:
artifact-url:
description: "URL of the built artifact"
value: ${{ jobs.build.outputs.artifact-url }}
jobs:
build:
runs-on: ubuntu-latest
outputs:
artifact-url: ${{ steps.upload.outputs.artifact-url }}
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- name: Upload artifact
id: upload
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.app-name }}-dist
path: dist/
- name: Set output
run: echo "artifact-url=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" >> $GITHUB_OUTPUT
Consuming outputs
jobs:
build:
uses: ./.github/workflows/reusable-build-with-output.yml
with:
app-name: my-app
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Use output
run: echo "Artifact at ${{ needs.build.outputs.artifact-url }}"
Explanation
How caller and callee connect
To the caller, a uses: job is a single node in the DAG: it shows up as one job in the run graph, expands internally into the called workflow’s own jobs, and participates in needs like any other job. The call is resolved when the workflow runs, not when you edit it, so the ref in owner/repo/.github/workflows/file.yml@ref decides which version executes. A branch like @main always picks the newest commit, while a tag or commit SHA freezes the implementation.
The called workflow file must live under .github/workflows/ in its repository, and that repository must allow the access level you need: public repos work from anywhere, while private or internal repos require the Actions access setting that permits calls from repositories in the same organization.
Permissions and secrets
The called workflow doesn’t see any caller secrets by default. You either map each named secret under secrets: in the calling job or forward everything at once with secrets: inherit. For GITHUB_TOKEN, the effective permissions inside the called workflow are the most restrictive of what the caller and the callee declare. A reusable workflow can lower privileges, never elevate them.
Environment variables don’t cross the boundary either: a top-level env: in the caller isn’t visible inside the called workflow, so pass anything the callee needs as an explicit input.
Passing data back with outputs
Getting data back to the caller takes two hops. A job inside the called workflow publishes outputs from steps; the workflow_call.outputs block re-exposes them at workflow level; and the caller reads them through needs.<job-id>.outputs. Without the middle hop, job outputs stay invisible to the caller.
Nesting and limits
Reusable workflows can call other reusable workflows up to four levels deep, enough headroom for orchestration layers like a “quality checks” wrapper that fans out to lint and test workflows. Beyond that, keep a few structural limits in mind: a uses: job can’t contain steps or run (the call replaces them), callers can’t reach inside the called workflow to tweak a single step, and needs only connects jobs within the same workflow file; the callee manages its own internal dependencies.
Common errors
called workflow cannot be used: the referenced repo is private or internal and its Actions settings don’t allow access from your repository. Fix the “Access” setting under Actions → General on the called repo, or move the workflow to a public one.- Workflow not found / invalid reference: the path must be
.github/workflows/<file>.ymlinside the referenced repo, and the@refmust resolve. Check typos in owner/repo/path and that the ref exists. workflow_callmissing: a workflow withouton: workflow_callisn’t callable even if everything else looks right.- Inputs not arriving: when
with:names don’t matchworkflow_call.inputs, validation fails, and a required input with no caller value aborts the run.
Variants
Reusable workflow with Docker build
# .github/workflows/reusable-docker-build.yml
name: Reusable Docker Build
on:
workflow_call:
inputs:
image-name:
type: string
required: true
registry:
type: string
default: "ghcr.io"
tag:
type: string
default: "latest"
secrets:
registry-token:
required: true
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ${{ inputs.registry }}
username: ${{ github.actor }}
password: ${{ secrets.registry-token }}
- uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ inputs.registry }}/${{ github.repository }}/${{ inputs.image-name }}:${{ inputs.tag }}
cache-from: type=gha
cache-to: type=gha,mode=max
Reusable workflow with environment protection
# .github/workflows/reusable-deploy-protected.yml
name: Reusable Deploy (Protected)
on:
workflow_call:
inputs:
environment:
type: string
required: true
secrets:
deploy-token:
required: true
jobs:
deploy:
runs-on: ubuntu-latest
environment:
name: ${{ inputs.environment }}
url: https://${{ inputs.environment }}.myapp.com
steps:
- uses: actions/checkout@v4
- name: Deploy
run: ./deploy.sh --env ${{ inputs.environment }} --token ${{ secrets.deploy-token }}
Best Practices
- Store shared workflows in a central repo;
.githubis the common convention and keeps them discoverable - Pin callers to a tag or SHA (
@v1,@abc123). A moving@maincan break every consumer on a bad commit - Keep
workflow_callas the only trigger. Mixing it withpushorpull_requestruns the reusable workflow on its own repo’s events too - Document inputs and secrets with
descriptionfields; it helps consumers - Keep reusable workflows focused: one concern per workflow (build, test, deploy)
secrets: inheritforwards every caller secret at once. Convenient, but explicit mapping keeps the blast radius smaller- Set
permissionsexplicitly; reusable workflows should follow least-privilege - Use outputs to pass data back to the caller instead of writing to shared state
Common Mistakes
- Not adding the
workflow_calltrigger: a workflow is only callable whenon:includesworkflow_call. - Mixing
workflow_callwith other triggers: push or schedule triggers will also fire the reusable workflow on its own repo. Keepworkflow_callas its only trigger. - Forgetting to pass secrets: a called workflow sees no caller secrets unless you map them under
secrets:in the calling job (or setsecrets: inherit). - Expecting
needsto reach inside the callee:needsconnects jobs in the caller’s graph, and the called workflow manages its own internal job dependencies. - Not pinning to a ref: a moving
@maincan break callers when the workflow changes. Point to a tag or a commit SHA instead.
See Also
- Complete Guide to CI/CD with GitHub Actions: end-to-end pipeline design around these patterns
- Reusing workflows (GitHub Docs): official reference for
workflow_call, limits and outputs - Workflow syntax for GitHub Actions: full
on.workflow_callsyntax reference - Companion runnable workflows: the YAML files from this recipe ready to drop into a repository
Frequently Asked Questions
What is a reusable workflow?
A GitHub Actions workflow defined with on: workflow_call that other workflows invoke through a job-level uses: reference, passing typed inputs and explicitly mapped secrets.
How is a reusable workflow different from a composite action?
Composite actions reuse individual steps inside a job. Reusable workflows reuse entire workflows: multiple jobs with their own runners. Use composite actions for small step sequences, reusable workflows for full pipelines.
Can I pass all secrets at once?
Yes: set secrets: inherit on the calling job and every caller secret is forwarded. Prefer explicit mapping when the callee only needs a couple of secrets, since inherit widens what the called code can read.
Can a reusable workflow call another reusable workflow?
Yes: nesting works up to four levels deep. That covers orchestration workflows like "quality checks" that internally call lint and test workflows.
How do I share reusable workflows across repositories?
Host them in a central repository (the org's .github repo is the common convention) and call them with uses: my-org/.github/.github/workflows/workflow.yml@ref. Private or internal repos must enable Actions access for other repositories in the organization.
Related Resources
GitHub Actions Workflow Examples: CI/CD, Matrix & Secrets
How to build and deploy with GitHub Actions using workflows, matrices, caching, secrets, environments, and reusable workflows.
RecipeGitHub Actions Matrix Strategy: Test Across OS and Versions
Run one job across every OS/runtime combination with GitHub Actions matrix strategy: include/exclude, fail-fast, dynamic matrices, and service containers.
RecipeCI/CD Pipeline Setup
Set up automated CI/CD pipelines for testing, building, and deploying applications with GitHub Actions and what works.
RecipeOverride Docker Compose Configs per Environment
How to use Docker Compose override files for environment-specific configurations, covering dev, test, staging, production, profiles, and secrets.
GuideComplete Guide to CI/CD with GitHub Actions
Build CI/CD pipelines from scratch with GitHub Actions. Covers workflows, runners, matrix builds, caching, secrets, environments, deployment strategies, and reusable workflows.
DocCI/CD Pipeline Security Template
A template for securing build and deployment pipelines against credential leaks, tampering, supply chain attacks, and unauthorized deployments.