Caching, artifacts and matrix builds

Intermediate · 16 min

Speed up CI by caching dependencies, pass build output between jobs as artifacts, and test several Node versions with a matrix, avoiding name clashes.

Before thisWorkflows, triggers, jobs and stepsRunners and the marketplaceSecrets, variables and contexts

Mickey Gousset · 16 min

Caching, artifacts and matrix builds

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Mickey GoussetReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A cache saves files, usually downloaded dependencies, under a key so later runs can restore them instead of downloading again, and a key built from the lockfile's hash changes exactly when the dependencies do.
  • A cache is an optimisation you can lose at any time, while an artifact is a named output of a run that you upload and download on purpose.
  • Jobs run on separate machines, so build output reaches another job only as an artifact, and that job must declare needs on the one that produced it.
  • A matrix runs the same job for each combination of values, such as three Node versions, and each combination is a separate job.
  • Artifacts are immutable and need unique names, so in a matrix include the matrix value in the artifact name.

Three tools for three problems

As the notes project’s tests grow, three problems appear in CI.

  1. Every run starts from an empty machine and downloads the same dependencies again. That is slow and wasteful. Fix: cache.
  2. One job builds something, and another job needs it. Jobs run on separate machines. Fix: artifacts.
  3. The project claims to work on several Node versions, but you test only one. Fix: a matrix.

Caching

A cache stores files under a key. On the next run, the workflow asks for that key. If it exists, the files are restored and the download is skipped. If not, the job runs normally and saves the files at the end.

The usual key is built from a hash of the lockfile:

- uses: actions/cache@v6
  with:
    path: ~/.npm
    key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
    restore-keys: |
      npm-${{ runner.os }}-

When package-lock.json is unchanged, the key matches exactly and the cache is restored. When someone changes a dependency, the hash changes, the exact key misses, and restore-keys falls back to the most recent cache with the matching prefix. That is a stale but useful starting point, and a fresh cache is saved under the new key at the end if the job succeeds. According to the docs I read on 2026-11-23, caches cannot be modified once created, so a changed set of files needs a new key.

Facts about caches from GitHub’s “Dependency caching” page:

  • Entries not accessed in over 7 days are removed.
  • Each repository gets 10 GB by default, and the oldest-accessed caches are evicted first when it is full.
  • A run can restore caches from its own branch and the default branch, and for pull requests the base branch. Sibling branches cannot read each other’s caches.

For npm you often do not need actions/cache directly. actions/setup-node can do it for you:

- uses: actions/setup-node@v7
  with:
    node-version: 24
    cache: npm

I read the action.yml for setup-node at v7, and it documents a cache input that accepts npm, yarn or pnpm, and a cache-dependency-path for the lockfile. It also has a package-manager-cache input, default true, which the file says enables caching automatically when package.json names npm in its packageManager or devEngines.packageManager field. If you rely on cache: npm, make sure the lockfile exists. I made one for the notes project with npm install --package-lock-only, then confirmed npm ci and npm test pass from it, but I could not test the cache action’s behaviour on GitHub.

Another finding from reading actions/cache’s action.yml at v6: the save-always input is marked deprecated, with a message that it “does not work as intended and will be removed”. Plenty of tutorials still use it. The file points to a separate actions/cache/restore and save approach instead.

A cache is a speed-up, never a requirement. Treat a cache miss as normal. Never put anything in a cache that your build cannot recreate. The docs also warn that cache contents are not signed or verified.

Artifacts

An artifact is a named bundle of files that a run uploads on purpose. You use them to hand build output to a later job, or to keep results, such as a test report or a built site, so you can download them from the run page.

- uses: actions/upload-artifact@v7
  with:
    name: site
    path: dist
    retention-days: 7
    if-no-files-found: error

# in a later job
- uses: actions/download-artifact@v8
  with:
    name: site
    path: site

