yamlintermediate

GitHub Actions — Reusable Workflow Pattern

Create reusable workflows called from other workflows with inputs, secrets, and output passing.

yaml
# .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.