Skip to content

Commit

Permalink
Vercel sync env vars extension (#1425)
Browse files Browse the repository at this point in the history
* Added vercelSyncEnvVars extension

* Updated extension and added try catch

* VercelEnvironment fix

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Added changeset

* Moved vercelSynvEnvVars to code and updated core.ts

* Fixed import

* removed type

---------

Co-authored-by: Matt Aitken <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent 90514a7 commit 982906c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-ligers-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/build": patch
---

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.
1 change: 1 addition & 0 deletions packages/build/src/extensions/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./core/additionalPackages.js";
export * from "./core/syncEnvVars.js";
export * from "./core/aptGet.js";
export * from "./core/ffmpeg.js";
export * from "./core/vercelSyncEnvVars.js";
83 changes: 83 additions & 0 deletions packages/build/src/extensions/core/vercelSyncEnvVars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { BuildExtension } from "@trigger.dev/core/v3/build";
import { syncEnvVars } from "../core.js";

export function syncVercelEnvVars(
options?: { projectId?: string; vercelAccessToken?: 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;

if (!projectId) {
throw new Error(
"vercelSyncEnvVars: you did not pass in a projectId or set the VERCEL_PROJECT_ID env var.",
);
}

if (!vercelAccessToken) {
throw new Error(
"vercelSyncEnvVars: you did not pass in a vercelAccessToken or set the VERCEL_ACCESS_TOKEN env var.",
);
}

const environmentMap = {
prod: "production",
staging: "preview",
dev: "development",
} 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 vercelApiUrl =
`https://api.vercel.com/v8/projects/${projectId}/env?decrypt=true`;

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 filteredEnvs = data.envs
.filter(
(env: { type: string; value: string; target: string[] }) =>
env.value &&
env.target.includes(vercelEnvironment),
)
.map((env: { key: string; value: string }) => ({
name: env.key,
value: env.value,
}));

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);
},
};
}

0 comments on commit 982906c

Please sign in to comment.