Use this when speeding up GitHub Actions with caching.
Caching · GitHub Actions
Caching in GitHub Actions stores dependencies like npm, pip or Maven files between runs with actions/cache or setup-* cache options, keyed by lock-file hashes to speed up builds.
1. Caching speeds dependencies.
2. Key on lock files.
3. Caches are scoped by branch; PRs can read base caches.
4. Don't cache secrets.
5. setup-* actions have built-in caching.
key → restore → build → save
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The primary procedure is: Run the job once with a guaranteed cache miss and once with the intended key; inspect cache-hit metadata and verify the build/test result is identical apart from performance. Exercise failure-oriented cases such as stale key design, cache poisoning from low-trust triggers, secrets cached accidentally, partial restore masking dependency drift and cache-hit assumptions breaking clean builds so the test covers failure cases, not only the happy path.
Step 2 Common mistakes Narrow the fault
- Caching secrets or build outputs that differ per environment.
Step 3 Where it fails Narrow the fault
Failure analysis for GitHub Actions → Caching should follow the path through the components: workflow context → key calculation → cache lookup/permission → archive restore → package/tool validation → optional save. Tests should include edge cases such as stale key design, cache poisoning from low-trust triggers, secrets cached accidentally, partial restore masking dependency drift and cache-hit assumptions breaking clean builds. Recovery should disable or version-bump the suspect key, reproduce on a clean cache miss, repair key/trust scoping, then compare dependency lock data and build/test output.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Key caches on hashes of lock files.
- setup-node, setup-python and others have built-in caching.
- A stale or poisoned cache can break builds.
- actions/cache key with hashFiles(lock files) — correct key
gh cache list— caches- Cache scope per branch — poisoning risk
ReferenceWhy it matters1 section · depth on demand
In depth
Caching in GitHub Actions uses actions/cache (or built-in caching in setup actions) to save directories between runs, keyed by a hash of dependency files.
- uses: actions/setup-python@v5
with: { python-version: '3.12', cache: pip }
- uses: actions/cache@v4
with:
path: ~/.ansible/collections
key: collections-${{ hashFiles('collections/requirements.yml') }}
| Limit | Detail |
|---|---|
| Scope | Caches from the default branch are readable by other branches |
| Size | Repository cache quota; old entries evicted |
| Restore keys | Partial matches when the exact key misses |
Caches are not artifacts: use actions/upload-artifact for files other jobs or people need.
ReferenceImportant terms & boundaries1 section · depth on demand
Mechanics
Caching in GitHub Actions is owned at runtime by the GitHub Actions cache service together with the workflow job and cache action/setup action that computes lookup keys. The durable intent is workflow YAML defining cache paths, exact key, restore keys and the trust context allowed to read or write caches. Reliable automation must carry stable identity across the path: repository, branch/ref scope, workflow run/job, cache key/version and cache entry ID. The consumer-visible target is dependency or intermediate files restored into the runner workspace, never secrets or authoritative build artifacts. The key invariants are keys encode dependency inputs, restored caches are treated as untrusted acceleration data, cache miss remains a valid path and cache contents are reproducible.
ReferenceConfiguration & state1 section · depth on demand
Lifecycle
Store and review workflow YAML defining cache paths, exact key, restore keys and the trust context allowed to read or write caches; pin or at least record implementation/model versions and keep environment-specific identity outside reusable logic where possible. The stable identity contract is repository, branch/ref scope, workflow run/job, cache key/version and cache entry ID.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in GitHub Actions > Caching. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.github.com/en/actions/concepts/workflows-and-actions/dependency-caching · confidence 94%Quick lookup tables
GitHub Actions → Caching · operational reference
Context-specific ownership, verification, output and failure-boundary lookup for GitHub Actions → Caching.
| Layer | GitHub Actions → Caching operational reference |
|---|---|
| Runtime/ownership | GitHub Actions dependency caching · the GitHub Actions cache service together with the workflow job and cache action/setup action that computes lookup keys |
| Persistent intent | workflow YAML defining cache paths, exact key, restore keys and the trust context allowed to read or write caches |
| Verification | Run the job once with a guaranteed cache miss and once with the intended key; inspect cache-hit metadata and verify the build/test result is identical apart from performance. |
| Expected interpretation | The correct cache scope/key is selected, untrusted contexts cannot gain inappropriate write capability, misses rebuild successfully, and restored data does not alter the verified build result. |
| Recovery boundary | disable or version-bump the suspect key, reproduce on a clean cache miss, repair key/trust scoping, then compare dependency lock data and build/test output |
Deep reference
Healthy vs suspicious evidence
Output interpretation
At minimum retain run/job ID, ref, key, restore key matched, cache-hit boolean, cache entry/version, restored paths, save result, build checksum/test result and timing. The useful output makes ambiguity visible, links to the target identity from repository, branch/ref scope, workflow run/job, cache key/version and cache entry ID, and provides enough context to safely decide whether a retry is permitted.