talira

actions/cache

Cache dependencies and build outputs in GitHub Actions

open sourceMITfree

actions/cache is GitHub's official action for GitHub Actions that saves files (downloaded dependencies, build outputs) under a key you build from contexts and hashFiles, and restores them in later workflow runs. Its actions/cache/restore and actions/cache/save sub-actions split the two halves, so a job can restore without saving or save at any step, and lookup-only: true only checks whether an entry exists (setting cache-hit) without downloading it. With the main action, a key that missed is saved when the job completes successfully, and a saved entry is immutable. Entries are scoped by branch: a run can restore caches from its own branch, the default branch and a pull request's base branch, and a pull request run's caches only from re-runs of that pull request. GitHub keeps up to 10 GB of caches per repository by default (the limit can be raised, and storage beyond 10 GB is billed) and removes entries not accessed in 7 days, evicting the least recently used first when the limit is reached.

Problems it solves

  • every CI run downloads all my npm packages again from scratch
  • keep build outputs between GitHub workflow runs so jobs don't rebuild everything each push
  • skip the install step when the lockfile hasn't changed since the last run
  • check whether something was already cached for this key without downloading the whole thing
  • remember that a job already passed for this exact hash of its input files and skip it next time
  • restore a cache in one job and save it from a different step or job
  • save the cache even when a later step in the job fails
  • let feature branch CI reuse the dependencies cache that main already built
  • fail the job straight away when the cache it expects isn't there
  • share one cache between Windows and Linux runners
  • my CI cache goes stale and I need a new one whenever the lockfile changes

Features

  • save and restore paths under a key built from contexts and hashFiles
  • restore-keys prefix matching to fall back to the nearest older cache
  • actions/cache/restore and actions/cache/save sub-actions to restore or save on their own
  • lookup-only checks whether an entry exists without downloading it
  • cache-hit output to skip steps on an exact key match
  • fail-on-cache-miss stops the job when no entry matches
  • enableCrossOsArchive lets Windows runners share caches with other operating systems
  • a save denied by a read-only cache token warns instead of failing the job
  • falls back to the default branch's caches when the current branch has none

Limitations

  • Can't update a cache entry once it's saved: a key is immutable, so new contents need a new key1
  • Doesn't keep entries that go unused: GitHub removes any not accessed in 7 days, and evicts the least recently used once a repository passes its limit (10 GB by default)23
  • Doesn't share caches with sibling or child branches or other tags: a run restores only its own branch's, the default branch's and a pull request's base branch's2
  • Doesn't let other branches restore a pull request run's caches: they're scoped to its merge ref, so only re-runs of that pull request can2

Alternatives

The same job

These do what actions/cache does.

  1. Swatinem/rust-cache A GitHub Action that implements smart caching for rust/cargo projects with sensible defaults.
  2. WarpBuild High-performance GitHub Actions runners: 2x faster, 50% cheaper

Compare actions/cache with the top 3