Chapter 9: GitHub Actions Hands-On
9.7b Matrix and reuse (awareness only)
Why: the problem
Suppose we are at Freshworks. The Node.js library must work on Node 18, 20 and 22, and on Linux and Windows. Writing six almost-same jobs means six places to update. And there are ten repos, each with the same 60-line test workflow copied by hand. When a step changes, someone edits ten files and misses two. Matrix fixes the first problem: one job, many combinations. Reusable workflows fix the second: one shared workflow file that many repos call.
What we will build
You will run one test job across Node versions and operating systems with a matrix, include and exclude combinations, and see fail-fast behaviour. Then you will make a reusable workflow with inputs and secrets and call it from two app repos. Stacks: Node.js, Python and Java.
What: every term in plain English
- Matrix — A list of values. The job runs once for each combination.
- include — Adds an extra combination or extra values to one combination.
- exclude — Removes a combination you do not need.
- fail-fast — If true, one failed combination cancels the rest. Default true.
- max-parallel — How many combinations run at once.
- Reusable workflow — A workflow with on: workflow_call that other workflows can call like a function.
- inputs — Values the caller passes, like the Node version.
- secrets: inherit — The caller passes all its secrets to the reusable workflow.
How: step by step
-
Write one test job with a matrix
strategy: matrix: node: [18, 20, 22]What it does: Three runs from one job.
Output
test (18) test (20) test (22) -
Add the OS dimension
os: [ubuntu-latest, windows-latest]What it does: Now 3 x 2 = 6 runs.
Output
6 jobs queued -
Create the shared workflow repo
mkdir -p ci-templates/.github/workflowsWhat it does: One repo for templates, owned by the platform team.
Output
(no output) -
Call it from an app
uses: freshworks-lab/ci-templates/.github/workflows/node-ci.yml@v1What it does: Pin to a tag.
Output
call / test Success
Example 1: Node
Stack. Node.js 18/20/22, Linux and Windows
Goal. Node.js matrix across versions and operating systems.
.github/workflows/ci.yml
name: ci
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
exclude:
- os: windows-latest
node: 18
include:
- os: ubuntu-latest
node: 22
coverage: true
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci && npm test
- if: matrix.coverage
run: npm run coverage
What each line does
runs-on: ${{ matrix.os }}— The OS comes from the matrix.fail-fast: false— Let all combinations finish so you see every failure.exclude— Skip Windows plus Node 18.include ... coverage: true— Add a flag to one combination only.if: matrix.coverage— Run coverage once, not six times.
Green output
test (ubuntu-latest, 18) Success 31s
test (ubuntu-latest, 20) Success 29s
test (ubuntu-latest, 22) Success 44s (coverage 91%)
test (windows-latest, 20) Failure 58s
test (windows-latest, 22) Failure 55s
Error: ENOENT: no such file or directory, open 'data\\menu.json'
Expected result. You found a Windows path bug that no Linux developer would see. Use path.join to fix it.
Example 2: Python: test against many versions with max-parallel
Stack. Python 3.10 to 3.13
Goal. Python: test against many versions with max-parallel.
.github/workflows/ci.yml
name: ci
on: [pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
max-parallel: 2
matrix:
python: ["3.10", "3.11", "3.12", "3.13"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
cache: pip
- run: pip install -r requirements.txt && pytest -q
What each line does
python: ["3.10", ...]— Quotes matter: unquoted 3.10 becomes 3.1.max-parallel: 2— Only two at a time, to save shared runners.setup-python with matrix value— Each run gets its own Python.
Green output
test (3.10) Success
test (3.11) Success
test (3.12) Success
test (3.13) Failure
E DeprecationWarning: datetime.datetime.utcnow() is deprecated
Expected result. You know before users do that Python 3.13 needs a small fix.
Example 3: Reusable workflow for Java apps, called from two repos
Stack. Java 17/21, Maven, reusable workflow
Goal. Reusable workflow for Java apps, called from two repos.
ci-templates/.github/workflows/java-ci.yml
name: java-ci
on:
workflow_call:
inputs:
java-version:
type: string
default: "17"
secrets:
SONAR_TOKEN:
required: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: ${{ inputs.java-version }}
cache: maven
- run: ./mvnw -B -ntp verify
orders-service/.github/workflows/ci.yml
name: ci
on: [push, pull_request]
jobs:
call:
uses: freshworks-lab/ci-templates/.github/workflows/java-ci.yml@v1
with:
java-version: "21"
secrets: inherit
What each line does
on: workflow_call— This workflow can be called by others.inputs: java-version— A parameter with a default.secrets: SONAR_TOKEN— Declares which secrets it may receive.uses: org/repo/.github/workflows/file.yml@v1— Call the template at tag v1.secrets: inherit— Pass the caller secrets along.
Green output
call / build Success 52s
[INFO] Tests run: 40, Failures: 0
[INFO] BUILD SUCCESS
Expected result. Ten Java repos now have a 7-line ci.yml. One change in the template, tagged v2, upgrades them all.
Verify: did it work?
-
Count matrix jobs
gh run view --json jobs --jq ".jobs[].name"What it does: Lists every combination.
Output
test (ubuntu-latest, 18) test (ubuntu-latest, 20) ... -
Check which template version ran
Open the run > the call job headerWhat it does: Shows the template file and ref.
Output
freshworks-lab/ci-templates/.github/workflows/java-ci.yml@v1
When it is red 1
Error: The template is not valid. Matrix vector 'node' does not contain any values
Cause. An empty or wrongly indented list.
Fix, step by step
- Check the list under matrix.
- Use [18, 20, 22] style.
When it is red 2
error parsing called workflow: workflow is not reusable as it is missing a `on.workflow_call` trigger
Cause. The template has no workflow_call.
Fix, step by step
- Add on: workflow_call to the template.
- Tag a new version.
When it is red 3
Unable to resolve action freshworks-lab/ci-templates, repository not found
Cause. The template repo is private and not shared with the caller.
Fix, step by step
- In the template repo settings, allow access from repos in the organisation.
- Check the path and tag spelling.
Cleanup
-
Delete an old template tag
git tag -d v0 && git push origin :refs/tags/v0What it does: Remove tags nobody uses.
Output
- [deleted] v0
Ravindra Bagale's Tip
Do not copy six jobs. Use a matrix, and keep one template!
सहा job copy करू नका. Matrix वापरा, आणि template एकच ठेवा!
छह job copy मत करो. Matrix इस्तेमाल करो, और template एक ही रखो!
Practice tasks
Easy Version matrix
Why: Catch version bugs.
What to do: Test on three versions.
- Add matrix.node.
Check: Three runs.
Medium Exclude and include
Why: Shape the matrix.
What to do: Skip one combo and add a flag.
- Use exclude and include.
Check: The run list matches.
Hard Template
Why: Stop copy-paste.
What to do: Make a reusable workflow with one input.
- workflow_call, inputs, call it.
Check: The app ci.yml is under 10 lines.
Easy Say matrix and reusable workflows in two lines
Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.
What to do: Write two short lines about matrix and reusable workflows for a fresher who knows only Linux and Git basics.
- Write the problem it solves.
- Write the one workflow line or command you use for it.
- Read it aloud to a friend.
Check: A fresher understands it without asking a question.
Easy Make a throwaway practice repo
Why: Practice needs a repo you can break. If the workflow history gets messy, you delete the repo and start again.
What to do: Create a new empty repo named actions-lab with one small app and push it.
- mkdir actions-lab && cd actions-lab && git init -b main
- Add app code and a test file.
- git remote add origin https://git.example.com/ravi/actions-lab.git && git push -u origin main
Check: The Actions tab of the repo shows your first run.
Medium Do it on a second stack
Why: A CI idea must work for Node.js, Python and Java. Only the setup and test lines should change.
What to do: Repeat this lesson with a different language than the one you used first.
- If you used Python, use Node.js now.
- Change only the setup action and the test command.
- Keep the same job names.
Check: Same result, and the job names did not change.
Medium Break it on purpose
Why: You must know the red log by face. In a real outage there is no time to read slowly.
What to do: Cause one error from the "When it is red" part of this lesson and fix it.
- Follow the error section.
- Copy the exact message into your notes.
- Write the fix in one line.
Check: You fixed it using only the steps, without searching the web.
Medium Write a one-page runbook
Why: At 2 a.m. nobody remembers YAML keys. A short page saves the on-call person.
What to do: Write a one-page runbook for this lesson.
- Title, when to use, exact YAML or commands, how to check, how to undo.
Check: A teammate can follow it alone.
Hard Explain it like an interview
Why: Interviewers want real stories, not definitions.
What to do: Record a 60 second answer about this lesson.
- Say the problem, the workflow lines you wrote, and one error you hit.
Check: Your answer has a real YAML line and a real error message.
Hard Draw the flow top to bottom
Why: A drawing shows the order of work and which job waits for which.
What to do: Draw this lesson as boxes from top to bottom.
- Start at git push, go through each job and step, end at the green check or the deployed app.
Check: Each box matches a real step in your run log.
Homework: try at home
Try at home
At home, add a Node or Python version matrix to your lab repo with fail-fast: false. Then move the test job into a reusable workflow and call it.
Recap
- Matrix runs one job across many versions and OSes.
- Use include, exclude and fail-fast to shape it.
- Reusable workflows share CI across repos.
- Pin templates with tags.
- Pass secrets explicitly or with inherit.
FAQ
Is there a limit on matrix size?
Yes, up to 256 jobs per workflow run.
Matrix costs more minutes?
Yes, each combination is a job. Keep it meaningful.
Reusable workflow vs composite action?
Reusable workflows hold whole jobs; composite actions bundle steps inside one job.
Can a reusable workflow call another?
Yes, a few levels deep.
Can the template see caller secrets?
Only those passed or inherited.
Interview questions
-
What is a matrix strategy?
A way to run one job over combinations like versions and OSes.
-
fail-fast true vs false?
True cancels others on first failure; false runs all for full results.
-
How do you share CI across many repos?
Reusable workflows or composite actions in a central repo, pinned by tag.
-
How do you pass secrets to reusable workflows?
Declare them and pass them, or use secrets: inherit.
-
How do you control matrix cost?
exclude, max-parallel and only meaningful combinations.
Quick quiz
Q1. What does a matrix do?
Runs one job for each combination of values.
Q2. What does fail-fast: false do?
Lets all combinations finish.
Q3. How do you remove one combination?
exclude.
Q4. Which trigger makes a workflow reusable?
workflow_call.
Q5. Why pin a template to a tag?
So changes are reviewed and builds are repeatable.
Did you understand? Let us try it once more.
Revision card
Interview card