Caches
A cache keeps directories between runs, such as a Go module cache, ~/.npm or ~/.m2, so the
next run doesn't download everything again. Files you want to download or hand to a later flow
step are artifacts, not caches.
In the run spec
caches is a list of up to 10 caches, in any container or VM run: the New run form, the API,
templates, .forge/ run files and flow steps.
image: golang:1.24
working_dir: /workspace
env: { GOMODCACHE: /cache/go/mod, GOCACHE: /cache/go/build }
caches:
- key: go-${runner.arch}-${hashFiles('**/go.sum')}
restore_keys: [go-${runner.arch}-]
paths: [/cache/go/mod, /cache/go/build]
commands:
- run: go test ./...
- key (required): the entry to restore and the name to save under. At most 255 bytes once filled in.
- restore_keys: up to 10 prefixes, tried in order when
keyhas no entry. The newest entry whose key starts with the prefix is restored. - paths (required): 1 to 10 absolute directories or files. They can't be below
/proc,/sys,/devor/run, and paths of one run's caches can't overlap. - policy:
pull-push(the default) restores and saves,pullonly restores,pushonly saves. - save:
on_success(the default) saves once the process exited 0;alwayssaves whatever the exit code. - timeouts.cache_seconds: how long restoring and saving may each take. 600 by default, at most 3600.
Compose runs can't have caches.
Keys
These are filled in when the run starts:
${hashFiles('glob', …)} |
a SHA-256 of the files below /workspace that match, up to 10 globs; ** matches any depth |
${git.branch} |
the run's branch, empty without one |
${git.ref} |
the run's ref, such as refs/heads/main, empty without Git |
${runner.arch} |
the runner's architecture: amd64 or arm64 |
${params.NAME} |
in templates and flows |
Write $${ for a literal ${. Put ${runner.arch} in the key of anything compiled or native,
since an arm64 runner can't use an amd64 build cache.
Entries never change. The first save of a key wins; a later run with the same key restores it and doesn't save. Put what changes, such as a lockfile's hash, in the key, and keep a restore key that matches the previous entry.
When they run
- The run checks out its commit and its inputs into
/workspace. - Each cache is restored into its paths.
- The command or commands run.
- Each cache is saved, as its
policyandsaveallow, unless that key exists already. - Artifacts are collected and images pushed.
Restoring and saving run as the run's user, in the run's own container or VM. A miss, a failed restore or a failed save never fails the run.
Which runs see which caches
Caches belong to a project. Each entry also has the scope of the run that saved it: its branch, tag or pull request, or "runs without Git". A run restores from its own scope first, then from its repository's default branch, and saves only to its own scope. So a pull request never changes what the default branch's runs restore. A pull request from a fork reads the default branch's caches and never saves.
To warm the caches for everyone, run the default branch, for example with a push trigger on
main.
Quota and expiry
Each project has a cache quota, 10 GiB by default, and caches unused for 7 days expire. Your organization's plan sets both; the Quotas page shows them. Past the quota, the entries used longest ago are deleted. Caches aren't billed by size, but restoring and saving are part of the run's time.
See and delete them
The run page has a Caches section once the run has ended. Each cache shows its key, how the restore went (Hit, Partial for a restore key, Miss or Failed) and how the save went (Saved, Not saved or Failed), with size and time.
The project's Caches page shows the usage against the quota and lists the entries with key, scope, size and last use. You can search by key prefix and delete one entry, the matching ones or all of them. Deleting needs the "Delete caches" permission, which developers and up have.
forge cache list # the project's entries and usage
forge cache list --key go- # only keys starting with go-
forge cache delete ENTRY # one entry
forge cache delete --key go- # every entry whose key starts with go-
forge cache delete --all
Delete a cache when it holds something broken: the next run misses and saves a fresh one.
Recipes
These paths are where the official images keep the tool's cache when they run as root.
npm. Cache the download cache, not node_modules, which npm ci deletes first:
image: node:22
caches:
- key: npm-${hashFiles('**/package-lock.json')}
restore_keys: [npm-]
paths: [/root/.npm]
commands:
- run: npm ci --prefer-offline
Maven:
image: maven:3.9-eclipse-temurin-21
caches:
- key: maven-${hashFiles('**/pom.xml')}
restore_keys: [maven-]
paths: [/root/.m2/repository]
command: [mvn, --batch-mode, verify]
pip. Point it at a fixed path and put the architecture in the key, as it keeps built wheels:
image: python:3.13
env: { PIP_CACHE_DIR: /cache/pip }
caches:
- key: pip-${runner.arch}-${hashFiles('**/requirements*.txt')}
restore_keys: [pip-${runner.arch}-]
paths: [/cache/pip]
Local runs
With forge run --local, each cache path is a Docker volume on your machine. Keys aren't used:
the next local run finds what the last one left.