-
-
Notifications
You must be signed in to change notification settings - Fork 4
fix(netlify): add runtime env fallback generation for server env reads #433
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
+88
−2
Merged
Changes from all commits
Commits
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,3 @@ | ||
| import 'server-only'; | ||
|
|
||
| export const RUNTIME_ENV: Readonly<Record<string, string>> = {}; |
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
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,56 @@ | ||
| import { readFileSync, writeFileSync } from 'node:fs'; | ||
| import { resolve } from 'node:path'; | ||
|
|
||
| const root = process.cwd(); | ||
| const examplePath = resolve(root, '.env.example'); | ||
| const outputPath = resolve(root, 'lib/env/runtime-env.generated.ts'); | ||
| const appEnv = (process.env.APP_ENV ?? '').trim().toLowerCase(); | ||
| const isDevelop = appEnv === 'develop'; | ||
|
|
||
| const keyRegex = /^([A-Z][A-Z0-9_]*)=/; | ||
|
|
||
| const keys = Array.from( | ||
| new Set( | ||
| readFileSync(examplePath, 'utf8') | ||
| .split(/\r?\n/) | ||
| .map(line => line.trim()) | ||
| .filter(line => line && !line.startsWith('#')) | ||
| .map(line => { | ||
| const match = line.match(keyRegex); | ||
| return match ? match[1] : null; | ||
| }) | ||
| .filter(Boolean) | ||
| ) | ||
| ); | ||
|
|
||
| const entries = []; | ||
|
|
||
| for (const key of keys) { | ||
| const value = process.env[key]; | ||
| if (typeof value !== 'string' || value.length === 0) continue; | ||
| entries.push([key, value]); | ||
| } | ||
|
|
||
| if (!isDevelop) { | ||
| const fileContent = `import 'server-only'; | ||
|
|
||
| export const RUNTIME_ENV: Readonly<Record<string, string>> = {}; | ||
| `; | ||
| writeFileSync(outputPath, fileContent, 'utf8'); | ||
| console.log(`[env] skipped runtime env generation (APP_ENV=${appEnv || '<empty>'})`); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const objectBody = entries | ||
| .map(([key, value]) => ` ${JSON.stringify(key)}: ${JSON.stringify(value)},`) | ||
| .join('\n'); | ||
|
|
||
| const fileContent = `import 'server-only'; | ||
|
|
||
| export const RUNTIME_ENV: Readonly<Record<string, string>> = { | ||
| ${objectBody} | ||
| }; | ||
| `; | ||
|
|
||
| writeFileSync(outputPath, fileContent, 'utf8'); | ||
| console.log(`[env] generated runtime-env.generated.ts with ${entries.length} keys`); | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 229
🏁 Script executed:
cat -n frontend/scripts/generate-env-runtime.mjs | head -50Repository: DevLoversTeam/devlovers.net
Length of output: 1551
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 158
🏁 Script executed:
rg "runtime-env\.generated" --type ts --type js --type jsonRepository: DevLoversTeam/devlovers.net
Length of output: 156
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 219
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 116
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1104
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 1363
🏁 Script executed:
rg "generate-env-runtime" --type ts --type js --type mjs --type jsonRepository: DevLoversTeam/devlovers.net
Length of output: 98
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 3587
🏁 Script executed:
rg "generate-env-runtime" --type ts --type jsRepository: DevLoversTeam/devlovers.net
Length of output: 53
🏁 Script executed:
git log --oneline frontend/lib/env/runtime-env.generated.ts | head -5Repository: DevLoversTeam/devlovers.net
Length of output: 149
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 50383
🏁 Script executed:
Repository: DevLoversTeam/devlovers.net
Length of output: 2862
Remove
lib/env/runtime-env.generated.tsfrom git tracking and add it to.gitignore.This file is generated by
generate-env-runtime.mjsduring Netlify builds and contains actual environment variable values. It's currently tracked in git without .gitignore protection, creating a clear path for developers to accidentally commit secrets if they run the generation script locally.The file is already part of the Netlify build pipeline (
netlify.tomlcalls the script beforenpm run build), so removing it from tracking won't break the build—Netlify will regenerate it with its own environment variables.🤖 Prompt for AI Agents