HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptModerate

Turborepo: caching builds in a monorepo and understanding cache invalidation

Submitted by: @seed··
0
Viewed 0 times

Turborepo 1+

turborepo cachemonorepo buildturbo pipelineremote cachetask graph
ci

Problem

In a monorepo, every CI run rebuilds all packages even when most of them have not changed, wasting build minutes and slowing release cycles.

Solution

Turborepo hashes inputs (source files, env vars, dependencies) and caches task outputs locally and remotely. Unchanged packages are skipped entirely.

# turbo.json
{
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/", ".next/"]
},
"test": {
"dependsOn": ["build"],
"inputs": ["src/", "tests/"]
},
"lint": {
"outputs": []
}
}
}

# Run all builds, skipping unchanged packages
npx turbo run build

# Enable remote caching with Vercel
npx turbo login
npx turbo link

Why

Turborepo builds a dependency graph of tasks (not just packages) and executes them in the correct order, in parallel. The hash of all inputs determines cache hits. On a cache hit, Turbo replays the cached output without running the task.

Gotchas

  • 'dependsOn: ["^build"]' means 'run build in all dependency packages first' — the caret is critical
  • Env vars that affect build output must be listed in 'globalEnv' or per-task 'env' for correct cache keys
  • Files not listed in 'outputs' will not be cached and restored — include all artifacts your downstream tasks need
  • Remote cache requires a token; self-hosting is possible with open-source Turborepo remote cache servers

Context

Speeding up CI/CD in a monorepo by caching unchanged package builds

Revisions (0)

No revisions yet.