Til 2025-05-21

ShinaBR2ShinaBR2
1 min read
  • We need to tell Turborepo to pass some environment variables into the apps in the monorepo, otherwise, the environment variables won’t be available.

Environment variable in Turborepo and Vite

Problem: Handle environment variables correctly in the Turborepo structure in Github actions.

First, we have this workflow file. Important notice here is the CODECOV_TOKEN should NOT be prefixed with VITE_

- name: Build
  run: pnpm build --filter=main
  env:
    VITE_API_KEY: ${{ secrets.VITE_API_KEY }}
    CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

And in the apps/til/vite.config.ts for example

const codecovToken = process.env.CODECOV_TOKEN;
// This is always undefined in CI servers

Root cause: turborepo does NOT automatically make custom environment variables available to the build command.

After spending hours trying and failing, I finally figured out the root cause: we need to let turborepo pass down the environment variable to the build step. We need to have this passThroughEnv options in the build step in turbo.json

{
  "$schema": "https://turborepo.org/schema.json",
  "tasks": {
    "build": {
      "outputs": ["dist/**"],
      "dependsOn": ["^build"],
      "passThroughEnv": ["CODECOV_TOKEN"]
    },
  }
}
0
Subscribe to my newsletter

Read articles from ShinaBR2 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ShinaBR2
ShinaBR2

Enthusiasm for building products with less code but more reliable.