Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/config/config-file.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ These environment variables are only used during the build process and are not e

#### syncEnvVars

The `syncEnvVars` build extension replaces the deprecated `resolveEnvVars` export. Check out our [syncEnvVars documentation](/deploy-environment-variables#sync-env-vars-from-another-service) for more information.
The `syncEnvVars` build extension replaces the deprecated `resolveEnvVars` export. Check out our [syncEnvVars documentation](/deploy-environment-variables#sync-env-vars-from-another-service) for more information and example usage (including syncing environment variables from Infisical and Vercel).

#### audioWaveform

Expand Down
4 changes: 4 additions & 0 deletions docs/deploy-environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export default defineConfig({
});
```

#### Syncing environment variables from Vercel

To sync environment variables from your Vercel projects to Trigger.dev, check out our [syncing environment variables from Vercel example](/guides/examples/vercel-sync-env-vars).

#### 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](https://infisical.com) to Trigger.dev. This means they'll appear on the Environment Variables page so you can confirm that it's worked.
Expand Down
83 changes: 83 additions & 0 deletions docs/guides/examples/vercel-sync-env-vars.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
title: "Syncing environment variables from your Vercel projects"
sidebarTitle: "Syncing Vercel env vars"
description: "This example demonstrates how to sync environment variables from your Vercel project to Trigger.dev."
---

## Overview

This example shows how to automatically sync environment variables from your Vercel project to Trigger.dev.

## Build configuration

To sync environment variables from your Vercel project to Trigger.dev, you'll first need to add this build configuration to your `trigger.config.ts` file. This extension will then automatically run every time you deploy your project.

This code syncs encrypted environment variables, filtering them based on the current environment (production, preview, or development).

```ts trigger.config.ts
import { defineConfig } from "@trigger.dev/sdk/v3";
import { vercelSyncEnvVars } from "@trigger.dev/build/extensions/vercelSyncEnvVars";

export default defineConfig({
build: {
extensions:
extensions: [
syncVercelEnvVars(),
syncEnvVars(async (ctx) => {
const environmentMap = {
// Account for the different environment names used by Vercel
prod: "production",
staging: "preview",
dev: "development",
} as const;

const vercelEnvironment =
environmentMap[ctx.environment as keyof typeof environmentMap];

const vercelApiUrl =
`https://api.vercel.com/v8/projects/${process.env.VERCEL_PROJECT_ID}/env?decrypt=true`;

const response = await fetch(vercelApiUrl, {
headers: {
Authorization: `Bearer ${process.env.VERCEL_ACCESS_TOKEN}`,
},
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();

const filteredEnvs = data.envs
.filter(
(env: { type: string; value: string; target: string[] }) =>
env.type === "encrypted" &&
env.value &&
env.target.includes(vercelEnvironment),
)
.map((env: { key: string; value: string }) => ({
name: env.key,
value: env.value,
}));

return filteredEnvs;
}),
],,
},
});
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix syntax errors and inconsistencies in the code example.

There are several issues in the code that need to be addressed:

  1. Duplicate extensions: key
  2. Inconsistent function names: imports vercelSyncEnvVars but uses syncEnvVars
  3. Undefined syncVercelEnvVars() function
  4. Extra comma in the extensions array

Here's the corrected version:

 import { defineConfig } from "@trigger.dev/sdk/v3";
 import { vercelSyncEnvVars } from "@trigger.dev/build/extensions/vercelSyncEnvVars";

 export default defineConfig({
   build: {
-    extensions:
     extensions: [
-      syncVercelEnvVars(),
-      syncEnvVars(async (ctx) => {
+      vercelSyncEnvVars(async (ctx) => {
         const environmentMap = {
           // Account for the different environment names used by Vercel
           prod: "production",
           staging: "preview",
           dev: "development",
         } as const;

         const vercelEnvironment =
           environmentMap[ctx.environment as keyof typeof environmentMap];

         const vercelApiUrl =
           `https://api.vercel.com/v8/projects/${process.env.VERCEL_PROJECT_ID}/env?decrypt=true`;

         const response = await fetch(vercelApiUrl, {
           headers: {
             Authorization: `Bearer ${process.env.VERCEL_ACCESS_TOKEN}`,
           },
         });

         if (!response.ok) {
           throw new Error(`HTTP error! status: ${response.status}`);
         }

         const data = await response.json();

         const filteredEnvs = data.envs
           .filter(
             (env: { type: string; value: string; target: string[] }) =>
               env.type === "encrypted" &&
               env.value &&
               env.target.includes(vercelEnvironment),
           )
           .map((env: { key: string; value: string }) => ({
             name: env.key,
             value: env.value,
           }));

         return filteredEnvs;
-      }),
-    ],,
+      }),
+    ],
   },
 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
```ts trigger.config.ts
import { defineConfig } from "@trigger.dev/sdk/v3";
import { vercelSyncEnvVars } from "@trigger.dev/build/extensions/vercelSyncEnvVars";
export default defineConfig({
build: {
extensions:
extensions: [
syncVercelEnvVars(),
syncEnvVars(async (ctx) => {
const environmentMap = {
// Account for the different environment names used by Vercel
prod: "production",
staging: "preview",
dev: "development",
} as const;
const vercelEnvironment =
environmentMap[ctx.environment as keyof typeof environmentMap];
const vercelApiUrl =
`https://api.vercel.com/v8/projects/${process.env.VERCEL_PROJECT_ID}/env?decrypt=true`;
const response = await fetch(vercelApiUrl, {
headers: {
Authorization: `Bearer ${process.env.VERCEL_ACCESS_TOKEN}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const filteredEnvs = data.envs
.filter(
(env: { type: string; value: string; target: string[] }) =>
env.type === "encrypted" &&
env.value &&
env.target.includes(vercelEnvironment),
)
.map((env: { key: string; value: string }) => ({
name: env.key,
value: env.value,
}));
return filteredEnvs;
}),
],,
},
});
```
```ts trigger.config.ts
import { defineConfig } from "@trigger.dev/sdk/v3";
import { vercelSyncEnvVars } from "@trigger.dev/build/extensions/vercelSyncEnvVars";
export default defineConfig({
build: {
extensions: [
vercelSyncEnvVars(async (ctx) => {
const environmentMap = {
// Account for the different environment names used by Vercel
prod: "production",
staging: "preview",
dev: "development",
} as const;
const vercelEnvironment =
environmentMap[ctx.environment as keyof typeof environmentMap];
const vercelApiUrl =
`https://api.vercel.com/v8/projects/${process.env.VERCEL_PROJECT_ID}/env?decrypt=true`;
const response = await fetch(vercelApiUrl, {
headers: {
Authorization: `Bearer ${process.env.VERCEL_ACCESS_TOKEN}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const filteredEnvs = data.envs
.filter(
(env: { type: string; value: string; target: string[] }) =>
env.type === "encrypted" &&
env.value &&
env.target.includes(vercelEnvironment),
)
.map((env: { key: string; value: string }) => ({
name: env.key,
value: env.value,
}));
return filteredEnvs;
}),
],
},
});
```


<Note>
[Build extensions](/config/config-file#extensions) allow you to hook into the build system and
customize the build process or the resulting bundle and container image (in the case of
deploying). You can use pre-built extensions or create your own.
</Note>

## Running the sync operation

To run the sync operation, simply run the `deploy` command. You should see some output in the console indicating that the environment variables have been synced, and they should now be available in the Trigger.dev dashboard.

```bash
npx trigger.dev@latest deploy
```
3 changes: 2 additions & 1 deletion docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@
"guides/examples/supabase-storage-upload",
"guides/examples/react-pdf",
"guides/examples/resend-email-sequence",
"guides/examples/vercel-ai-sdk"
"guides/examples/vercel-ai-sdk",
"guides/examples/vercel-sync-env-vars"
]
},
{
Expand Down