Skip to content

Commit 251a1ee

Browse files
authored
Merge branch 'main' into docs/vercel-sync-env-vars
2 parents 45f3230 + 9f6887b commit 251a1ee

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

.changeset/good-ligers-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/build": patch
3+
---
4+
5+
Added a Vercel sync env vars extension. Given a Vercel projectId and access token it will sync Vercel env vars when deploying Trigger.dev tasks.

docs/guides/examples/scrape-hacker-news.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ description: "This example demonstrates how to scrape the top 3 articles from Ha
77
import LocalDevelopment from "/snippets/local-development-extensions.mdx";
88
import ScrapingWarning from "/snippets/web-scraping-warning.mdx";
99

10+
<div className="w-full h-full aspect-video">
11+
<iframe width="100%" height="100%" src="https://www.youtube.com/embed/6azvzrZITKY?si=muKtsBiS9TJGGKWg" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen/>
12+
</div>
13+
1014
## Overview
1115

1216
In this example we'll be using a number of different tools and features to:

packages/build/src/extensions/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./core/additionalPackages.js";
33
export * from "./core/syncEnvVars.js";
44
export * from "./core/aptGet.js";
55
export * from "./core/ffmpeg.js";
6+
export * from "./core/vercelSyncEnvVars.js";
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { BuildExtension } from "@trigger.dev/core/v3/build";
2+
import { syncEnvVars } from "../core.js";
3+
4+
export function syncVercelEnvVars(
5+
options?: { projectId?: string; vercelAccessToken?: string },
6+
): BuildExtension {
7+
const sync = syncEnvVars(async (ctx) => {
8+
const projectId = options?.projectId ?? process.env.VERCEL_PROJECT_ID ??
9+
ctx.env.VERCEL_PROJECT_ID;
10+
const vercelAccessToken = options?.vercelAccessToken ??
11+
process.env.VERCEL_ACCESS_TOKEN ??
12+
ctx.env.VERCEL_ACCESS_TOKEN;
13+
14+
if (!projectId) {
15+
throw new Error(
16+
"vercelSyncEnvVars: you did not pass in a projectId or set the VERCEL_PROJECT_ID env var.",
17+
);
18+
}
19+
20+
if (!vercelAccessToken) {
21+
throw new Error(
22+
"vercelSyncEnvVars: you did not pass in a vercelAccessToken or set the VERCEL_ACCESS_TOKEN env var.",
23+
);
24+
}
25+
26+
const environmentMap = {
27+
prod: "production",
28+
staging: "preview",
29+
dev: "development",
30+
} as const;
31+
32+
const vercelEnvironment =
33+
environmentMap[ctx.environment as keyof typeof environmentMap];
34+
35+
if (!vercelEnvironment) {
36+
throw new Error(
37+
`Invalid environment '${ctx.environment}'. Expected 'prod', 'staging', or 'dev'.`,
38+
);
39+
}
40+
const vercelApiUrl =
41+
`https://api.vercel.com/v8/projects/${projectId}/env?decrypt=true`;
42+
43+
try {
44+
const response = await fetch(vercelApiUrl, {
45+
headers: {
46+
Authorization: `Bearer ${vercelAccessToken}`,
47+
},
48+
});
49+
50+
if (!response.ok) {
51+
throw new Error(`HTTP error! status: ${response.status}`);
52+
}
53+
54+
const data = await response.json();
55+
56+
const filteredEnvs = data.envs
57+
.filter(
58+
(env: { type: string; value: string; target: string[] }) =>
59+
env.value &&
60+
env.target.includes(vercelEnvironment),
61+
)
62+
.map((env: { key: string; value: string }) => ({
63+
name: env.key,
64+
value: env.value,
65+
}));
66+
67+
return filteredEnvs;
68+
} catch (error) {
69+
console.error(
70+
"Error fetching or processing Vercel environment variables:",
71+
error,
72+
);
73+
throw error; // Re-throw the error to be handled by the caller
74+
}
75+
});
76+
77+
return {
78+
name: "SyncVercelEnvVarsExtension",
79+
async onBuildComplete(context, manifest) {
80+
await sync.onBuildComplete?.(context, manifest);
81+
},
82+
};
83+
}

0 commit comments

Comments
 (0)