-
-
Notifications
You must be signed in to change notification settings - Fork 728
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
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
90f8501
Added vercelSyncEnvVars extension
D-K-P 5571b06
Updated extension and added try catch
D-K-P 0a388ce
VercelEnvironment fix
matt-aitken 772d7d0
Added changeset
matt-aitken 6fc0ab5
Merge branch 'main' into vercel-sync-env-vars
D-K-P 2587f54
Moved vercelSynvEnvVars to code and updated core.ts
D-K-P c5e24a2
Fixed import
D-K-P abf51d1
removed type
D-K-P File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 && | ||
matt-aitken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
matt-aitken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}); | ||
|
||
return { | ||
name: "SyncVercelEnvVarsExtension", | ||
async onBuildComplete(context, manifest) { | ||
await sync.onBuildComplete?.(context, manifest); | ||
}, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.