Skip to content

Commit 70ef88c

Browse files
fix: load internal environment variables in Envelope (#5429)
* fix: inject `NETLIFY_DEV` env var in the process * refactor: add internal env vars to envelope method * chore: update site names in tests
1 parent b0a6c85 commit 70ef88c

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed

src/utils/env/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scop
146146
const accountEnv = formatEnvelopeData({ context, envelopeItems: accountEnvelopeItems, scope, source: 'account' })
147147
const siteEnv = formatEnvelopeData({ context, envelopeItems: siteEnvelopeItems, scope, source: 'ui' })
148148
const generalEnv = filterEnvBySource(env, 'general')
149+
const internalEnv = filterEnvBySource(env, 'internal')
149150
const addonsEnv = filterEnvBySource(env, 'addons')
150151
const configFileEnv = filterEnvBySource(env, 'configFile')
151152

@@ -159,6 +160,7 @@ export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scop
159160
...(includeConfigEnvVars ? addonsEnv : {}),
160161
...siteEnv,
161162
...(includeConfigEnvVars ? configFileEnv : {}),
163+
...internalEnv,
162164
}
163165
}
164166

tests/integration/100.command.dev.test.cjs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path')
55
const avaTest = require('ava')
66
const { isCI } = require('ci-info')
77
const dotProp = require('dot-prop')
8+
const getAvailablePort = require('get-port')
89
const jwt = require('jsonwebtoken')
910
const { Response } = require('node-fetch')
1011

@@ -964,3 +965,122 @@ test('should have only allowed environment variables set', async (t) => {
964965
})
965966
})
966967
})
968+
969+
test('should inject the `NETLIFY_DEV` environment variable in the process (legacy environment variables)', async (t) => {
970+
const externalServerPort = await getAvailablePort()
971+
const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs')
972+
const command = `node ${externalServerPath} ${externalServerPort}`
973+
974+
await withSiteBuilder('site-with-legacy-env-vars', async (builder) => {
975+
const publicDir = 'public'
976+
977+
await builder
978+
.withNetlifyToml({
979+
config: {
980+
build: {
981+
publish: publicDir,
982+
},
983+
dev: {
984+
command,
985+
publish: publicDir,
986+
targetPort: externalServerPort,
987+
framework: '#custom',
988+
},
989+
},
990+
})
991+
.buildAsync()
992+
993+
await withDevServer({ cwd: builder.directory }, async ({ port }) => {
994+
const response = await got(`http://localhost:${port}/`).json()
995+
996+
t.is(response.env.NETLIFY_DEV, 'true')
997+
})
998+
})
999+
})
1000+
1001+
test('should inject the `NETLIFY_DEV` environment variable in the process', async (t) => {
1002+
const siteInfo = {
1003+
account_slug: 'test-account',
1004+
build_settings: {
1005+
env: {},
1006+
},
1007+
id: 'site_id',
1008+
name: 'site-name',
1009+
use_envelope: true,
1010+
}
1011+
const existingVar = {
1012+
key: 'EXISTING_VAR',
1013+
scopes: ['builds', 'functions'],
1014+
values: [
1015+
{
1016+
id: '1234',
1017+
context: 'production',
1018+
value: 'envelope-prod-value',
1019+
},
1020+
{
1021+
id: '2345',
1022+
context: 'dev',
1023+
value: 'envelope-dev-value',
1024+
},
1025+
],
1026+
}
1027+
const routes = [
1028+
{ path: 'sites/site_id', response: siteInfo },
1029+
{ path: 'sites/site_id/service-instances', response: [] },
1030+
{
1031+
path: 'accounts',
1032+
response: [{ slug: siteInfo.account_slug }],
1033+
},
1034+
{
1035+
path: 'accounts/test-account/env/EXISTING_VAR',
1036+
response: existingVar,
1037+
},
1038+
{
1039+
path: 'accounts/test-account/env',
1040+
response: [existingVar],
1041+
},
1042+
]
1043+
1044+
const externalServerPort = await getAvailablePort()
1045+
const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs')
1046+
const command = `node ${externalServerPath} ${externalServerPort}`
1047+
1048+
await withSiteBuilder('site-with-env-vars', async (builder) => {
1049+
const publicDir = 'public'
1050+
1051+
await builder
1052+
.withNetlifyToml({
1053+
config: {
1054+
build: {
1055+
publish: publicDir,
1056+
},
1057+
dev: {
1058+
command,
1059+
publish: publicDir,
1060+
targetPort: externalServerPort,
1061+
framework: '#custom',
1062+
},
1063+
},
1064+
})
1065+
.buildAsync()
1066+
1067+
await withMockApi(routes, async ({ apiUrl }) => {
1068+
await withDevServer(
1069+
{
1070+
cwd: builder.directory,
1071+
offline: false,
1072+
env: {
1073+
NETLIFY_API_URL: apiUrl,
1074+
NETLIFY_SITE_ID: 'site_id',
1075+
NETLIFY_AUTH_TOKEN: 'fake-token',
1076+
},
1077+
},
1078+
async ({ port }) => {
1079+
const response = await got(`http://localhost:${port}/`).json()
1080+
1081+
t.is(response.env.NETLIFY_DEV, 'true')
1082+
},
1083+
)
1084+
})
1085+
})
1086+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const process = require('process')
2+
3+
const { startExternalServer } = require('./external-server.cjs')
4+
5+
const port = Number.parseInt(process.argv[2])
6+
7+
if (Number.isNaN(port)) {
8+
throw new TypeError(`Invalid port`)
9+
}
10+
11+
console.log('Running external server on port', port, process.env.NETLIFY_DEV)
12+
13+
startExternalServer({ port })

tests/integration/utils/external-server.cjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
const { env } = require('process')
2+
13
const express = require('express')
24

3-
const startExternalServer = () => {
5+
const startExternalServer = ({ port } = {}) => {
46
const app = express()
57
app.use(express.urlencoded({ extended: true }))
68
app.all('*', function onRequest(req, res) {
7-
res.json({ url: req.url, body: req.body, method: req.method, headers: req.headers })
9+
res.json({ url: req.url, body: req.body, method: req.method, headers: req.headers, env })
810
})
911

10-
return app.listen()
12+
return app.listen({ port })
1113
}
1214

1315
module.exports = {

0 commit comments

Comments
 (0)