Lesson content
Read, practise, then check your understanding
Advanced Workflows: Matrices, Caching, and Reuse
Advanced workflows remove duplication and shorten feedback without hiding behavior. A matrix expands one job into multiple combinations. Caches reuse expensive dependency data, artifacts transfer or retain outputs, and reusable workflows centralize an entire audited process.
Core ideas
strategy.matrixcreates one job per combination and supportsincludeandexcludeadjustments.fail-fastcontrols whether one matrix failure cancels other in-progress combinations.- Caches are keyed snapshots intended for reusable dependencies; artifacts are outputs from a particular run.
- A reusable workflow declares
workflow_callinputs, secrets, and outputs and is invoked at job level.
How it works
Build cache keys from the operating system, tool version, and lockfile hash. Restore keys can accept older compatible entries, but an incorrect cache must never change correctness. Use artifacts for compiled packages, test reports, or diagnostics that downstream jobs or people need.
Configuration example
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [20, 22]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm test
Read the example from top to bottom: the trigger creates a run, the job requests a runner, and each step receives only the context configured for it. Adapt names, versions, permissions, and commands to the repository rather than copying production credentials or policies blindly.
Production guidance
- Keep the matrix to supported combinations; exhaustive combinations can multiply cost quickly.
- Do not cache secrets, build outputs that should be rebuilt, or mutable global state.
- Version reusable workflow contracts and test changes with representative callers.
The chapter quiz follows this lesson and checks both the vocabulary and the operational decisions behind the configuration.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.