Skip to content

Commit d661d71

Browse files
committed
Added preview branch support to syncVercelEnvVars()
1 parent 3afa516 commit d661d71

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

packages/build/src/extensions/core/vercelSyncEnvVars.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,49 @@
11
import { BuildExtension } from "@trigger.dev/core/v3/build";
22
import { syncEnvVars } from "../core.js";
33

4-
export function syncVercelEnvVars(
5-
options?: {
6-
projectId?: string;
7-
vercelAccessToken?: string;
8-
vercelTeamId?: string;
9-
},
10-
): BuildExtension {
4+
export function syncVercelEnvVars(options?: {
5+
projectId?: string;
6+
vercelAccessToken?: string;
7+
vercelTeamId?: string;
8+
}): BuildExtension {
119
const sync = syncEnvVars(async (ctx) => {
12-
const projectId = options?.projectId ?? process.env.VERCEL_PROJECT_ID ??
13-
ctx.env.VERCEL_PROJECT_ID;
14-
const vercelAccessToken = options?.vercelAccessToken ??
15-
process.env.VERCEL_ACCESS_TOKEN ??
16-
ctx.env.VERCEL_ACCESS_TOKEN;
17-
const vercelTeamId = options?.vercelTeamId ?? process.env.VERCEL_TEAM_ID ??
18-
ctx.env.VERCEL_TEAM_ID;
10+
const projectId =
11+
options?.projectId ?? process.env.VERCEL_PROJECT_ID ?? ctx.env.VERCEL_PROJECT_ID;
12+
const vercelAccessToken =
13+
options?.vercelAccessToken ?? process.env.VERCEL_ACCESS_TOKEN ?? ctx.env.VERCEL_ACCESS_TOKEN;
14+
const vercelTeamId =
15+
options?.vercelTeamId ?? process.env.VERCEL_TEAM_ID ?? ctx.env.VERCEL_TEAM_ID;
1916

2017
if (!projectId) {
2118
throw new Error(
22-
"syncVercelEnvVars: you did not pass in a projectId or set the VERCEL_PROJECT_ID env var.",
19+
"syncVercelEnvVars: you did not pass in a projectId or set the VERCEL_PROJECT_ID env var."
2320
);
2421
}
2522

2623
if (!vercelAccessToken) {
2724
throw new Error(
28-
"syncVercelEnvVars: you did not pass in a vercelAccessToken or set the VERCEL_ACCESS_TOKEN env var.",
25+
"syncVercelEnvVars: you did not pass in a vercelAccessToken or set the VERCEL_ACCESS_TOKEN env var."
2926
);
3027
}
3128

3229
const environmentMap = {
3330
prod: "production",
3431
staging: "preview",
3532
dev: "development",
33+
preview: "preview",
3634
} as const;
3735

38-
const vercelEnvironment =
39-
environmentMap[ctx.environment as keyof typeof environmentMap];
36+
const vercelEnvironment = environmentMap[ctx.environment as keyof typeof environmentMap];
4037

4138
if (!vercelEnvironment) {
4239
throw new Error(
43-
`Invalid environment '${ctx.environment}'. Expected 'prod', 'staging', or 'dev'.`,
40+
`Invalid environment '${ctx.environment}'. Expected 'prod', 'staging', or 'dev'.`
4441
);
4542
}
4643
const params = new URLSearchParams({ decrypt: "true" });
4744
if (vercelTeamId) params.set("teamId", vercelTeamId);
48-
const vercelApiUrl =
49-
`https://api.vercel.com/v8/projects/${projectId}/env?${params}`;
45+
if (ctx.branch) params.set("gitBranch", ctx.branch);
46+
const vercelApiUrl = `https://api.vercel.com/v8/projects/${projectId}/env?${params}`;
5047

5148
try {
5249
const response = await fetch(vercelApiUrl, {
@@ -64,8 +61,7 @@ export function syncVercelEnvVars(
6461
const filteredEnvs = data.envs
6562
.filter(
6663
(env: { type: string; value: string; target: string[] }) =>
67-
env.value &&
68-
env.target.includes(vercelEnvironment),
64+
env.value && env.target.includes(vercelEnvironment)
6965
)
7066
.map((env: { key: string; value: string }) => ({
7167
name: env.key,
@@ -74,10 +70,7 @@ export function syncVercelEnvVars(
7470

7571
return filteredEnvs;
7672
} catch (error) {
77-
console.error(
78-
"Error fetching or processing Vercel environment variables:",
79-
error,
80-
);
73+
console.error("Error fetching or processing Vercel environment variables:", error);
8174
throw error; // Re-throw the error to be handled by the caller
8275
}
8376
});

0 commit comments

Comments
 (0)