Reusable Workflows & Composite Actions
Once a company has twenty repositories, it has twenty copies of the same release workflow, each slightly out of date. GitHub Actions has two ways to share CI code: reusable workflows (share whole jobs) and composite actions (share a sequence of steps). Release engineering teams typically own a small set of both, versioned like any other release.
Which one?
| Reusable workflow | Composite action | |
|---|---|---|
| Unit of reuse | one or more jobs (with their own runners, matrices, environments) | steps inside the caller's job |
| Defined in | .github/workflows/<name>.yml with on: workflow_call |
action.yml with runs: using: composite, in any repository or folder |
| Called with | jobs.<id>.uses: owner/repo/.github/workflows/release.yml@v2 |
steps[*].uses: owner/repo/path@v2 (or ./.github/actions/setup) |
| Secrets | passed explicitly (secrets:) or secrets: inherit |
none of its own; the caller passes values as inputs |
| Environments, approvals | yes (its jobs can use environment:) |
no (runs inside the caller's job) |
| Shows in the UI as | separate jobs | one step (logs grouped) |
| Limits | 10 levels of nesting, 50 unique reusable workflows per workflow file | — |
| Best for | "the release pipeline", "the deploy pipeline" | "set up our toolchain", "package and checksum" |
A reusable release workflow
# acme/workflows/.github/workflows/release.yml (the shared repository)
name: release
on:
workflow_call:
inputs:
version:
description: Version to release, without the v
required: true
type: string
targets:
description: JSON list of targets to build
required: false
type: string
default: '["linux-x86_64"]'
secrets:
SIGNING_KEY:
required: true
outputs:
release-url:
description: URL of the GitHub Release
value: ${{ jobs.publish.outputs.url }}
jobs:
build:
strategy:
matrix:
target: ${{ fromJSON(inputs.targets) }}
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- run: ./scripts/package.sh "${{ inputs.version }}" "${{ matrix.target }}"
- uses: actions/upload-artifact@v7
with:
name: dist-${{ matrix.target }}
path: dist/*
publish:
needs: build
runs-on: ubuntu-24.04
permissions:
contents: write
outputs:
url: ${{ steps.release.outputs.url }}
steps:
- uses: actions/download-artifact@v8
with:
pattern: dist-*
merge-multiple: true
path: dist
- id: release
env:
GH_TOKEN: ${{ github.token }}
SIGNING_KEY: ${{ secrets.SIGNING_KEY }}
VERSION: ${{ inputs.version }}
run: |
./scripts/sign-checksums.sh dist
gh release create "v$VERSION" dist/* --repo "$GITHUB_REPOSITORY" --verify-tag --notes-file dist/NOTES.md
echo "url=https://github.com/$GITHUB_REPOSITORY/releases/tag/v$VERSION" >> "$GITHUB_OUTPUT"The caller, in each product repository:
# .github/workflows/on-tag.yml
on:
push:
tags: ['v*']
jobs:
release:
uses: acme/workflows/.github/workflows/release.yml@v2
permissions:
contents: write
with:
version: ${{ github.ref_name }} # see the note below
targets: '["linux-x86_64", "linux-aarch64"]'
secrets:
SIGNING_KEY: ${{ secrets.SIGNING_KEY }} # or: secrets: inherit
announce:
needs: release
runs-on: ubuntu-24.04
steps:
- run: echo "Released at ${{ needs.release.outputs.release-url }}"Rules that bite:
- A calling job may only contain
name,uses,with,secrets,strategy,needs,if,concurrency,permissions(and a few related keys): noruns-on, nosteps. - The caller's workflow-level
envis not passed to the called workflow; useinputs. - The called workflow gets the caller's
GITHUB_TOKENpermissions and can only lower them, never raise them. github.ref_namefor tagv1.4.2isv1.4.2; the reusable workflow above expects the version without thev, so a real caller strips it in a small step first (or the shared workflow does it). Write such conventions into the input'sdescription.
A composite action
# acme/actions/setup-toolchain/action.yml
name: Set up the Acme toolchain
description: Node, the pinned release tools, and the package cache
inputs:
node-version:
description: Node.js version
default: '24'
runs:
using: composite
steps:
- uses: actions/setup-node@v7
with:
node-version: ${{ inputs.node-version }}
cache: npm
- run: npm ci --no-audit --no-fund
shell: bash # required for run steps in composite actions
- run: echo "$PWD/node_modules/.bin" >> "$GITHUB_PATH"
shell: bashUsed as one step: - uses: acme/actions/setup-toolchain@v1. A composite action has no runs-on and no secrets of its own; it runs in the caller's job, so it shares the workspace and environment with the caller's other steps.
A model: nesting and permissions
Reusable workflows can call reusable workflows, but each level can only keep or reduce the token permissions, and the chain may be at most 10 levels deep (the top-level caller included). The model walks a call chain and computes the effective permissions at every level:
LEVELS = {"none": 0, "read": 1, "write": 2}
MAX_DEPTH = 10
def effective(parent, requested):
"""A called workflow can only keep or lower its caller's permissions."""
if requested is None: # nothing requested: inherit
return dict(parent)
out = {}
for scope in parent:
asked = requested.get(scope, "none") # unspecified scopes become none
out[scope] = min(parent[scope], asked, key=LEVELS.get)
return out
chain = [
("on-tag.yml (caller)", {"contents": "write", "packages": "write", "id-token": "write"}),
("release.yml", {"contents": "write", "id-token": "write"}),
("sign.yml", {"contents": "read", "id-token": "write", "packages": "write"}),
("notify.yml", None),
]
perms = chain[0][1]
for depth, (name, requested) in enumerate(chain, start=1):
perms = effective(perms, requested) if depth > 1 else dict(requested)
granted = ", ".join(f"{k}:{v}" for k, v in perms.items() if v != "none") or "nothing"
print(f"level {depth} {name:<22} {granted}")
print("sign.yml asked for packages:write, but release.yml had already dropped it")
deep = 12
print(f"a chain of {deep} levels is {'allowed' if deep <= MAX_DEPTH else 'rejected (max ' + str(MAX_DEPTH) + ')'}")level 1 on-tag.yml (caller) contents:write, packages:write, id-token:write
level 2 release.yml contents:write, id-token:write
level 3 sign.yml contents:read, id-token:write
level 4 notify.yml contents:read, id-token:write
sign.yml asked for packages:write, but release.yml had already dropped it
a chain of 12 levels is rejected (max 10)
Versioning shared CI code
Shared workflows and actions are releases, with users who break when you change them:
- tag them with SemVer and keep a moving major tag (
v2→ latestv2.x.y), the convention all official actions use; - callers pin a major tag, or better, a full commit SHA with the version in a comment (Pinning by SHA);
- write a changelog, and treat renaming an input as a breaking change;
- test them with a workflow in their own repository that calls them.
Test yourself
You need a shared deployment with a protected environment and manual approval. Which mechanism?
- A composite action
- A reusable workflow (it defines jobs, which can use environments)
- A shell script
- A matrix
A called workflow requests contents: write but the caller granted contents: read. What does it get?
- write
- read: called workflows can only keep or lower permissions
- none
- The workflow fails to load
What must every run: step in a composite action specify?
runs-onshellenvid