This guide explains how to create and use callable/reusable GitHub Actions workflows for efficient CI/CD management.
Reusable workflows allow you to define a workflow in one repository and call it from workflows in other repositories, promoting code reuse and consistency across projects.
Create a workflow file in .github/workflows/ with the workflow_call trigger:
name: Reusable CI Workflow
on:
workflow_call:
inputs:
node-version:
required: false
type: string
default: '18'
environment:
required: false
type: string
default: 'development'
secrets:
token:
required: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Upload coverage
uses: codecov/codecov-action@v3From another workflow, call it using uses:
name: CI
on:
push:
branches: [ main ]
jobs:
call-reusable-workflow:
uses: ./.github/workflows/reusable-ci.yml
with:
node-version: '20'
environment: 'production'
secrets:
token: ${{ secrets.GITHUB_TOKEN }}- Input Validation: Always validate inputs in your reusable workflows
- Secret Management: Use secrets carefully and document required secrets
- Version Pinning: Pin workflow versions to ensure stability
- Error Handling: Implement proper error handling and status reporting
- Documentation: Document inputs, outputs, and required secrets clearly
# In reusable workflow
outputs:
test-results:
value: ${{ jobs.test.outputs.results }}
# In calling workflow
jobs:
call-workflow:
uses: ./.github/workflows/reusable-ci.yml
outputs:
results: ${{ jobs.call-workflow.outputs.test-results }}jobs:
call-workflow:
if: github.event_name == 'pull_request'
uses: ./.github/workflows/reusable-ci.yml- DRY Principle: Avoid duplicating workflow logic
- Consistency: Ensure uniform CI/CD across repositories
- Maintenance: Update workflows in one place
- Security: Centralized control over sensitive operations
- Scalability: Easy to extend and modify workflows