Skip to content

fix(previews): inject project env at runtime, with PREVIEW_ overrides - #259

Merged
bobdivx merged 2 commits into
mainfrom
fix/preview-runtime-env
Oct 21, 2026
Merged

bobdivx merged 2 commits into
mainfrom
fix/preview-runtime-env

Conversation

@bobdivx

@bobdivx bobdivx commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

Problem

PR preview containers started without the project's env vars (e.g. Vigie preview-6: Variables runtime manquantes: TURSO_* ; OIDC_*), while prod got them.

Root cause

load_preview_env_file selected ... WHERE preview = true on project_env_vars, but that column does not exist (the opt-in column is preview_allowed). The SQL error was swallowed by unwrap_or_default(), so the generated .env only held DEVFORGE_PREVIEW*. The log line [env] cloned project env → .env was printed anyway.

Fix

Previews now build their runtime env from the project vars, like prod (fork PRs are still never deployed). The env is still passed at runtime via --env-file, the same way as prod, and nothing is added to the image.

  • PREVIEW_<KEY> overrides <KEY> in previews and is not passed under its own name. For example, PREVIEW_TURSO_DATABASE_URL / PREVIEW_TURSO_AUTH_TOKEN point a preview at a separate database. No databases are created automatically.
  • *_REDIRECT_URI / *_REDIRECT_URL values, and any URL value whose host is the project's production host (AUTH_URL, NEXTAUTH_URL, …), are rewritten to the preview host, keeping path and query (unless overridden by PREVIEW_<KEY>). Other hosts (for example the OIDC issuer) are kept as they are.
  • DEVFORGE_PREVIEW, DEVFORGE_PREVIEW_PR and DEVFORGE_PREVIEW_URL are always set by DevForge and cannot be overridden.
  • A warning is logged when a preview shares the prod database (TURSO_DATABASE_URL, DATABASE_URL, … with no PREVIEW_ override).

Tests

Pure build_preview_env / rewrite_url_host / preview_shared_db_keys tests in pr_previews_tests.rs.

PR previews read their env with a query on a non-existent column
(project_env_vars.preview); the error was swallowed, so the container
only got DEVFORGE_PREVIEW* and apps failed on missing runtime vars.

Previews now get the project env like prod, then:
- PREVIEW_<KEY> overrides <KEY> (e.g. a separate preview database),
- *_REDIRECT_URI / *_REDIRECT_URL point to the preview host (same path),
- DEVFORGE_PREVIEW* markers cannot be spoofed.
A warning is logged when a preview shares the prod database.
@bobdivx
bobdivx merged commit 174f980 into main Oct 21, 2026
4 checks passed
@bobdivx
bobdivx deleted the fix/preview-runtime-env branch October 21, 2026 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant