Our Nx monorepo had no shared cache. Every laptop kept its own, and CI passed one between runs through the GitHub Actions cache, which is scoped to a branch and a job. So a pull request, a laptop and a deploy each rebuilt what main had already built.
Before building anything I measured it. A container mirroring our post-merge job filled a throwaway cache, then a laptop with no local cache ran the usual checks against it:
| Caller | Without the cache | With it |
|---|---|---|
| Lint (44 projects) | 6 min 22 s | 0.4 s |
| Typecheck | 3 min 40 s | 13.7 s |
| Generate the database client | 4.2 s | 0.03 s |
Nx makes the server part easy
Nx 23.1 can talk to any server that implements its remote cache spec: GET and PUT on /v1/cache/{hash}, pointed at with two environment variables. No Nx Cloud licence needed. Our server is about a thousand lines of TypeScript, running as a small container in front of one storage bucket, with a 30-day retention rule.
The hard part is trust
A shared build cache has one nasty failure mode, and it has a CVE (CVE-2025-36852, nicknamed CREEP): an untrusted run, like a pull request or a laptop, writes an entry under a hash, and a trusted run later restores it into a production build. That's why the simple bucket-backed cache plugins were deprecated. A careless custom server walks straight back into it.
So the design starts from who may write:
| Caller | Reads | Writes |
|---|---|---|
| Laptops | everything | nothing |
| Pull request checks | everything | nothing |
| Main: the build job | build entries | build entries |
| Main: the check jobs | everything | check entries |
| A test environment's release | its own area, then shared builds | only its own area |
| Production release | build entries | nothing |
A few rules make that hold:
- No long-lived write credential exists. Writers on
mainget a short-lived identity token, and only a push tomainfrom the right workflow can obtain one. A pull request, a schedule or a manual run can't, whatever its workflow file says. A CI check fails if any other job even asks for a writer identity. - Entries are write-once. A write only succeeds if nothing exists under that hash yet, and the server aborts if the bytes it received don't match the declared length, so a partial upload is never served.
- Build entries and check entries are separate. Lint plugins and test code run in the check jobs but never in a release build, so those jobs can't supply anything a production build restores.
- Every entry records who wrote it, set when it's created and never rewritten.
Giving test environments their own area
Our test environments run on their own branches, which drift from main, so they got few cache hits and rebuilt the same packages on every deploy. They needed somewhere to write, without ever being able to write somewhere main or production reads.
GitHub Actions can hand a job a short-lived OIDC token that says which repository, workflow and environment it is. But those tokens expire within minutes, and Nx reads its access token once at startup, so a long build would start failing halfway through. So the job trades the GitHub token for a session first:
- Job startsGitHub issues an OIDC token for it
- Exchangethe server checks repository, workflow, branch and environment
- Sessiona signed token, valid for 90 minutes
- Buildreads and writes only that environment's area
Removing an environment from the server's allow-list ends its sessions immediately.
The cache must never fail a build
The cache only saves time, so it's never allowed to break anything:
- Before a job uses the cache, it makes one cheap request to check that the server accepts its token. If not, the job runs without it.
- Nx exits with an error when the cache fails mid-run, however good the build was. A small wrapper spots Nx's own cache error lines and reruns the command once without the cache.
- If the session exchange fails, the release simply builds cold.
Developers run one setup command that adds the server and their read-only token to their shell profile, and a --check flag to confirm it works.