Skip to content

Commit 47a64c0

Browse files
authored
docs: improve env var documentation with .env upload + ctx environment details (#2680)
1 parent fc351cb commit 47a64c0

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/deploy-environment-variables.mdx

Lines changed: 78 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.

0 commit comments

Comments
 (0)