Skip to content
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

fix: load internal environment variables in Envelope #5429

Merged
merged 3 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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