6270abaab8
* Make the tooltip text color grey so it’s readable again the primary color * Setup and styling for the Guides section * Better Guides icon * Improved the prerequisites * WIP Nextjs guide and new folder structures * Copy updates * Useful next steps is now a snippet * Better icons for the next steps section * Renamed the “prerequisites” snippet * Node.js guide * Added a “Creating a project” guide * New snippet for prerequisites * New Remix guide * Added Remix to the side bar * Tweaked icons for creating a project page * Added the hello world step to the onboarding steps * Better “useful next steps” snippet card links * Moved prerequisites * Fixing links to images * Moved v2 migration page to guides * Removed dead link * Attempt fix for redirect * Fixed redirects * Getting started section includes link to roadmap * Removed icon from side menu * Deleted all v2 pages (excluding updating mint.json) * Removed v2 pages, redirects and versions from mint.json * Deleted v2 snippets * Deleted un-used pages * Set of more useful coming soon snippets * All snippets use the updated format * updated folder “v3/“ with “/pages” * Moved all main docs files to the route and updated the redirect * Fixed URLs in the mdx pages to the new route path * URL goes to the proper pricing page * Better delayed runs image * Attempt fix for self hosting page not redirecting
178 lines
5.7 KiB
Plaintext
178 lines
5.7 KiB
Plaintext
---
|
|
title: "Environment Variables"
|
|
description: "Any environment variables used in your tasks need to be added so the deployed code will run successfully."
|
|
---
|
|
|
|
An environment variable in Node.js is accessed in your code using `process.env.MY_ENV_VAR`.
|
|
|
|
We deploy your tasks and scale them up and down when they are triggered. So any environment variables you use in your tasks need to accessible to us so your code will run successfully.
|
|
|
|
## In the dashboard
|
|
|
|
### Setting environment variables
|
|
|
|
<Steps>
|
|
|
|
<Step title="Go to the Environment Variables page">
|
|
In the sidebar select the "Environment Variables" page, then press the "New environment variable"
|
|
button. 
|
|
</Step>
|
|
|
|
<Step title="Add your environment variables">
|
|
You can add values for your local dev environment, staging and prod. 
|
|
</Step>
|
|
|
|
</Steps>
|
|
|
|
<Note>
|
|
Specifying Dev values is optional. They will be overriden by values in your .env file when running
|
|
locally.
|
|
</Note>
|
|
|
|
### Editing environment variables
|
|
|
|
You can edit an environment variable's values. You cannot edit the key name, you must delete and create a new one.
|
|
|
|
<Steps>
|
|
|
|
<Step title="Press the action button on a variable">
|
|

|
|
</Step>
|
|
|
|
<Step title="Press edit">
|
|

|
|
</Step>
|
|
|
|
</Steps>
|
|
|
|
### Deleting environment variables
|
|
|
|
<Warn>
|
|
Environment variables are fetched and injected before a runs begins. So if you delete one you can
|
|
cause runs to fail that are expecting variables to be set.
|
|
</Warn>
|
|
|
|
<Steps>
|
|
|
|
<Step title="Press the action button on a variable">
|
|

|
|
</Step>
|
|
|
|
<Step title="Press delete">
|
|
This will immediately delete the variable. 
|
|
</Step>
|
|
|
|
</Steps>
|
|
|
|
## In your code
|
|
|
|
You can use our SDK to get and manipulate environment variables. You can also easily sync environment variables from another service into Trigger.dev.
|
|
|
|
### Directly manipulating environment variables
|
|
|
|
We have a complete set of SDK functions (and REST API) you can use to directly manipulate environment variables.
|
|
|
|
| Function | Description |
|
|
| ----------------------------------------------------- | ----------------------------------------------------------- |
|
|
| [envvars.list()](/management/envvars/list) | List all environment variables |
|
|
| [envvars.upload()](/management/envvars/import) | Upload multiple env vars. You can override existing values. |
|
|
| [envvars.create()](/management/envvars/create) | Create a new environment variable |
|
|
| [envvars.retrieve()](/management/envvars/retrieve) | Retrieve an environment variable |
|
|
| [envvars.update()](/management/envvars/update) | Update a single environment variable |
|
|
| [envvars.del()](/management/envvars/delete) | Delete a single environment variable |
|
|
|
|
### Sync env vars from another service
|
|
|
|
You could use the SDK functions above but it's much easier to use our `resolveEnvVars` function in your `trigger.config` file.
|
|
|
|
In this example we're using env vars from Infisical.
|
|
|
|
```ts /trigger.config.ts
|
|
import type { TriggerConfig, ResolveEnvironmentVariablesFunction } from "@trigger.dev/sdk/v3";
|
|
|
|
//This runs when you run the deploy command or the dev command
|
|
export const resolveEnvVars: ResolveEnvironmentVariablesFunction = async ({
|
|
//the project ref (starting with "proj_")
|
|
projectRef,
|
|
//any existing env vars from a .env file or Trigger.dev
|
|
env,
|
|
//"dev", "staging", or "prod"
|
|
environment,
|
|
}) => {
|
|
//the existing environment variables from Trigger.dev (or your local .env file)
|
|
if (env.INFISICAL_CLIENT_ID === undefined || env.INFISICAL_CLIENT_SECRET === undefined) {
|
|
//returning undefined won't modify the existing env vars
|
|
return;
|
|
}
|
|
|
|
const client = new InfisicalClient({
|
|
clientId: env.INFISICAL_CLIENT_ID,
|
|
clientSecret: env.INFISICAL_CLIENT_SECRET,
|
|
});
|
|
|
|
const secrets = await client.listSecrets({
|
|
environment,
|
|
projectId: env.INFISICAL_PROJECT_ID!,
|
|
});
|
|
|
|
return {
|
|
variables: secrets.map((secret) => ({
|
|
name: secret.secretKey,
|
|
value: secret.secretValue,
|
|
})),
|
|
// this defaults to true
|
|
// override: true,
|
|
};
|
|
};
|
|
|
|
//the rest of your config file
|
|
export const config: TriggerConfig = {
|
|
project: "proj_1234567890",
|
|
//etc
|
|
};
|
|
```
|
|
|
|
#### Local development
|
|
|
|
When you [develop locally](/cli-dev) `resolveEnvVars()` will inject the env vars from Infisical into your local `process.env`.
|
|
|
|
#### Deploy
|
|
|
|
When you run the [CLI deploy command](/cli-deploy) directly or using [GitHub Actions](/github-actions) it will sync the environment variables from Infisical to Trigger.dev. This means they'll appear on the Environment Variables page so you can confirm that it's worked.
|
|
|
|
This means that you need to redeploy your Trigger.dev tasks if you change the environment variables in Infisical.
|
|
|
|
### The variables return type
|
|
|
|
You can return `variables` as an object with string keys and values, or an array of names + values.
|
|
|
|
```ts
|
|
return {
|
|
variables: {
|
|
MY_ENV_VAR: "my value",
|
|
MY_OTHER_ENV_VAR: "my other value",
|
|
},
|
|
};
|
|
```
|
|
|
|
or
|
|
|
|
```ts
|
|
return {
|
|
variables: [
|
|
{
|
|
name: "MY_ENV_VAR",
|
|
value: "my value",
|
|
},
|
|
{
|
|
name: "MY_OTHER_ENV_VAR",
|
|
value: "my other value",
|
|
},
|
|
],
|
|
};
|
|
```
|
|
|
|
This should mean that for most secret services you won't need to convert the data into a different format.
|