CI/CD and automation · Intermediate · 20 min
Run a GitHub Actions build matrix across operating systems and versions
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GitHub Actions · Node.js 26.13.1 · actionlint 1.7.12
You will have a GitHub Actions workflow that tests one Node.js project on Linux, macOS and Windows across three Node.js versions, with trimmed and added legs, an experimental nightly leg that cannot fail the run, and a matrix generated by a script that keeps pull request runs small.
On this page
A test suite that passes on your laptop tells you it works on one operating system with one runtime version. Your users and colleagues run other combinations: a Windows machine with an older Node.js, a Mac on the newest one. A build matrix runs the same job once for each combination you list, side by side, so a failure that only happens on Windows shows up as one red leg instead of as a bug report.
/tutorials/run-node-tests-in-github-actions used a matrix of two Node.js versions on Linux. Here you go further. You build a grid of three operating systems and three Node.js versions, cut the legs you do not need, add one that tests tomorrow's Node.js without blocking anyone, make shell steps behave the same on Windows, and finally let a script decide the matrix so pull requests get a quick run and main gets the full one. Every workflow version is checked with actionlint before it goes near GitHub.
Step 1 · Make a small project worth testing on three systems
The project is one function that splits text into lines. Line endings are the classic difference between Windows (\r\n) and everything else (\n), so it is a fair thing to test on all three systems. Create it in a throwaway folder:
mkdir -p ~/matrix-demo
cd ~/matrix-demo
git init -b main
npm init -y
mkdir -p src testcat > src/lines.js <<'JS'
// Splits text into lines, accepting Windows (\r\n) and Unix (\n) line endings.
function splitLines(text) {
if (typeof text !== 'string') {
throw new TypeError('splitLines needs a string')
}
if (text === '') {
return []
}
return text.replace(/\r?\n$/, '').split(/\r?\n/)
}
module.exports = { splitLines }
JS
cat > test/lines.test.js <<'JS'
const test = require('node:test')
const assert = require('node:assert/strict')
const os = require('node:os')
const { splitLines } = require('../src/lines.js')
test('splits Unix line endings', () => {
assert.deepEqual(splitLines('a\nb\nc'), ['a', 'b', 'c'])
})
test('splits Windows line endings', () => {
assert.deepEqual(splitLines('a\r\nb\r\n'), ['a', 'b'])
})
test('handles the line ending of the machine it runs on', () => {
const text = ['one', 'two'].join(os.EOL) + os.EOL
assert.deepEqual(splitLines(text), ['one', 'two'])
})
test('returns no lines for empty text', () => {
assert.deepEqual(splitLines(''), [])
})
JS
npm pkg set scripts.test="node --test"
npm install --package-lock-onlyThe third test uses os.EOL, which is \r\n on Windows and \n elsewhere. It is the one test whose input changes with the operating system, which is exactly what a matrix is for.
Check it worked
npm testThe summary at the end shows tests 4, pass 4 and fail 0. On your own machine only one value of os.EOL was tried. The matrix will try both.
Step 2 · Write a three-by-three matrix
A matrix is a set of named lists under strategy.matrix. GitHub multiplies them out and runs one job per combination. Two lists of three values give nine jobs. Write the first version of the workflow:
mkdir -p .github/workflows
cat > .github/workflows/test.yml <<'YAML'
name: Test
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: [22, 24, 26]
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
YAMLruns-on: ${{ matrix.os }} is what makes the operating system a dimension: each leg asks for a different runner. Node.js 22, 24 and 26 are the three supported long-term-support lines as of November 2026. Node.js moved to one major release a year from version 27, so check nodejs.org/en/about/previous-releases and update the list when a line reaches end of life.
Check it worked
actionlint
echo "actionlint exit code: $?"No output and exit code 0 mean the file is valid. When it runs on GitHub, the Actions tab lists nine jobs with names like test (windows-latest, 24).
Step 3 · Trim the grid and add an experimental leg
Nine legs is more than this project needs. On private repositories, Windows and macOS minutes cost more than Linux minutes. A common trade-off is to test the oldest supported version on Linux only, and to add one leg that tries the next Node.js before it is released. Replace the workflow:
cat > .github/workflows/test.yml <<'YAML'
name: Test
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
test:
name: ${{ matrix.os }} · Node ${{ matrix.node }}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
fail-fast: false
max-parallel: 4
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: [22, 24, 26]
exclude:
- os: macos-latest
node: 22
- os: windows-latest
node: 22
include:
- os: ubuntu-latest
node: 28-nightly
experimental: true
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
YAMLWhat each new key does:
excluderemoves combinations from the grid. Each entry must match a combination that the lists produce. GitHub processes allexcludeentries before anyinclude, so you cannot exclude a leg thatincludeadds.includeadds to the result. An entry whose values do not match any existing combination becomes a new leg, sonode: 28-nightlyadds one job. Its extra key,experimental, exists only on that leg.28-nightlyasks setup-node for the latest nightly build of the Node.js main branch, which was version 28 at the time of writing. Nightlies break, and on some days one is published without a build for every platform, which is why it runs on Linux only.continue-on-error: ${{ matrix.experimental == true }}lets that one leg fail without failing the workflow run. On every other legmatrix.experimentalis empty, so the expression isfalse.fail-fast: falsekeeps the other legs running when one fails. The default,true, cancels everything still queued or running, which hides whether a failure is specific to one combination.max-parallel: 4caps how many legs run at once. Without it, GitHub runs as many as there are runners available. A cap helps when the tests share something limited, such as a staging database or an API rate limit.namegives each leg a readable label, such aswindows-latest · Node 24. Branch protection rules refer to checks by name, so pick a format once and keep it.
Check it worked
actionlint
echo "actionlint exit code: $?"Count the legs yourself: 3 × 3 = 9, minus 2 excluded, plus 1 included, makes 8. On GitHub, the experimental leg shows its own result, but the run as a whole only fails if one of the other seven does.
ubuntu-latest: Node.js 22, 24, 26 and the 28 nightly (which may fail)macos-latest: Node.js 24 and 26windows-latest: Node.js 24 and 26
Step 4 · Let actionlint catch a matrix mistake
A typo in a matrix can cost you quietly: an exclude that matches nothing removes nothing, and you pay for legs you meant to drop. Make that mistake on purpose in a copy of the file:
cat > .github/workflows/broken.yml <<'YAML'
name: Broken
on: push
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [24, 26]
exclude:
- os: windows-latest
node: 20
steps:
- run: echo "Node ${{ matrix.node }}"
YAML
actionlint .github/workflows/broken.yml || echo "actionlint found a problem (exit code $?)"Check it worked
actionlint prints the line of the bad exclude entry with a message along the lines of the value 20 not existing in the matrix's node values, and exits with a non-zero code. Remove the broken file before you go on:
rm .github/workflows/broken.yml
actionlintStep 5 · Write shell steps that also work on Windows
On Windows runners a run: step uses PowerShell (pwsh) unless you say otherwise. A line such as echo "done" >> "$GITHUB_STEP_SUMMARY" means one thing in bash and something else in PowerShell, where $GITHUB_STEP_SUMMARY is an unset PowerShell variable. Windows runners also ship the bash from Git for Windows, so the simplest fix is to choose bash for every step in the workflow.
Two other details bite on Windows. Git can convert line endings to \r\n when it checks files out there, and bash then fails on a script with $'\r': command not found. A .gitattributes file pins scripts to \n. And a script kept in a file is easier to read and to run on your own machine than a long run: block.
Write the attributes file and a small report script:
cat > .gitattributes <<'TXT'
* text=auto
*.sh text eol=lf
TXT
mkdir -p ci
cat > ci/report.sh <<'SH'
#!/usr/bin/env bash
# Adds one line about this matrix leg to the job summary (or prints it when run by hand).
set -euo pipefail
line="- ${RUNNER_OS:-$(uname -s)}: Node $(node --version) for matrix value ${NODE_LABEL:-unknown}, platform $(node -p 'process.platform')"
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
echo "$line" >> "$GITHUB_STEP_SUMMARY"
fi
echo "$line"
SHThe script reads RUNNER_OS, which GitHub sets to Linux, Windows or macOS, and GITHUB_STEP_SUMMARY, the file whose contents appear on the run's summary page. When you run it by hand, neither is set and it falls back to printing.
Now point the workflow at bash and call the script. The matrix value reaches the script through env: rather than being pasted into the command with ${{ }}, which keeps values out of the shell's parsing:
cat > .github/workflows/test.yml <<'YAML'
name: Test
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
defaults:
run:
shell: bash
jobs:
test:
name: ${{ matrix.os }} · Node ${{ matrix.node }}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
fail-fast: false
max-parallel: 4
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: [22, 24, 26]
exclude:
- os: macos-latest
node: 22
- os: windows-latest
node: 22
include:
- os: ubuntu-latest
node: 28-nightly
experimental: true
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
- name: Report this leg
env:
NODE_LABEL: ${{ matrix.node }}
run: bash ci/report.sh
YAMLCheck it worked
Run the script the way a step would, with a summary file standing in for GitHub's:
SUMMARY_FILE="$(mktemp)"
RUNNER_OS=Linux NODE_LABEL=26 GITHUB_STEP_SUMMARY="$SUMMARY_FILE" bash ci/report.sh
cat "$SUMMARY_FILE"
rm "$SUMMARY_FILE"
actionlintThe script prints a line like - Linux: Node v26.13.1 for matrix value 26, platform linux, and the same line is in the summary file. On the Windows legs the line ends in platform win32, which is your proof that the step ran in bash on Windows.
Step 6 · Generate the matrix from a script
The full matrix is right for main. For a pull request you want a quicker answer: two operating systems and the two newest versions are usually enough to catch most mistakes, and they finish sooner, which also matters when you are watching a run from a phone on a slow or prepaid connection. A script can make that choice. Keep the version list in a plain file, so a version change is a one-line edit:
cat > ci/node-versions <<'TXT'
# Node.js lines to test, oldest first. One per line.
22
24
26
TXT
cat > ci/matrix.sh <<'SH'
#!/usr/bin/env bash
# Prints the test matrix as one line of JSON and, in a workflow, saves it as the output "matrix".
# EVENT_NAME=pull_request gives a small matrix; anything else gives the full one.
set -euo pipefail
nightly="28-nightly"
versions=()
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in ''|'#'*) continue ;; esac
versions+=("$line")
done < ci/node-versions
if [ "${#versions[@]}" -lt 2 ]; then
echo "ci/node-versions needs at least two versions" >&2
false
fi
json_list() {
local out="" item
for item in "$@"; do
out="${out:+$out,}\"$item\""
done
printf '[%s]' "$out"
}
count="${#versions[@]}"
if [ "${EVENT_NAME:-push}" = "pull_request" ]; then
newest=("${versions[@]:$((count - 2))}")
matrix="{\"os\":$(json_list ubuntu-latest windows-latest),\"node\":$(json_list "${newest[@]}")}"
else
oldest="${versions[0]}"
matrix="{\"os\":$(json_list ubuntu-latest macos-latest windows-latest),\"node\":$(json_list "${versions[@]}"),"
matrix+="\"exclude\":[{\"os\":\"macos-latest\",\"node\":\"$oldest\"},{\"os\":\"windows-latest\",\"node\":\"$oldest\"}],"
matrix+="\"include\":[{\"os\":\"ubuntu-latest\",\"node\":\"$nightly\",\"experimental\":true}]}"
fi
if [ -n "${GITHUB_OUTPUT:-}" ]; then
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
fi
echo "$matrix"
SHThe script sticks to features of bash 3.2, the version macOS ships, so it runs the same on your Mac, on Linux and in Git Bash on Windows. It excludes whichever version is oldest in the file, so when you drop 22 and add a new line, the exclusions follow.
Check it worked
Install jq to read the JSON (brew install jq on macOS), then run the script for both kinds of event and count the legs with the same arithmetic as before:
sudo apt update
sudo apt install -y jq
EVENT_NAME=push bash ci/matrix.sh | jq .
for event in push pull_request; do
legs="$(EVENT_NAME="$event" bash ci/matrix.sh | jq '(.os | length) * (.node | length) - ((.exclude // []) | length) + ((.include // []) | length)')"
echo "$event: $legs legs"
doneThe first command prints the full matrix, laid out by jq. The loop prints push: 8 legs and pull_request: 4 legs. If jq reports a parse error, the script produced broken JSON; fix that before it reaches a workflow, where the error would only show on GitHub.
Step 7 · Feed the generated matrix to the test job
A first job, plan, runs the script on a single Linux runner and publishes the JSON as a job output. The test job waits for it with needs: and turns the string back into a matrix with fromJSON. Replace the workflow one last time:
cat > .github/workflows/test.yml <<'YAML'
name: Test
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
defaults:
run:
shell: bash
jobs:
plan:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.matrix }}
steps:
- uses: actions/checkout@v7
- id: matrix
env:
EVENT_NAME: ${{ github.event_name }}
run: bash ci/matrix.sh
test:
needs: plan
name: ${{ matrix.os }} · Node ${{ matrix.node }}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental == true }}
strategy:
fail-fast: false
max-parallel: 4
matrix: ${{ fromJSON(needs.plan.outputs.matrix) }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
- name: Report this leg
env:
NODE_LABEL: ${{ matrix.node }}
run: bash ci/report.sh
YAMLThe whole matrix, including its exclude and include lists, now comes from the JSON. The output must be a single line, which is why the script builds the JSON without line breaks. A generated matrix still counts towards GitHub's limit of 256 jobs per matrix in one run.
Check it worked
actionlint
echo "actionlint exit code: $?"Exit code 0 again. actionlint cannot know which values the script will produce, so it checks the structure and the expressions around the matrix rather than its contents. That is why the previous step tested the script on its own.
Step 8 · Push it and read the run
Commit everything and push to your repository:
cat > .gitignore <<'TXT'
node_modules
TXT
git add .
git commit -m "Test on Linux, macOS and Windows with a generated matrix"
git ls-files --eol ci/matrix.shgit ls-files --eol shows attr/text eol=lf for the script, so Windows checkouts keep \n endings.
git remote add origin git@github.com:YOUR-USER/YOUR-REPO.git
git push -u origin mainReplace YOUR-USER/YOUR-REPO with your repository.
Check it worked
Open the Actions tab and the newest Test run. The graph shows plan first, then eight legs fanning out from it, with no more than four running at a time. Open plan and its matrix step: the log ends with the JSON line you saw locally. Open any Windows leg and the Report this leg step: the run's summary page lists one line per leg, and the Windows lines say platform win32.
Then open a pull request from a branch. Its run shows four legs: Linux and Windows on Node.js 24 and 26. If the nightly leg fails one morning, it gets a red mark of its own while the run stays green.
When you require checks on main in your branch protection rule, choose the stable names, such as ubuntu-latest · Node 26. Do not require the nightly leg, and remember that pull requests only produce four of the eight names, so require a check that exists on both, or add a final job that needs: test and require that one.
Next steps
- Make each leg faster by caching npm downloads and build output: /tutorials/cache-dependencies-and-build-outputs-in-github-actions
- Revisit the single-OS starting point and branch protection: /tutorials/run-node-tests-in-github-actions
- Deploy once every leg passes: /tutorials/deploy-to-a-server-over-ssh-with-github-actions
- Go back over the workflow file basics: /tutorials/github-actions-first-workflow
Related tutorials
- 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.
- 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 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.
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