Caching & Artifacts
GitHub Actions has two ways to keep files after a job ends, and they are for different things:
| Cache | Artifact | |
|---|---|---|
| Purpose | speed: reuse downloaded dependencies or build outputs between runs | results: pass files between jobs of one run, and let people download them |
| Correctness | optional: a miss only makes the job slower | required: the next job or the release depends on it |
| Action | actions/cache@v6 (or cache: in setup-* actions) |
actions/upload-artifact@v7, actions/download-artifact@v8 |
| Lifetime | evicted when unused for 7 days; 10 GB per repository by default | retention 90 days by default (configurable, retention-days) |
| Mutability | immutable: a key, once saved, is never overwritten | an artifact name is unique per run (overwrite: true replaces it) |
The rule that follows: never let a release depend on a cache. Caches are best-effort and shared between runs; artifacts are what a release job consumes.
Built-in caching in setup actions
The easiest cache is the one the language's setup action manages. It caches the package manager's download cache (not node_modules), keyed on the lockfile:
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm # or yarn, pnpm; key = OS + hash of the lockfile
- run: npm ciactions/setup-python (pip, pipenv, Poetry), actions/setup-java (Maven, Gradle), actions/setup-go (go.sum) and actions/setup-dotnet (NuGet) offer the same. Since setup-node v6, npm caching turns on automatically when package.json declares npm in packageManager (or devEngines.packageManager); for privileged release workflows the action's documentation recommends package-manager-cache: false, so a poisoned cache can't influence what gets published.
actions/cache directly
For anything else (a ccache directory, Gradle's build cache, downloaded toolchains), use actions/cache:
- uses: actions/cache@v6
id: ccache
with:
path: ~/.cache/ccache
key: ccache-${{ runner.os }}-${{ matrix.compiler }}-${{ hashFiles('**/CMakeLists.txt', 'conanfile.py') }}
restore-keys: |
ccache-${{ runner.os }}-${{ matrix.compiler }}-
- run: cmake --build build
- if: steps.ccache.outputs.cache-hit != 'true'
run: echo "cache was rebuilt or partially restored"How the lookup works (the CI job graph model implements it):
- an exact match of
key→ restored,cache-hitistrue, and nothing is saved at the end (keys are immutable); - otherwise the
restore-keysprefixes, in order, picking the most recently created cache for a prefix → restored,cache-hitis nottrue, and a new cache is saved underkeywhen the job succeeds; - otherwise nothing; the job saves a fresh cache under
key.
actions/cache/restore@v6 and actions/cache/save@v6 split the two halves, for example to save a cache even when tests fail, or to restore read-only in pull requests.
Which branches can see which caches
Caches are scoped to the branch (ref) that created them, so a pull request from an untrusted fork can't plant a cache that main later restores. A run can restore caches from its own branch and the default branch; a pull-request run can also restore from the base branch, and caches it creates are only visible to re-runs of that pull request. Sibling branches can't see each other's caches, and a tag can't restore another tag's cache. This model applies those rules:
caches = [ # (created by ref, key)
("refs/heads/main", "npm-Linux-aaa"),
("refs/heads/release/1.5", "npm-Linux-bbb"),
("refs/heads/feature-x", "npm-Linux-ccc"),
("refs/pull/42/merge", "npm-Linux-ddd"),
("refs/tags/v1.5.0", "npm-Linux-eee"),
]
DEFAULT = "refs/heads/main"
def visible(run_ref, base_ref=None):
allowed = {run_ref, DEFAULT}
if base_ref:
allowed.add(base_ref)
return [key for ref, key in caches if ref in allowed]
runs = [
("push to main", "refs/heads/main", None),
("push to release/1.5", "refs/heads/release/1.5", None),
("push to feature-y", "refs/heads/feature-y", None),
("PR #42 into release/1.5", "refs/pull/42/merge", "refs/heads/release/1.5"),
("PR #43 into main", "refs/pull/43/merge", "refs/heads/main"),
("tag v1.5.1", "refs/tags/v1.5.1", None),
]
for name, ref, base in runs:
print(f"{name:<25} can restore {visible(ref, base)}")push to main can restore ['npm-Linux-aaa']
push to release/1.5 can restore ['npm-Linux-aaa', 'npm-Linux-bbb']
push to feature-y can restore ['npm-Linux-aaa']
PR #42 into release/1.5 can restore ['npm-Linux-aaa', 'npm-Linux-bbb', 'npm-Linux-ddd']
PR #43 into main can restore ['npm-Linux-aaa']
tag v1.5.1 can restore ['npm-Linux-aaa']
feature-y can't use feature-x's cache, PR #43 can't use PR #42's, and the tag build only sees main's. That's why a cache warmed on the default branch (a scheduled job on main is a common trick) helps every other branch.
Artifacts between jobs
A release matrix builds on several runners; a later job collects the results. Upload with a unique name per matrix job, download with a pattern and merge-multiple:
jobs:
build:
strategy:
matrix:
include:
- { target: linux-x86_64, runner: ubuntu-24.04 }
- { target: linux-aarch64, runner: ubuntu-24.04-arm }
- { target: windows-x86_64, runner: windows-2025 }
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v7
- run: ./scripts/package.sh "${{ matrix.target }}" # writes dist/tad-<version>-<target>.*
shell: bash
- uses: actions/upload-artifact@v7
with:
name: dist-${{ matrix.target }}
path: dist/*
if-no-files-found: error
retention-days: 7
checksums:
needs: build
runs-on: ubuntu-24.04
steps:
- uses: actions/download-artifact@v8
with:
pattern: dist-*
merge-multiple: true # all files into one folder instead of one folder per artifact
path: dist
- run: cd dist && sha256sum * > SHA256SUMS && cat SHA256SUMS
- uses: actions/upload-artifact@v7
with:
name: release-files
path: dist/*if-no-files-found: error turns a packaging script that silently produced nothing into a failed job, instead of a release with a missing platform. Artifact names must be unique within a run, which is why each matrix job includes its target in the name.
Test yourself
Where should a release job get its binaries from?
- actions/cache
- Artifacts uploaded by the build jobs of the same run
- The runner's home directory
- The previous release
A cache was restored from a restore-keys prefix. What is cache-hit?
- true
- Not true (it is only true for an exact key match)
- partial
- The restored key
Can a run on branch feature-y restore a cache created on feature-x?
- Yes, always
- No: only its own branch's and the default branch's caches (plus the base branch for PRs)
- Only with restore-keys
- Only on self-hosted runners