Details from the action.yml files at upload-artifact@v7 and download-artifact@v8, which I read on 2026-11-23:

  • if-no-files-found defaults to warn, which only logs a warning when the path matches nothing. Set error so a broken build cannot upload an empty artifact and pass.
  • retention-days has a minimum of 1, and a maximum of 90 unless the repository settings change it. 0 means the default.
  • Hidden files are excluded by default. include-hidden-files defaults to false.
  • overwrite defaults to false: uploading a name that already exists in the run fails. The docs note artifacts are immutable. A changed file needs a new name.
  • Download takes a name, or a pattern, and with several matches merge-multiple controls whether they land in one folder or separate folders.

Because jobs run on separate machines, the order matters. The downloading job must have needs: pointing at the uploading job, or it may look for the artifact before it exists.

Matrix builds

The notes CLI should work on every Node version we support. The Node release schedule I read on 2026-11-23 shows 22, 24 and 26 all still in support. A matrix runs one job definition once per combination of values.

name: CI

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node: [22, 24, 26]
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: ${{ matrix.node }}
          cache: npm
      - run: npm ci
      - run: npm test
      - run: npm run build
      - uses: actions/upload-artifact@v7
        with:
          name: site-node-${{ matrix.node }}
          path: dist
          retention-days: 7
          if-no-files-found: error

This creates three separate jobs, one for each Node version, running in parallel. Each is named after its matrix values in the checks list. That has a consequence for branch protection: a required check must name a job that exists, and matrix jobs get their value in the name, such as test (22).

  • fail-fast: false. By default the first failure cancels the rest. Turn it off when you want to see exactly which versions break.
  • ${{ matrix.node }} reads the current value, from the matrix context.
  • The artifact name includes the matrix value. All three legs upload a build, and with overwrite false, the same name would fail. This is the commonest matrix-and-artifact mistake.

You can also add fields with include, remove combinations with exclude, and matrix over operating systems with runs-on: ${{ matrix.os }}. Each extra axis multiplies the jobs, so three Node versions on three operating systems is nine jobs, and runner minutes on private repositories are billed for each.

I validated the YAML above with a parser, ran npm ci, npm test (three tests pass) and npm run build locally on Node 26.13.1, and did not run the matrix on Node 22 or 24, or any of the actions. Everything about GitHub’s behaviour here comes from the docs and the action.yml files.

What the video covers

Mickey Gousset’s “Mastering Matrix Jobs in GitHub Actions” is about 16 minutes and was published around 2025 (YouTube shows “1 year ago”). The description says it is a deep dive into matrix jobs. This video covers only the matrix third of the lesson. I found no single established English video that covers cache, artifacts and matrix together, and I could not get chapter lists to pick a range from a long course, so caching and artifacts here come from the docs and the action.yml files, and two videos are linked under deeper: Mickey’s cache video (about 2023, so check version numbers if you follow along) and a 75-minute lecture from Cloud With VarJosh, published around July 2026, whose description covers artifacts, cache keys, restore keys and retention with a Python and pip caching demo. It has no chapter list, so I did not clip it, and it is too long to embed. Mickey Gousset works at GitHub, and the video is on his own channel, not GitHub’s. I could not read a transcript or chapters. English is judged from the title, description and channel.

Where this fits

Your workflow is now fast and tests several versions. As the number of repositories grows, the same YAML gets copied between them, which is the next problem: reusable workflows and composite actions.

Check yourself

  1. 01Why use hashFiles of the lockfile in a cache key?

    The key then changes only when the dependencies change. An exact match restores the cache, and a changed lockfile misses and saves a fresh one, so you never restore stale dependencies for a new lockfile.

  2. 02What is the difference between a cache and an artifact?

    A cache is a best-effort speed-up that GitHub may evict, keyed for reuse across runs. An artifact is a deliberate output of one run, kept for a set number of days, that you download or pass to another job.

  3. 03A matrix of three Node versions all upload an artifact called site. What happens?

    The uploads clash. With the default settings, uploading a name that already exists in the run fails, so give each leg its own name, such as site-${{ matrix.node }}.

  4. 04Why does fail-fast: false matter on a matrix?

    By default the first failing combination cancels the rest. Setting it to false lets every combination finish, so you see exactly which Node versions break.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark