GitHub Actions — Reusable Workflow Pattern
Create reusable workflows called from other workflows with inputs, secrets, and output passing.
# .github/workflows/reusable-deploy.yml
name: Reusable Deploy
on:
workflow_call:
inputs:
environment:
description: 'Target environment'
required: true
type: string
node-version:
description: 'Node.js version'
required: false
default: '20'
type: string
secrets:
DEPLOY_TOKEN:
required: true
outputs:
deploy-url:
description: 'Deployed URL'
value: ${{ jobs.deploy.outputs.url }}
jobs:
deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
outputs:
url: ${{ steps.deploy.outputs.url }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
cache: npm
- run: npm ci && npm run build
- id: deploy
run: echo "url=https://${{ inputs.environment }}.example.com" >> $GITHUB_OUTPUT
---
# .github/workflows/main.yml (caller)
name: CI/CD
on:
push:
branches: [main]
jobs:
deploy-staging:
uses: ./.github/workflows/reusable-deploy.yml
with:
environment: staging
secrets:
DEPLOY_TOKEN: ${{ secrets.STAGING_TOKEN }}
deploy-prod:
needs: deploy-staging
uses: ./.github/workflows/reusable-deploy.yml
with:
environment: production
secrets:
DEPLOY_TOKEN: ${{ secrets.PROD_TOKEN }}Sponsored
GitHub
Use Cases
- DRY deployment workflows across environments
- Standardizing CI/CD across multiple repositories
- Composable pipeline building blocks
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
GitHub Actions — Node.js CI Pipeline
Complete CI workflow for Node.js: install, lint, test, build on every push and PR with caching.
Best for: Automated testing on every push and PR
GitHub Actions CI/CD Pipeline
Complete GitHub Actions workflow with test, build, and deploy stages for a Node.js application.
Best for: Automated testing and deployment on push
GitHub Actions Docker Build and Push
GitHub Actions workflow to build a Docker image and push it to GitHub Container Registry on release.
Best for: Automated Docker image publishing on release
GitHub Actions Test Matrix Strategy
GitHub Actions workflow with matrix strategy to test across multiple Node versions and OS platforms.
Best for: Cross-platform compatibility testing