Turborepo globalEnv versus per-task env for build-time public variables
0 reputation · 22 Sept 2021, 06:07 UTC
0 reputation · 22 Sept 2021, 06:07 UTC
Turborepo v2 defaults to strict environment variable mode, where only variables declared in globalEnv or a task's env array participate in the task hash. Framework-prefixed variables such as NEXT_PUBLIC_* or VITE_* are inlined at build time, so their values must vary the cache key when the target environment changes. The documentation shows both globalEnv and per-task env can declare these variables, but it does not prescribe which scope is appropriate for values that differ between preview, staging, and production deployments.
Placing a variable in globalEnv forces every task in the pipeline to re-execute when that variable changes, even tasks that do not consume it. Declaring the same variable in each task's env array limits invalidation to the tasks that actually read it, but duplicates configuration and risks drift. Neither approach clearly addresses whether a shared remote cache should store separate artifacts per deployment environment or rely on a single cache key for all builds of the same commit.
globalEnv or per-task env when their values differ across deployment targets?32275 reputation · 22 Sept 2021, 09:11 UTC
In Turborepo v2, only variables listed in globalEnv or a task’s env array participate in the task-specific cache hash. Framework-prefixed public variables such as NEXT_PUBLIC_* or VITE_* are inlined at build time, meaning their values must vary the cache key when the target environment changes to avoid serving the wrong environment's assets.
globalEnv: A variable declared here is included in every task’s cache key hash. A change to a global variable invalidates the entire pipeline, regardless of whether a specific task actually consumes that variable.env: A variable listed in a specific task’s env array affects only that task’s cache key. Other tasks remain cached unless their own declared variables or inputs change.Neither scoping approach automatically incorporates a deployment environment identifier (like STAGING or PROD) into the remote cache key. Framework-prefixed variables are inlined during the build process; while their specific values influence the hash, the remote cache treats the resulting artifact as a match for any environment that produces the same hash.
~/.turbocache/...) to confirm whether the variable appears in all task hashes or only declared tasks.turbo run --dry or observe cache misses to determine if the entire pipeline or only specific tasks are re-invoked.To finalize the recommendation, I need one missing diagnostic detail: what deployment environment identifier your pipeline currently uses (or plans to use) for differentiating preview, staging, and production builds.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.