Skip to content

Commit 90f8501

Browse files
committed
Added vercelSyncEnvVars extension
1 parent 768036a commit 90f8501

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { BuildExtension } from "@trigger.dev/core/v3/build";
2+
import { syncEnvVars } from "./core.js";
3+
4+
export function vercelSyncEnvVars(): BuildExtension {
5+
const sync = syncEnvVars(async (ctx) => {
6+
const environmentMap = {
7+
prod: "production",
8+
staging: "preview",
9+
dev: "development",
10+
} as const;
11+
12+
const vercelEnvironment =
13+
environmentMap[ctx.environment as keyof typeof environmentMap];
14+
15+
const vercelApiUrl =
16+
`https://api.vercel.com/v8/projects/${process.env.VERCEL_PROJECT_ID}/env?decrypt=true`;
17+
18+
const response = await fetch(vercelApiUrl, {
19+
headers: {
20+
Authorization: `Bearer ${process.env.VERCEL_ACCESS_TOKEN}`,
21+
},
22+
});
23+
24+
if (!response.ok) {
25+
throw new Error(`HTTP error! status: ${response.status}`);
26+
}
27+
28+
const data = await response.json();
29+
30+
const filteredEnvs = data.envs
31+
.filter(
32+
(env: { type: string; value: string; target: string[] }) =>
33+
env.type === "encrypted" && env.value &&
34+
env.target.includes(vercelEnvironment),
35+
)
36+
.map((env: { key: string; value: string }) => ({
37+
name: env.key,
38+
value: env.value,
39+
}));
40+
41+
return filteredEnvs;
42+
});
43+
44+
return {
45+
name: "SyncVercelEnvVarsExtension",
46+
async onBuildComplete(context, manifest) {
47+
await sync.onBuildComplete?.(context, manifest);
48+
},
49+
};
50+
}

0 commit comments

Comments
 (0)