Lesson 10 / 23
Caching & Matrix Builds
Speed up GitHub Actions with dependency caching and test across Node versions or operating systems with a build matrix.
Cache dependencies
Caching the package manager's store avoids re-downloading dependencies on every run. setup-node can do it for you.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm # restores ~/.npm keyed by package-lock.json
- run: npm ci
- run: npm testBuild matrix
A matrix runs the same job across several combinations in parallel.
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
node: [20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: "${{ matrix.node }}" }
- run: npm ci && npm testKey caches on lockfile hashes so they refresh when dependencies change. A matrix multiplies run time, so only include combinations you actually support.
Quick check
Quick check: What does a build matrix do?
- Runs a job across several combinations in parallel
- Encrypts secrets
- Deploys to production
- Deletes caches
Answer
Runs a job across several combinations in parallel — Each matrix combination becomes its own job run.