Índice do artigofaltam 4 min de leitura
Caching is one of the biggest performance levers in CI/CD. It is also one of the biggest sources of intermittent bugs.
The goal of this post is practical: to teach you how to design cache keys that speed up the pipeline without leaving you with "haunted" builds.
This post is part of the series:
CI/CD series map
- A guide to Git CI/CD for small teams: performance, caching and runners
- GitLab CI: runner tags and job routing
- CI/CD caching: how to design keys and avoid stale caches (GitHub and GitLab)
- Docker layer caching in CI: BuildKit, buildx, cache-from and cache-to
- Dedicated CI/CD runner: when it pays off and how to plan it in a small team
- macOS runners in CI: Intel vs Apple Silicon, costs and pitfalls
Cache vs artifacts: start by separating what is what
Rule of thumb:
- Cache = reuse rebuildable directories (dependencies, downloads).
- Artifacts = carry build results between jobs (dist, binary, report).
When you use the cache to carry build output, you increase the risk of:
- a job consuming an "old" output
- inconsistent results per branch/architecture
1) How to design cache keys (what goes in and what stays out)
A good cache key usually has:
- Operating system
- Architecture (ARM64/x64)
- Lockfile hash
- Runtime version (when needed)
A bad cache key usually has:
- Commit SHA (invalidates on every commit)
- timestamp
- branch name without normalization (explodes the number of caches)
1.1 Example (GitHub Actions)
- name: Cache npm
uses: actions/cache@v6
with:
path: ~/.npm
key: ${{ runner.os }}-${{ runner.arch }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-${{ runner.arch }}-npm-Why this works:
- The lockfile hash only changes when a dependency changes.
- The prefix in
restore-keyslets you reuse a "close" cache when the exact one does not exist. runner.archprevents mixing ARM64 and x64.
About the action version: in November 2026, the actions/cache README uses @v6 in its examples (GitHub's documentation still shows @v4). Since v5, the action runs on Node.js 24 and requires runner version 2.327.1 or later. If you use a self-hosted runner, update the runner before bumping the version.
1.2 Example (GitLab CI/CD)
In GitLab, the equivalent of hashFiles is cache:key:files: the key becomes the SHA of the content of the listed files. With cache:key:prefix you combine that SHA with a string that keeps the caches apart. GitLab's documentation uses $CI_JOB_NAME as the prefix, and that is what the example below takes advantage of: the job name carries the architecture.
.node-cache:
cache:
key:
files:
- package-lock.json
prefix: $CI_JOB_NAME
paths:
- .npm/
before_script:
- npm ci --cache .npm --prefer-offline
test-x64:
extends: .node-cache
tags: [linux-x64]
script:
- npm test
test-arm64:
extends: .node-cache
tags: [linux-arm64]
script:
- npm testThe tag names (linux-x64, linux-arm64) are examples: use the tags that route to your runners.
Why this works:
- The key only changes when
package-lock.jsonchanges, as in the GitHub example. It ends up as something liketest-arm64-<lockfile sha>. - Each job has its own cache, so ARM64 and x64 never mix. The cost is that two jobs on the same architecture do not share a cache; if that matters, replace the prefix with a fixed string per architecture (for example
node-arm64) defined in each job. - The cache lives in
.npm/inside the project. GitLab only caches paths inside the project directory, which is why we usenpm ci --cache .npminstead of the default~/.npm.
Two caveats that apply to GitLab:
CI_RUNNER_EXECUTABLE_ARCHlooks like the natural place for the architecture, but the documentation itself warns that it describes the GitLab Runner executable and may not match the executor environment (a container for another platform, for example). Also,cache:keydoes not accept the/character. Check the value in your runner's log before using it in the key.fallback_keysdoes not work like GitHub'srestore-keys. GitHub looks for caches whose key starts with the given prefix; GitLab tries the keys in the list by exact name, in order (up to five per cache). With a lockfile-based key there is no "similar" key to look for, so listing fallbacks only makes sense if some job saves a cache with a fixed name.
2) What to cache (Node, Python, Java, Ruby)
General rule: cache the "package manager cache", not necessarily the final folder.
- Node (npm):
~/.npmon GitHub; on GitLab,.npm/inside the project withnpm ci --cache .npm - Node (pnpm): the store (on Linux the current default is
~/.local/share/pnpm/store; on GitLab, pointstore-dirto a folder inside the project) - Python (pip):
~/.cache/pip - Java/Gradle:
~/.gradle/caches - Ruby/bundler:
vendor/bundle(with care)
What to avoid as a first step:
- caching
node_modulesindiscriminately (it can be huge and unstable) - caching build outputs (that is an artifact)
- letting a job from an untrusted source (a fork pipeline, for example) write to a cache that trusted jobs restore later. The pnpm documentation treats the store as a trusted cache, and GitLab, by default, already separates caches for protected and unprotected branches.
3) Invalidation: what changes the cache (and how to avoid a "useless cache")
Three classic causes of an "always miss" cache:
- The key changes on every commit (e.g. it includes the SHA).
- The lockfile is not included in the hash (the cache goes stale and can break things).
- You are caching a directory that changes on every run.
Checklist to fix it:
- the lockfile is part of the key (
hashFiles(...)or equivalent). - the architecture is part of the key.
- the cache paths actually exist (do not invent directories).
- the cache is restored before installing dependencies.
4) Caching in pipelines with ARM64 and x64
When you have ARM64 and x64:
- caches of native dependencies cannot be shared
- caches need to be duplicated (one per architecture)
Practical rule:
- never share a cache across architectures without putting
archin the key
Final checklist
- The cache key includes the lockfile hash.
- The cache key includes OS + architecture.
- Restore keys exist on GitHub; on GitLab, fallbacks only with a fixed-name key (and never for build output).
- The cache is for dependencies (artifacts are for outputs).
- Observable metric: time saved per job (before/after).





