All guides

Caching in GitHub Actions: what to cache and when it hurts

A job on a GitHub-hosted runner starts from a clean image and downloads its dependencies every time. A cache keeps them between runs. Its key, its branch scope and the storage limit decide whether a run finds it.

Hands typing on a laptop that shows code

How the cache action works

actions/cache restores files when its step runs. If no cache had that exact key and the job completes successfully, the action saves a new cache from the same path. An existing cache cannot be changed: new content needs a new key.

YAML

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

Keys and restore keys

The key is any string up to 512 characters. An exact match is a cache hit, and the step sets its cache-hit output to true.

hashFiles returns one hash for the files that match a pattern. With the lockfile in the key, a dependency change produces a new key and a new cache.

restore-keys are fallbacks, tried in order when the key misses. Each one matches as a prefix, and the most recently created match wins. After such a partial match cache-hit is false, and the action's README recommends installing the missing or updated dependencies.

The caching in the setup actions

GitHub's reference says the setup actions create and restore dependency caches with minimal configuration:

  • actions/setup-node@v7 takes cache: npm, yarn or pnpm. It caches the package manager's global data and does not cache node_modules. With no cache input, it enables npm caching by itself when package.json names npm in packageManager. Set package-manager-cache: false to turn that off.
  • actions/setup-python@v7 takes cache: pip, pipenv or poetry. Caching is off by default.
  • actions/setup-go@v7 caches modules and build outputs by default, keyed on go.mod. Set cache: false to turn it off.
  • actions/setup-java@v6 takes cache: maven, gradle or sbt.

YAML

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

Set node-version to the version your project uses.

The size limit and eviction

GitHub sets no limit on the number of caches, but the total size in a repository is limited to 10 GB by default. Enterprise owners, organization owners and repository administrators can raise the limit, and usage beyond 10 GB is billed.

Two rules remove caches:

  • A cache that has not been accessed in over 7 days is removed. The repository's cache retention setting defaults to 7 days.
  • When the total exceeds the limit, GitHub saves the new cache and then evicts caches in order of last access date, oldest first, until the total is under the limit.

Which branches read which caches

A run restores caches created on its own branch or on the default branch. A pull request run can also read the caches of its base branch. No run reads the caches of a child branch, a sibling branch or another tag.

A cache saved by a pull_request run belongs to the merge ref, refs/pull/.../merge. Only re-runs of that pull request can restore it.

Runs from low-trust triggers such as pull_request_target, issue_comment and workflow_run get read-only access to the default branch's caches. A save from such a run fails with a warning and the job continues. For these runs, GitHub's reference says to keep the cache updated from a trusted workflow, such as a build triggered by a push to the default branch. The cache-mode workflow key sets the access explicitly: read, write, write-only or none.

When a cache hurts

  1. Pull request caches fill the storage. GitHub describes the case: many pull requests, each with caches restricted to it, take up most of the cache storage, and eviction then creates and deletes the default branch's caches at a high frequency. Its documented fix is a workflow that runs gh cache delete for a pull request's caches when the pull request is closed.
  2. Secrets in the cached path. Anyone who can open a pull request against the repository can read the caches of the base branch.
  3. Trusting what was restored. Cache contents are not signed or verified. GitHub tells you to treat restored files as untrusted input. The setup-node README recommends package-manager-cache: false for workflows with higher privileges or access to sensitive information.
  4. Unpinned pip requirements. The setup-python README notes that when a requirements file does not pin versions, pip installs the newest release, the restored cache goes unused and the total build time increases.

Docker layers with the gha backend

Docker's gha cache backend stores the build cache in the GitHub Actions cache. Docker's example, with docker/setup-buildx-action before the build:

YAML

- uses: docker/setup-buildx-action@v4
- uses: docker/build-push-action@v7
  with:
    context: .
    push: true
    tags: "<registry>/<image>:latest"
    cache-from: type=gha
    cache-to: type=gha,mode=max

mode=min, the default, caches only the layers that are exported into the resulting image. mode=max caches all layers, including those of intermediate steps. The scope parameter defaults to buildkit. If you build several images with the same scope, each build overwrites the cache of the previous one, so give each image its own scope.

Docker states that GitHub's size limits and cache access restrictions still apply, and it marks this backend as experimental.

What to check first

Open the repository's Actions tab and click Caches in the sidebar. The list shows how much space each cache uses, when it was created and when it was last used, and a Branch menu filters it. Look for caches that belong to pull request refs and for a total close to the limit.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides