Skip to content

Commit 577a4bf

Browse files
committed
docs: improve env var documentation with .env upload + ctx environment details
1 parent a70ab10 commit 577a4bf

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

docs/deploy-environment-variables.mdx

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,84 @@ We have a complete set of SDK functions (and REST API) you can use to directly m
8383
| [envvars.update()](/management/envvars/update) | Update a single environment variable |
8484
| [envvars.del()](/management/envvars/delete) | Delete a single environment variable |
8585

86+
#### Initial load from .env file
87+
88+
To initially load environment variables from a `.env` file into your Trigger.dev cloud environment, you can use `envvars.upload()`. This is useful for one-time bulk imports when setting up a new project or environment.
89+
90+
```ts
91+
import { envvars } from "@trigger.dev/sdk";
92+
import { readFileSync } from "fs";
93+
import { parse } from "dotenv";
94+
95+
// Read and parse your .env file
96+
const envContent = readFileSync(".env.production", "utf-8");
97+
const parsed = parse(envContent);
98+
99+
// Upload to Trigger.dev (replace with your project ref and environment slug)
100+
await envvars.upload("proj_your_project_ref", "prod", {
101+
variables: parsed,
102+
override: false, // Set to true to override existing variables
103+
});
104+
```
105+
106+
When called inside a task, you can omit the project ref and environment slug as they'll be automatically inferred from the task context:
107+
108+
```ts
109+
import { envvars, task } from "@trigger.dev/sdk";
110+
import { readFileSync } from "fs";
111+
import { parse } from "dotenv";
112+
113+
export const setupEnvVars = task({
114+
id: "setup-env-vars",
115+
run: async () => {
116+
const envContent = readFileSync(".env.production", "utf-8");
117+
const parsed = parse(envContent);
118+
119+
// projectRef and environment slug are automatically inferred from ctx
120+
await envvars.upload({
121+
variables: parsed,
122+
override: false,
123+
});
124+
},
125+
});
126+
```
127+
128+
<Note>
129+
This is different from `syncEnvVars` which automatically syncs variables during every deploy. Use `envvars.upload()` for one-time initial loads, and `syncEnvVars` for ongoing synchronization.
130+
</Note>
131+
132+
#### Getting the current environment
133+
134+
When using `envvars.retrieve()` inside a task, you can access the current environment information from the task context (`ctx`). The `envvars.retrieve()` function doesn't return the environment, but you can get it from `ctx.environment`:
135+
136+
```ts
137+
import { envvars, task } from "@trigger.dev/sdk";
138+
139+
export const myTask = task({
140+
id: "my-task",
141+
run: async (payload, { ctx }) => {
142+
// Get the current environment information
143+
const currentEnv = ctx.environment.slug; // e.g., "dev", "prod", "staging"
144+
const envType = ctx.environment.type; // e.g., "DEVELOPMENT", "PRODUCTION", "STAGING", "PREVIEW"
145+
146+
// Retrieve an environment variable
147+
// When called inside a task, projectRef and slug are automatically inferred
148+
const apiKey = await envvars.retrieve("API_KEY");
149+
150+
console.log(`Retrieved API_KEY from environment: ${currentEnv} (${envType})`);
151+
console.log(`Value: ${apiKey.value}`);
152+
},
153+
});
154+
```
155+
156+
The context object provides:
157+
- `ctx.environment.slug` - The environment slug (e.g., "dev", "prod")
158+
- `ctx.environment.type` - The environment type ("DEVELOPMENT", "PRODUCTION", "STAGING", or "PREVIEW")
159+
- `ctx.environment.id` - The environment ID
160+
- `ctx.project.ref` - The project reference
161+
162+
For more information about the context object, see the [Context documentation](/context).
163+
86164
### Sync env vars from another service
87165

88166
You could use the SDK functions above but it's much easier to use our `syncEnvVars` build extension in your `trigger.config` file.
@@ -234,3 +312,52 @@ You can now use the `client` object to make authenticated requests to Google API
234312
</Step>
235313

236314
</Steps>
315+
316+
## Using `.env.production` or dotenvx with Trigger.dev
317+
318+
Trigger.dev does not automatically load `.env.production` files or dotenvx files during deploys.
319+
To use these files in your Trigger.dev environment:
320+
321+
### Option 1 — Manually add your environment variables
322+
323+
1. Open your `.env.production` (or `.env`) file
324+
2. Copy the full contents
325+
3. Go to your Trigger.dev project → **Environment Variables**
326+
4. Click **Add variables**
327+
5. Paste the contents directly into the editor
328+
329+
Trigger.dev will automatically parse and create each key/value pair.
330+
331+
This is the simplest way to bring dotenvx or `.env.production` variables into your Trigger.dev environment.
332+
333+
### Option 2 — Sync variables automatically using `syncEnvVars`
334+
335+
If you'd prefer an automated flow, you can use the `syncEnvVars` build extension to programmatically load and return your variables:
336+
337+
```ts
338+
import { defineConfig } from "@trigger.dev/sdk";
339+
import { syncEnvVars } from "@trigger.dev/build/extensions/core";
340+
import dotenvx from "@dotenvx/dotenvx";
341+
import { readFileSync } from "fs";
342+
343+
export default defineConfig({
344+
project: "<project id>",
345+
build: {
346+
extensions: [
347+
syncEnvVars(async () => {
348+
const envContent = readFileSync(".env.production", "utf-8");
349+
const parsed = dotenvx.parse(envContent);
350+
return parsed ?? {};
351+
}),
352+
],
353+
},
354+
});
355+
```
356+
357+
This will read your .env.production file using dotenvx and sync the variables to Trigger.dev during every deploy.
358+
359+
**Summary**
360+
361+
- Trigger.dev does not automatically detect .env.production or dotenvx files
362+
- You can paste them manually into the dashboard
363+
- Or sync them automatically using a build extension

0 commit comments

Comments
 (0)