-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathvercelSyncEnvVars.ts
More file actions
106 lines (91 loc) · 3.14 KB
/
vercelSyncEnvVars.ts
File metadata and controls
106 lines (91 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { BuildExtension } from "@trigger.dev/core/v3/build";
import { syncEnvVars } from "../core.js";
type EnvVar = { name: string; value: string; isParentEnv?: boolean };
export function syncVercelEnvVars(options?: {
projectId?: string;
vercelAccessToken?: string;
vercelTeamId?: string;
branch?: string;
}): BuildExtension {
const sync = syncEnvVars(async (ctx) => {
const projectId =
options?.projectId ?? process.env.VERCEL_PROJECT_ID ?? ctx.env.VERCEL_PROJECT_ID;
const vercelAccessToken =
options?.vercelAccessToken ??
process.env.VERCEL_ACCESS_TOKEN ??
ctx.env.VERCEL_ACCESS_TOKEN ??
process.env.VERCEL_TOKEN;
const vercelTeamId =
options?.vercelTeamId ?? process.env.VERCEL_TEAM_ID ?? ctx.env.VERCEL_TEAM_ID;
const branch =
options?.branch ??
process.env.VERCEL_PREVIEW_BRANCH ??
ctx.env.VERCEL_PREVIEW_BRANCH ??
ctx.branch;
console.debug("syncVercelEnvVars()", {
projectId,
vercelTeamId,
branch,
});
if (!projectId) {
throw new Error(
"syncVercelEnvVars: you did not pass in a projectId or set the VERCEL_PROJECT_ID env var."
);
}
if (!vercelAccessToken) {
throw new Error(
"syncVercelEnvVars: you did not pass in a vercelAccessToken or set the VERCEL_ACCESS_TOKEN env var."
);
}
const environmentMap = {
prod: "production",
staging: "preview",
dev: "development",
preview: "preview",
} as const;
const vercelEnvironment = environmentMap[ctx.environment as keyof typeof environmentMap];
if (!vercelEnvironment) {
throw new Error(
`Invalid environment '${ctx.environment}'. Expected 'prod', 'staging', or 'dev'.`
);
}
const params = new URLSearchParams({ decrypt: "true" });
if (vercelTeamId) params.set("teamId", vercelTeamId);
params.set("target", vercelEnvironment);
const vercelApiUrl = `https://api.vercel.com/v8/projects/${projectId}/env?${params}`;
try {
const response = await fetch(vercelApiUrl, {
headers: {
Authorization: `Bearer ${vercelAccessToken}`,
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const isBranchable = ctx.environment === "preview";
const filteredEnvs: EnvVar[] = data.envs
.filter(
(env: { type: string; value: string; target: string[] }) =>
env.value && env.target.includes(vercelEnvironment)
)
.map((env: { key: string; value: string; gitBranch?: string }) => {
return {
name: env.key,
value: env.value,
isParentEnv: isBranchable && !env.gitBranch,
};
});
return filteredEnvs;
} catch (error) {
console.error("Error fetching or processing Vercel environment variables:", error);
throw error; // Re-throw the error to be handled by the caller
}
});
return {
name: "SyncVercelEnvVarsExtension",
async onBuildComplete(context, manifest) {
await sync.onBuildComplete?.(context, manifest);
},
};
}