All guides

Reusable workflows and composite actions in GitHub Actions

GitHub Actions gives you two ways to stop copying a job or its steps from one workflow to another: a reusable workflow for whole jobs, and a composite action for a group of steps.

A whiteboard with handwritten release notes and sticky notes

A reusable workflow

A reusable workflow is a normal workflow file whose on key includes workflow_call. It lives in .github/workflows; subdirectories are not supported. It declares what it accepts and what it returns.

YAML

# my-org/platform-workflows/.github/workflows/deploy.yml
on:
  workflow_call:
    inputs:
      environment:
        required: true
        type: string
    secrets:
      deploy_token:
        required: true
    outputs:
      url:
        description: Address of the deployed app
        value: ${{ jobs.deploy.outputs.url }}

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: ${{ inputs.environment }}
    outputs:
      url: ${{ steps.release.outputs.url }}
    steps:
      - uses: actions/checkout@v7
      - id: release
        run: |
          ./deploy.sh
          echo "url=https://my-app.example.com" >> "$GITHUB_OUTPUT"
        env:
          DEPLOY_TOKEN: ${{ secrets.deploy_token }}

Every input needs a type of boolean, number or string. The value of a workflow output must be a job-level output, so a step output is mapped twice: step to job, then job to workflow. ./deploy.sh and the address stand for your own script and its result.

Calling it by path and ref

The caller puts uses on the job itself, not on a step.

YAML

jobs:
  deploy:
    uses: my-org/platform-workflows/.github/workflows/deploy.yml@v1
    with:
      environment: production
    secrets:
      deploy_token: ${{ secrets.DEPLOY_TOKEN }}

  report:
    needs: deploy
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deployed to $URL"
        env:
          URL: ${{ needs.deploy.outputs.url }}

The reference takes one of three forms:

  • {owner}/{repo}/.github/workflows/{filename}@{ref} for a workflow in another repository. The ref is a commit SHA, a release tag or a branch name.
  • ./.github/workflows/{filename} for a workflow in the same repository, taken from the same commit as the caller.
  • $/.github/workflows/{filename}, which GitHub recommends for the same repository. It takes no @{ref} and is not available in GitHub Enterprise Server.

Contexts and expressions are not allowed in uses. GitHub's reference lists the only keys a calling job can use: name, uses, with, secrets, strategy, needs, if, concurrency, permissions and cache-mode. The calling job cannot use environment. To use an environment's secrets, set environment on the job inside the reusable workflow.

Secrets and inherit

secrets: inherit passes every secret the caller can read. It works between repositories in the same organization, and between organizations in the same enterprise. The called workflow can then read secrets it never declared.

Secrets are passed only to the directly called workflow. In a chain A, B, C, workflow C receives a secret from A only if A passes it to B and B passes it to C. To pass some secrets and not all, name each one under secrets, as the caller above does.

Limits to plan around

  • A chain holds ten levels at most: the top caller and nine reusable workflows. Loops are not permitted.
  • One workflow file can call 50 unique reusable workflows, nested calls included.
  • env set at the workflow level of the caller does not reach the called workflow. Pass an input, or read a vars configuration variable.
  • GITHUB_TOKEN permissions can stay the same or shrink down the chain. They never grow.
  • The github context is always the caller's. actions/checkout in a called workflow checks out the repository that hosts the caller.
  • For GitHub-hosted runners, the bill always goes to the caller.

A composite action

A composite action is a repository or a directory with an action.yml file that collects a series of steps. It runs as a single step in a job.

YAML

# .github/actions/setup-app/action.yml
name: Set up the app
description: Install Node.js and the project dependencies
inputs:
  node-version:
    description: Node.js version to install
    required: true
runs:
  using: composite
  steps:
    - uses: actions/setup-node@v7
      with:
        node-version: ${{ inputs.node-version }}
    - run: npm ci
      shell: bash

shell is required on every step that has run. Call the action with uses: ./.github/actions/setup-app after a checkout step, or with uses: $/.github/actions/setup-app, which needs no checkout. From another repository the form is {owner}/{repo}/{path}@{ref}.

When each fits

GitHub's own comparison of the two gives these differences:

  • Reusable workflow: it contains one or more jobs, and each job names the type of machine it runs on. Every job and step is logged separately. It can use secrets.
  • Composite action: it contains steps and no jobs, and runs as one step in the caller's job, with other steps before or after it. The log shows it as one step. It cannot use secrets. Up to 10 composite actions can be nested in one workflow.

GitHub's guidance: if the steps must run on a different type of machine from the calling job, use a reusable workflow.

Versioning by tag or commit SHA

GitHub resolves a tag or a branch to whatever commit it points to when the workflow runs, and tags and branches can move. GitHub calls the full commit SHA the safest option for stability and security.

GitHub also offers a workflow dependency lockfile at .github/workflows/actions.lock. It pins every action a workflow uses, but it does not pin reusable workflow calls. Pin those by SHA in uses.

A reference that is not a SHA also affects re-runs. Re-running all jobs uses the reusable workflow from the specified reference. Re-running failed jobs, or one job, uses the commit SHA of the first attempt.

Mistakes to avoid

  1. A private workflow repository with closed access. For a private repository, the Access policy on its Actions settings page must explicitly allow the repositories that hold the caller workflows.
  2. The same concurrency group in caller and called workflow. A called workflow has its caller's name in github.workflow. If both use it as the group with cancel-in-progress: true, the caller is canceled when the called workflow runs.
  3. An environment secret the caller forgot. It resolves to an empty string and the run shows no error. Set required: true on the secret so the run fails.
  4. A rename. For actions and reusable workflows, GitHub Actions follows no redirects. After a change of owner or repository name, workflows that use the previous name fail.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides