patternbashTip
Composite actions for reusable multi-step logic within a repo
Viewed 0 times
composite actionreusable stepsaction.ymlDRYlocal actionshell
Problem
The same sequence of 4-5 steps appears in multiple jobs within a repository (e.g., checkout + setup Node + restore cache + install). Reusable workflows add an extra job layer and have input type limitations.
Solution
Create a composite action in .github/actions/setup-node:
Use it in any job:
# .github/actions/setup-node/action.yml
name: Setup Node
description: Checkout, setup Node, restore cache, and install dependencies
inputs:
node-version:
description: Node.js version
required: false
default: '20'
runs:
using: composite
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-nm-${{ hashFiles('package-lock.json') }}
- run: npm ci
shell: bashUse it in any job:
steps:
- uses: ./.github/actions/setup-node
with:
node-version: '22'Why
Composite actions run in the same job, share the same workspace and environment, and do not add extra job overhead. They are simpler than reusable workflows for step-level reuse.
Gotchas
- Every run step in a composite action must specify shell explicitly
- Composite actions cannot use continue-on-error at the action level—only individual steps
- Secrets cannot be inputs to composite actions; pass them as environment variables from the caller
Revisions (0)
No revisions yet.