Skip to content

Vercel sync env vars extension #1425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
83 changes: 83 additions & 0 deletions packages/build/src/extensions/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.type === "encrypted" && 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);
},
};
}