Skip to content

Commit

Permalink
fix: load internal environment variables in Envelope (#5429)
Browse files Browse the repository at this point in the history
* fix: inject `NETLIFY_DEV` env var in the process

* refactor: add internal env vars to envelope method

* chore: update site names in tests
  • Loading branch information
eduardoboucas authored Jan 24, 2023
1 parent b0a6c85 commit 70ef88c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/utils/env/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scop
const accountEnv = formatEnvelopeData({ context, envelopeItems: accountEnvelopeItems, scope, source: 'account' })
const siteEnv = formatEnvelopeData({ context, envelopeItems: siteEnvelopeItems, scope, source: 'ui' })
const generalEnv = filterEnvBySource(env, 'general')
const internalEnv = filterEnvBySource(env, 'internal')
const addonsEnv = filterEnvBySource(env, 'addons')
const configFileEnv = filterEnvBySource(env, 'configFile')

Expand All @@ -159,6 +160,7 @@ export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scop
...(includeConfigEnvVars ? addonsEnv : {}),
...siteEnv,
...(includeConfigEnvVars ? configFileEnv : {}),
...internalEnv,
}
}

Expand Down
120 changes: 120 additions & 0 deletions tests/integration/100.command.dev.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path')
const avaTest = require('ava')
const { isCI } = require('ci-info')
const dotProp = require('dot-prop')
const getAvailablePort = require('get-port')
const jwt = require('jsonwebtoken')
const { Response } = require('node-fetch')

Expand Down Expand Up @@ -964,3 +965,122 @@ test('should have only allowed environment variables set', async (t) => {
})
})
})

test('should inject the `NETLIFY_DEV` environment variable in the process (legacy environment variables)', async (t) => {
const externalServerPort = await getAvailablePort()
const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs')
const command = `node ${externalServerPath} ${externalServerPort}`

await withSiteBuilder('site-with-legacy-env-vars', async (builder) => {
const publicDir = 'public'

await builder
.withNetlifyToml({
config: {
build: {
publish: publicDir,
},
dev: {
command,
publish: publicDir,
targetPort: externalServerPort,
framework: '#custom',
},
},
})
.buildAsync()

await withDevServer({ cwd: builder.directory }, async ({ port }) => {
const response = await got(`http://localhost:${port}/`).json()

t.is(response.env.NETLIFY_DEV, 'true')
})
})
})

test('should inject the `NETLIFY_DEV` environment variable in the process', async (t) => {
const siteInfo = {
account_slug: 'test-account',
build_settings: {
env: {},
},
id: 'site_id',
name: 'site-name',
use_envelope: true,
}
const existingVar = {
key: 'EXISTING_VAR',
scopes: ['builds', 'functions'],
values: [
{
id: '1234',
context: 'production',
value: 'envelope-prod-value',
},
{
id: '2345',
context: 'dev',
value: 'envelope-dev-value',
},
],
}
const routes = [
{ path: 'sites/site_id', response: siteInfo },
{ path: 'sites/site_id/service-instances', response: [] },
{
path: 'accounts',
response: [{ slug: siteInfo.account_slug }],
},
{
path: 'accounts/test-account/env/EXISTING_VAR',
response: existingVar,
},
{
path: 'accounts/test-account/env',
response: [existingVar],
},
]

const externalServerPort = await getAvailablePort()
const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs')
const command = `node ${externalServerPath} ${externalServerPort}`

await withSiteBuilder('site-with-env-vars', async (builder) => {
const publicDir = 'public'

await builder
.withNetlifyToml({
config: {
build: {
publish: publicDir,
},
dev: {
command,
publish: publicDir,
targetPort: externalServerPort,
framework: '#custom',
},
},
})
.buildAsync()

await withMockApi(routes, async ({ apiUrl }) => {
await withDevServer(
{
cwd: builder.directory,
offline: false,
env: {
NETLIFY_API_URL: apiUrl,
NETLIFY_SITE_ID: 'site_id',
NETLIFY_AUTH_TOKEN: 'fake-token',
},
},
async ({ port }) => {
const response = await got(`http://localhost:${port}/`).json()

t.is(response.env.NETLIFY_DEV, 'true')
},
)
})
})
})
13 changes: 13 additions & 0 deletions tests/integration/utils/external-server-cli.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const process = require('process')

const { startExternalServer } = require('./external-server.cjs')

const port = Number.parseInt(process.argv[2])

if (Number.isNaN(port)) {
throw new TypeError(`Invalid port`)
}

console.log('Running external server on port', port, process.env.NETLIFY_DEV)

startExternalServer({ port })
8 changes: 5 additions & 3 deletions tests/integration/utils/external-server.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { env } = require('process')

const express = require('express')

const startExternalServer = () => {
const startExternalServer = ({ port } = {}) => {
const app = express()
app.use(express.urlencoded({ extended: true }))
app.all('*', function onRequest(req, res) {
res.json({ url: req.url, body: req.body, method: req.method, headers: req.headers })
res.json({ url: req.url, body: req.body, method: req.method, headers: req.headers, env })
})

return app.listen()
return app.listen({ port })
}

module.exports = {
Expand Down

1 comment on commit 70ef88c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Package size: 265 MB

Please sign in to comment.