Per-PR database URL for Deploy Preview functions: inject it before the build, or trigger previews from CI?

Use case

We use a serverless Postgres provider with database branching (Neon :slight_smile:). For each pull request, CI creates a short-lived database branch: a masked (anonymized) copy of production. We want each Deploy Preview’s functions to use that PR’s own database branch, so open PRs never share or overwrite each other’s data.

The CI job knows the new branch’s connection string about 20 seconds after the PR opens. Netlify starts the Deploy Preview build at about the same time.

What we know

  1. CI can set our DATABASE_URL to a branch-specific value through the API, and it stays isolated per PR. But functions take env values as they are when the deploy is created. On a PR’s first push, the build usually starts before CI has set the value, so the preview gets the default database and needs a rebuild.
  2. POST /sites/{site_id}/builds?review_id= returns 422 review_id is not supported for GitHub repositories. So CI can’t trigger, or re-trigger, a Deploy Preview it seems.
  3. Without review_id, the same endpoint (and build hooks) make production or branch deploys, not Deploy Previews.
  4. createSiteDeploy supports deploy-specific environment variables, which is exactly the isolation we want. But it needs a pre-built site, so we’d have to move preview builds out of Netlify.
  5. Variables in netlify.toml, or set during the build, aren’t available to functions.

Questions

  1. Can Deploy Previews of a GitHub repository be triggered or re-triggered from CI: through the API, a build hook, or something else?
  2. Can a Netlify-built Deploy Preview get deploy-specific env values for its functions, for example from a build plugin, or set right before the build starts?
  3. Can a Deploy Preview build wait for an external signal before it starts? For example a GitHub check, or a CI step that writes the env value.
  4. Is there a recommended pattern for “one database branch per Deploy Preview” with an external Postgres provider, similar to what Netlify DB does?
  5. Related: we build several sites from one monorepo, and most PRs only need one or two of them. Is there a way to skip a site’s Deploy Preview without it appearing in the deploy list, beyond the
    ignore command?

We were originally wanting to use Netlify database to have this kind of sexy/native integration, but we needed a few things that you didn’t support yet: notably masking data in database branches, and sharing database across several netlify projects.

Thanks in advance for any advice you can provide about this! It seems like we’re SO close !