CI/CD and automation · Intermediate · 18 min
Stop copying CI steps between repositories with reusable workflows and composite actions
By Padding · Updated 18 Nov 2026
GitHub Actions · actionlint 1.7.12 · Ubuntu 26.04
You will move repeated Node.js setup steps into a composite action and a whole test-and-build pipeline into a reusable workflow, tag them, and call both from two repositories with explicit or inherited secrets.
On this page
Once you have three or four repositories with CI, the same twenty lines turn up in every one of them: check out, install Node.js, restore the npm cache, npm ci, test, build, upload. Then a version needs changing, and you open four pull requests that make the same edit. Copies also drift: one repository forgets the cache, another still pins an old action.
GitHub Actions gives you two ways to write those lines once:
- A composite action bundles several steps into one step. A job uses it like any other action, with
uses:insidesteps:. - A reusable workflow bundles whole jobs, each with its own runner. A caller uses it in place of a job, with
uses:at job level.
In this tutorial you build both in a shared repository called ci-shared, test them there, tag a release, and call them from two application repositories, shop-web and shop-api. Everything lives in ~/ci-shared-demo on your computer so it cannot collide with your real work. Wherever you see YOUR-ORG, replace it with your organisation or user name.
Step 1 · Create the shared repository and an example project
Start with the repository that will hold the shared pieces. It also carries a tiny Node.js project under fixtures/, so the shared CI can be tested on something real before any other repository depends on it.
mkdir -p ~/ci-shared-demo/ci-shared
cd ~/ci-shared-demo/ci-shared
git init
mkdir -p .github/actions/node-setup .github/workflows fixtures/hello-node
cd fixtures/hello-node
cat > package.json <<'JSON'
{
"name": "hello-node",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"test": "node --test",
"build": "mkdir -p dist && cp greet.js dist/greet.js"
}
}
JSON
cat > package-lock.json <<'JSON'
{
"name": "hello-node",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hello-node",
"version": "1.0.0"
}
}
}
JSON
cat > greet.js <<'JS'
export function greet(name) {
return `Hello, ${name}`;
}
JS
cat > greet.test.js <<'JS'
import test from 'node:test';
import assert from 'node:assert/strict';
import { greet } from './greet.js';
test('greets by name', () => {
assert.equal(greet('Ada'), 'Hello, Ada');
});
JS
printf 'node_modules/\ndist/\n' > ~/ci-shared-demo/ci-shared/.gitignoreThe project has no dependencies, so the lock file is short, but npm ci still insists on having one. "type": "module" lets the files use import, and node --test finds files ending in .test.js and runs them with the test runner built into Node.js. The .gitignore keeps node_modules and dist out of the repository.
Check it worked
Run the same three commands the CI jobs will run:
cd ~/ci-shared-demo/ci-shared/fixtures/hello-node
npm ci
npm test
npm run build
ls distnpm test reports one passing test (# pass 1), and ls dist prints greet.js.
Step 2 · Write the composite action
A composite action is a folder with an action.yml file whose runs.using is composite. This one replaces the setup block every Node.js repository repeats: install Node.js with the npm cache, then install dependencies.
cd ~/ci-shared-demo/ci-shared
cat > .github/actions/node-setup/action.yml <<'YAML'
name: Node setup
description: Install Node.js with the npm cache and run npm ci. The job must check out the code first.
inputs:
node-version:
description: Node.js version to install
required: false
default: '26'
working-directory:
description: Folder that holds package.json and package-lock.json
required: false
default: '.'
npm-token:
description: Read token for a private npm registry, used by an .npmrc that reads NODE_AUTH_TOKEN. Leave empty for public packages.
required: false
default: ''
outputs:
node-version:
description: The exact Node.js version that was installed
value: ${{ steps.node.outputs.node-version }}
runs:
using: composite
steps:
- name: Install Node.js
id: node
uses: actions/setup-node@v7
with:
node-version: ${{ inputs.node-version }}
cache: npm
cache-dependency-path: ${{ inputs.working-directory }}/package-lock.json
- name: Install dependencies
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
NODE_AUTH_TOKEN: ${{ inputs.npm-token }}
run: npm ci
YAMLFour rules of composite actions show up in this file:
- Every
runstep needsshell:. In a workflow, a job has a default shell. In a composite action there is none, so leavingshell: bashout is an error. Steps thatuses:another action do not need it. - Inputs arrive through the
inputscontext, as${{ inputs.node-version }}. They are not turned intoINPUT_*environment variables the way JavaScript actions receive them. required: trueis not enforced. GitHub does not fail the step when a required input is missing. Give every input a sensible default, or check it yourself in arunstep.- Outputs need a
value, which usually points at a step output. Here the action passes through thenode-versionoutput ofactions/setup-node, so callers can log the exact version they got.
A composite action cannot read the secrets context. If it needs a secret, the caller passes it in as an input, which is why npm-token exists. The value stays masked in logs because it came from a secret.
Check it worked
actionlint checks workflows, not action.yml on its own, so check the metadata against the published schema for action files. check-jsonschema carries a copy of that schema. Install it with pipx if you do not have it yet:
pipx install check-jsonschema==0.39.0Then check the file:
cd ~/ci-shared-demo/ci-shared
check-jsonschema --builtin-schema vendor.github-actions .github/actions/node-setup/action.ymlIt prints ok -- validation done. The next steps also catch most of the same mistakes through actionlint, because a local workflow will use the action.
Step 3 · Write the reusable workflow
A reusable workflow is an ordinary workflow file in .github/workflows/ whose trigger is workflow_call. Under that trigger you declare its interface: typed inputs, the secrets it accepts and the outputs it returns.
cd ~/ci-shared-demo/ci-shared
cat > .github/workflows/node-ci.yml <<'YAML'
name: Node CI (reusable)
on:
workflow_call:
inputs:
node-version:
description: Node.js version for every job
type: string
default: '26'
working-directory:
description: Folder that holds package.json
type: string
default: '.'
build:
description: Run npm run build and upload dist/ as an artifact
type: boolean
default: true
secrets:
NPM_TOKEN:
description: Read token for a private npm registry
required: false
outputs:
node-version:
description: The exact Node.js version the tests ran on
value: ${{ jobs.test.outputs.node-version }}
artifact-name:
description: Name of the uploaded build artifact (empty when build is false)
value: ${{ jobs.build.outputs.artifact-name }}
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
outputs:
node-version: ${{ steps.setup.outputs.node-version }}
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Set up Node.js and install dependencies
id: setup
uses: YOUR-ORG/ci-shared/.github/actions/node-setup@v1
with:
node-version: ${{ inputs.node-version }}
working-directory: ${{ inputs.working-directory }}
npm-token: ${{ secrets.NPM_TOKEN }}
- name: Run the tests
working-directory: ${{ inputs.working-directory }}
run: npm test
build:
if: inputs.build
needs: test
runs-on: ubuntu-latest
outputs:
artifact-name: ${{ steps.name.outputs.artifact-name }}
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: YOUR-ORG/ci-shared/.github/actions/node-setup@v1
with:
node-version: ${{ inputs.node-version }}
working-directory: ${{ inputs.working-directory }}
npm-token: ${{ secrets.NPM_TOKEN }}
- name: Build
working-directory: ${{ inputs.working-directory }}
run: npm run build
- name: Choose the artifact name
id: name
run: echo "artifact-name=dist-${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
- uses: actions/upload-artifact@v7
with:
name: ${{ steps.name.outputs.artifact-name }}
path: ${{ inputs.working-directory }}/dist
if-no-files-found: error
YAMLSome details that are easy to get wrong:
- The composite action is referenced by its full name,
YOUR-ORG/ci-shared/.github/actions/node-setup@v1, not./.github/actions/node-setup. A reusable workflow replaces a job in the caller, and its jobs run in the caller's context, soactions/checkoutchecks out the caller's repository. A./path would look for the action inshop-web, where it does not exist. ReplaceYOUR-ORGhere before you commit. - Outputs pass through two layers. A step writes to
$GITHUB_OUTPUT, the job maps it underjobs.<id>.outputs, andon.workflow_call.outputsmaps the job output to a workflow output. Skip a layer and the caller receives an empty string. - Inputs are typed.
buildis aboolean, soif: inputs.buildworks without comparing strings, and callers passtrueorfalse, not quoted text. - The caller's
env:does not cross over. Environment variables set at the top of the calling workflow are not visible here. Anything the shared workflow needs must be an input. - Permissions can only shrink. The
GITHUB_TOKENa called workflow gets is the caller's, and the called workflow can lower its permissions but never raise them.
Check it worked
cd ~/ci-shared-demo/ci-shared
actionlint .github/workflows/node-ci.yml
echo "actionlint exit code: $?"actionlint prints nothing and the exit code is 0.
Step 4 · Test the shared pieces in their own repository
Before another repository depends on ci-shared, give it a workflow of its own that uses both pieces on the example project. Here the job checks out ci-shared itself, so local ./ paths are correct. This workflow runs on every pull request to the shared repository, which means a broken change shows up there and not in five application repositories at once.
cd ~/ci-shared-demo/ci-shared
cat > .github/workflows/self-test.yml <<'YAML'
name: Self-test
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
composite-action:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- name: Use the action from this commit
id: setup
uses: ./.github/actions/node-setup
with:
working-directory: fixtures/hello-node
- name: Show the installed version
env:
NODE_VERSION: ${{ steps.setup.outputs.node-version }}
run: echo "Installed Node.js $NODE_VERSION"
reusable-workflow:
uses: ./.github/workflows/node-ci.yml
with:
working-directory: fixtures/hello-node
build: true
YAMLWhen the call is local (./.github/workflows/...), actionlint reads the called file and checks the inputs, secrets and outputs against it. Try a mistake on purpose: this throwaway workflow misspells an input.
cd ~/ci-shared-demo/ci-shared
cat > .github/workflows/broken.yml <<'YAML'
name: Broken
on: pull_request
permissions:
contents: read
jobs:
reusable-workflow:
uses: ./.github/workflows/node-ci.yml
with:
node_version: '26'
YAML
actionlint .github/workflows/broken.yml > lint.txt 2>&1 || echo "actionlint found problems, as expected"
cat lint.txt
grep -q 'node_version' lint.txt
rm .github/workflows/broken.yml lint.txtactionlint reports that node_version is not defined in the reusable workflow and lists the inputs that are: build, node-version and working-directory. Without this check the mistake would only show up once the workflow ran on GitHub.
Check it worked
cd ~/ci-shared-demo/ci-shared
actionlint
echo "actionlint exit code: $?"With no arguments, actionlint checks every workflow in the repository and prints nothing.
One limit to know: the reusable workflow still uses the composite action at @v1, the released version, even in this self-test. A pull request that changes both action.yml and node-ci.yml tests the new workflow with the old action. The composite-action job covers the action on its own; keep changes to the two files in separate pull requests when you can.
Step 5 · Tag a release
Callers should pin the shared repository to a tag, not to main. Otherwise every merge to ci-shared changes the CI of every repository immediately, with no review on their side.
A common scheme is a fixed tag for each release (v1.0.0) plus a moving major tag (v1) that you move forward for compatible changes. Commit, then create both:
cd ~/ci-shared-demo/ci-shared
git add .
git commit -m "Add node-setup composite action and node-ci reusable workflow"
git tag -a v1.0.0 -m "node-setup and node-ci, first release"
git tag v1
git tag --listCreate an empty repository called ci-shared under YOUR-ORG on GitHub, then push the branch and both tags in one command:
git remote add origin git@github.com:YOUR-ORG/ci-shared.git
git push -u origin main v1.0.0 v1Push the tags with the branch, not after it. The push to main starts self-test.yml straight away, and its reusable-workflow job runs node-ci.yml, which uses the action at @v1. If v1 is not on GitHub yet, that first run fails because the action is not found.
If ci-shared is private or internal, other repositories cannot use it until you allow them. In ci-shared, open Settings, then Actions, then General, and under Access choose the option that allows repositories in your organisation to use its workflows and actions. Without this, callers fail with an error saying the workflow or action was not found.
Check it worked
cd ~/ci-shared-demo/ci-shared
git rev-parse v1^{commit}
git rev-parse v1.0.0^{commit}Both commands print the same commit hash: v1 and v1.0.0 point at the same commit. On GitHub, both tags are listed under the repository's tags.
Step 6 · Call both from two repositories
Now the application repositories. shop-web passes its secret explicitly and uses the workflow's outputs in a job of its own:
mkdir -p ~/ci-shared-demo/shop-web/.github/workflows
cd ~/ci-shared-demo/shop-web
git init
cat > .github/workflows/ci.yml <<'YAML'
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
ci:
uses: YOUR-ORG/ci-shared/.github/workflows/node-ci.yml@v1
with:
node-version: '26'
secrets:
NPM_TOKEN: ${{ secrets.NPM_READ_TOKEN }}
report:
needs: ci
runs-on: ubuntu-latest
steps:
- name: Report what was built
env:
ARTIFACT: ${{ needs.ci.outputs.artifact-name }}
NODE_VERSION: ${{ needs.ci.outputs.node-version }}
run: echo "Built $ARTIFACT on Node.js $NODE_VERSION"
YAMLThe whole CI is now six lines in the ci job. NPM_READ_TOKEN is a repository or organisation secret you add in Settings, then Secrets and variables, then Actions. The mapping renames it to the NPM_TOKEN the shared workflow declared, so each repository can keep its own secret names. If you do not use a private registry, delete the two secrets lines; the secret is optional.
shop-api is a service with no build output. It tests on two Node.js versions with a matrix and passes all of its secrets with inherit:
mkdir -p ~/ci-shared-demo/shop-api/.github/workflows
cd ~/ci-shared-demo/shop-api
git init
cat > .github/workflows/ci.yml <<'YAML'
name: CI
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
ci:
strategy:
fail-fast: false
matrix:
node: ['24', '26']
uses: YOUR-ORG/ci-shared/.github/workflows/node-ci.yml@v1
with:
node-version: ${{ matrix.node }}
build: false
secrets: inherit
YAMLCheck it worked
for repo in shop-web shop-api; do
(cd ~/ci-shared-demo/$repo && actionlint && echo "$repo: no problems")
doneBoth lines end with no problems. For a remote call actionlint checks the shape of the job and the uses reference but cannot read the called file, which is one more reason the self-test in ci-shared matters. After you push shop-web to GitHub, its run shows the ci job expanded into ci / test and ci / build, each with its own log, followed by report printing the artifact name and the Node.js version.
Step 7 · Choose between explicit secrets and inherit, and know the limits
secrets: inherit is shorter, but it hands the called workflow every repository and organisation secret the caller can see. Environment secrets never cross over, with or without inherit, because on.workflow_call does not support the environment keyword. That is reasonable when ci-shared is owned by the same team. It is a poor fit when the shared workflow is maintained by someone else, because a later change to it can start reading secrets nobody meant to give it. Explicit mapping documents exactly what crosses over and lets you rename. inherit also only works within one organisation, or between organisations in the same enterprise.
Two more rules about secrets: they reach only the workflow you call directly, so a reusable workflow that calls another one must pass them on itself. And the job that calls a reusable workflow cannot use environment:; put the environment, and its protection rules, on a job inside the called workflow.
The limits GitHub documents at the time of writing:
- Up to ten levels of workflows can be connected, counting the top-level caller, and loops are not allowed.
- One workflow file can call at most 50 unique reusable workflows, nested ones included.
- One workflow can nest up to 10 composite actions.
- A matrix can create at most 256 jobs per run, and that includes matrix calls to reusable workflows.
- With a matrix, as in
shop-api, the workflow's outputs come from the last matrix run that finished successfully and set a value. Do not rely on them to tell the Node.js 24 and 26 runs apart. - A reusable workflow is called by a job, never from inside
steps:. To mix shared and repository-specific steps in one job, use a composite action.
For extra safety, pin to a commit hash instead of a tag, with the tag as a comment, so a moved tag cannot change your CI without a pull request. Tools such as Dependabot and Renovate update those hashes for you:
cd ~/ci-shared-demo/ci-shared
echo "uses: YOUR-ORG/ci-shared/.github/workflows/node-ci.yml@$(git rev-parse v1.0.0^{commit}) # v1.0.0"Check it worked
The command prints a uses: line ending in a 40-character hash and # v1.0.0. Before you move on, decide for each caller which form it should use, and write the reason in a comment above the uses: line.
Step 8 · Release a compatible change
When you improve ci-shared without breaking callers, for example by adding an optional input, release v1.1.0 and move v1 to it. Callers on v1 pick up the change on their next run; callers pinned to a hash or to v1.0.0 do not.
cd ~/ci-shared-demo/ci-shared
git commit --allow-empty -m "Compatible change for v1.1.0"
git tag -a v1.1.0 -m "Compatible change"
git tag -f v1Moving a tag rewrites it on GitHub, so it needs a forced push of that one tag:
git push origin v1.1.0
git push --force origin v1A change that breaks callers, such as renaming an input or making a secret required, gets a new major tag, v2, so repositories move when they are ready.
Check it worked
cd ~/ci-shared-demo/ci-shared
test "$(git rev-parse v1^{commit})" = "$(git rev-parse v1.1.0^{commit})" && echo "v1 now points at v1.1.0"
test "$(git rev-parse v1.0.0^{commit})" != "$(git rev-parse v1^{commit})" && echo "v1.0.0 is unchanged"Both messages print.
When to use which
Question Composite action Reusable workflow
------------------------------------- ---------------------- ------------------------
Called from a step (steps: uses:) a job (jobs: uses:)
Contains steps only one or more jobs
Chooses its own runner no, uses the caller's yes, each job sets runs-on
Reads secrets no, pass them as inputs yes, declared or inherit
Caller adds its own steps around it yes, same job no, add separate jobs
Logs inside the caller step every job and step apart
Good for setup, small helpers a whole pipeline or policyA rule of thumb: if you want every repository to run the same pipeline, write a reusable workflow. If you want every job to start the same way and then do its own thing, write a composite action. Many teams use both, as here, with the reusable workflow built from composite actions.
For readers on slow or prepaid connections, shared CI also helps outside GitHub: one place to turn on the npm cache means less downloading for every repository, and a single fixed Node.js version per tag makes runs easier to reproduce on your own machine.
Step 9 · Clean up
When you have finished with the demo, remove the local folder:
rm -rf ~/ci-shared-demoIf you created the three GitHub repositories only for this tutorial, delete them too. With the GitHub CLI (it asks you to confirm each one, and needs the delete_repo scope, which gh auth refresh -s delete_repo adds):
gh repo delete YOUR-ORG/ci-shared
gh repo delete YOUR-ORG/shop-web
gh repo delete YOUR-ORG/shop-apiDeleting shop-web also deletes its NPM_READ_TOKEN repository secret. If you added it as an organisation secret instead, remove it under the organisation's Settings, then Secrets and variables, then Actions, or with gh secret delete NPM_READ_TOKEN --org YOUR-ORG. Revoke the npm token itself on the registry if nothing else uses it.
Check it worked
test ! -e ~/ci-shared-demo && echo "demo folder removed"It prints demo folder removed, and the three repositories no longer appear in your organisation's repository list.
Next steps
- Make the shared jobs faster with caching: /tutorials/cache-dependencies-and-build-outputs-in-github-actions
- Test across systems and versions: /tutorials/run-a-build-matrix-across-operating-systems-and-versions
- If you are new to workflows, start with /tutorials/github-actions-first-workflow
Related tutorials
- How to Create Your First GitHub Actions WorkflowYou will write a workflow file that runs on every push and every pull request, check its syntax on your own computer with actionlint, push it, and read the run on GitHub.
- Cache dependencies and build outputs in GitHub ActionsYou will have a GitHub Actions workflow that restores npm packages and a Next.js build cache under keys you designed, skips the install on an exact hit, and passes actionlint, plus a script that shows the keys on your own machine.
- How to Run Node.js Tests Automatically with GitHub ActionsYou will write tests with Node.js's built-in test runner, run them on every push and pull request with a GitHub Actions workflow that caches npm downloads and tries two Node.js versions, and check the workflow file locally before you push it.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials