Skip to content

Commit 05b5396

Browse files
authored
feat(build): Add NeonDB branch resolution for Vercel preview environments (#2729)
Vercel's NeonDB integration renders database connection environment variables at runtime, which means Trigger.dev cannot directly sync these values during the build process. This change adds support for fetching branch-specific NeonDB connection strings via the Neon API. feat(build): Add syncNeonEnvVars extension and improve Vercel env var syncing Add a new `syncNeonEnvVars` build extension for syncing environment variables from Neon database projects to Trigger.dev. The extension automatically detects branches and builds appropriate PostgreSQL connection strings for non-production environments (staging, dev, preview). Features of `syncNeonEnvVars`: - Fetches branch-specific database credentials from Neon API - Generates all standard Postgres connection strings (DATABASE_URL, POSTGRES_URL, POSTGRES_PRISMA_URL, etc.) with both pooled and unpooled variants - Supports custom database name, role name, and env var prefix options - Skips automatically in Vercel environments (Neon's Vercel integration handles this) - Skips for production environments (designed for preview/staging/dev branches) Improvements to `syncVercelEnvVars`: - When running in a Vercel build environment (detected via VERCEL env var), values are now read from process.env instead of the Vercel API response - This ensures the build uses the actual runtime values Vercel provides - Removed embedded Neon-specific logic (now handled by separate extension) - Simplified and cleaned up the extension code Documentation updates for both extensions with usage examples and configuration options. Closes #2714 ## ✅ Checklist - [x] I have followed every step in the [contributing guide](https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md) - [x] The PR title follows the convention. - [x] I ran and tested the code works --- ## Testing Set up Vercel + Trigger.dev envs, used Vercel's chat-bot-ai template.
2 parents b01b874 + 702f3b4 commit 05b5396

File tree

6 files changed

+410
-2
lines changed

6 files changed

+410
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/build": patch
3+
---
4+
5+
syncVercelEnvVars to skip API and read env vars directly from env.process for Vercel build environments. New syncNeonEnvVars build extension for syncing environment variablesfrom Neon database projects to Trigger.dev. The extension automatically detects branches and builds appropriate PostgreSQL connection strings for non-production, non-dev environments (staging, preview).

docs/config/extensions/syncEnvVars.mdx

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ The `syncVercelEnvVars` build extension syncs environment variables from your Ve
8080
the project with the environment variables you want to sync.
8181
</Note>
8282

83+
<Note>
84+
When running the build from a Vercel build environment (e.g., during a Vercel deployment), the
85+
environment variable values will be read from `process.env` instead of fetching them from the
86+
Vercel API. This is determined by checking if the `VERCEL` environment variable is present. The
87+
API is still used to determine which environment variables are configured for your project, but
88+
the actual values come from the local environment. Reading values from `process.env` allows the
89+
extension to use values that Vercel integrations (such as the Neon integration) set per preview
90+
deployment in the "Provisioning Integrations" phase that happens just before the Vercel build
91+
starts.
92+
</Note>
93+
94+
<Note>
95+
If you have the Neon database Vercel integration installed and are running builds outside of the
96+
Vercel environment, we recommend using `syncNeonEnvVars` in addition to `syncVercelEnvVars` for your
97+
database environment variables. This ensures that the correct database connection strings are used for your
98+
selected environment and current branch, as `syncVercelEnvVars` may not accurately reflect
99+
branch-specific database credentials when run locally.
100+
</Note>
101+
83102
```ts
84103
import { defineConfig } from "@trigger.dev/sdk";
85104
import { syncVercelEnvVars } from "@trigger.dev/build/extensions/core";
@@ -114,3 +133,79 @@ export default defineConfig({
114133
},
115134
});
116135
```
136+
137+
### syncNeonEnvVars
138+
139+
The `syncNeonEnvVars` build extension syncs environment variables from your Neon database project to Trigger.dev. It automatically detects branches and builds the appropriate database connection strings for your environment.
140+
141+
<Note>
142+
You need to set the `NEON_ACCESS_TOKEN` and `NEON_PROJECT_ID` environment variables, or pass them
143+
as arguments to the `syncNeonEnvVars` build extension. You can generate a `NEON_ACCESS_TOKEN` in
144+
your Neon [dashboard](https://console.neon.tech/app/settings/api-keys).
145+
</Note>
146+
147+
<Note>
148+
When running the build from a Vercel environment (determined by checking if the `VERCEL`
149+
environment variable is present), this extension is skipped entirely. This is because Neon's
150+
Vercel integration already handles environment variable synchronization in Vercel environments.
151+
</Note>
152+
153+
<Note>
154+
If you have the Neon database Vercel integration installed and are running builds outside of the
155+
Vercel environment, we recommend using `syncNeonEnvVars` in addition to `syncVercelEnvVars` for your
156+
database environment variables. This ensures that the correct database connection strings are used for your
157+
selected environment and current branch, as `syncVercelEnvVars` may not accurately reflect
158+
branch-specific database credentials when run locally.
159+
</Note>
160+
161+
<Note>
162+
This extension is skipped for `prod` environments. It is designed to sync branch-specific
163+
database connections for preview/staging environments.
164+
</Note>
165+
166+
```ts
167+
import { defineConfig } from "@trigger.dev/sdk";
168+
import { syncNeonEnvVars } from "@trigger.dev/build/extensions/core";
169+
170+
export default defineConfig({
171+
project: "<project ref>",
172+
// Your other config settings...
173+
build: {
174+
// This will automatically use the NEON_ACCESS_TOKEN and NEON_PROJECT_ID environment variables
175+
extensions: [syncNeonEnvVars()],
176+
},
177+
});
178+
```
179+
180+
Or you can pass in the token and project ID as arguments:
181+
182+
```ts
183+
import { defineConfig } from "@trigger.dev/sdk";
184+
import { syncNeonEnvVars } from "@trigger.dev/build/extensions/core";
185+
186+
export default defineConfig({
187+
project: "<project ref>",
188+
// Your other config settings...
189+
build: {
190+
extensions: [
191+
syncNeonEnvVars({
192+
projectId: "your-neon-project-id",
193+
neonAccessToken: "your-neon-access-token",
194+
branch: "your-branch-name", // optional, defaults to ctx.branch
195+
databaseName: "your-database-name", // optional, defaults to the first database
196+
roleName: "your-role-name", // optional, defaults to the database owner
197+
envVarPrefix: "MY_PREFIX_", // optional, prefix for all synced env vars
198+
}),
199+
],
200+
},
201+
});
202+
```
203+
204+
The extension syncs the following environment variables (with optional prefix):
205+
206+
- `DATABASE_URL` - Pooled connection string
207+
- `DATABASE_URL_UNPOOLED` - Direct connection string
208+
- `POSTGRES_URL`, `POSTGRES_URL_NO_SSL`, `POSTGRES_URL_NON_POOLING`
209+
- `POSTGRES_PRISMA_URL` - Connection string optimized for Prisma
210+
- `POSTGRES_HOST`, `POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_DATABASE`
211+
- `PGHOST`, `PGHOST_UNPOOLED`, `PGUSER`, `PGPASSWORD`, `PGDATABASE`

docs/guides/examples/vercel-sync-env-vars.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ To sync environment variables, you just need to add our build extension to your
1919
the project with the environment variables you want to sync.
2020
</Note>
2121

22+
<Note>
23+
When running the build from a Vercel build environment (e.g., during a Vercel deployment), the
24+
environment variable values will be read from `process.env` instead of fetching them from the
25+
Vercel API. This is determined by checking if the `VERCEL` environment variable is present. The
26+
API is still used to determine which environment variables are configured for your project, but
27+
the actual values come from the local environment.
28+
</Note>
29+
2230
```ts trigger.config.ts
2331
import { defineConfig } from "@trigger.dev/sdk";
2432
import { syncVercelEnvVars } from "@trigger.dev/build/extensions/core";

packages/build/src/extensions/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from "./core/additionalPackages.js";
33
export * from "./core/syncEnvVars.js";
44
export * from "./core/aptGet.js";
55
export * from "./core/ffmpeg.js";
6+
export * from "./core/neonSyncEnvVars.js";
67
export * from "./core/vercelSyncEnvVars.js";

0 commit comments

Comments
 (0)