Skip to content

Commit 08ac5d7

Browse files
authored
fix: wrong env values could be used for non-dev context in netlify deploy --build --context (#5538)
* fix: fix the wrong env context for `netlify deploy --build --context` * fix: only send env vars of the functions scope to functions
1 parent c4b15f6 commit 08ac5d7

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/commands/deploy/deploy.mjs

+23-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
} from '../../utils/command-helpers.mjs'
3232
import { DEFAULT_DEPLOY_TIMEOUT } from '../../utils/deploy/constants.mjs'
3333
import { deploySite } from '../../utils/deploy/deploy-site.mjs'
34+
import { getEnvelopeEnv } from '../../utils/env/index.mjs'
3435
import { getFunctionsManifestPath, getInternalFunctionsDir } from '../../utils/functions/index.mjs'
3536
import openBrowser from '../../utils/open-browser.mjs'
3637
import { link } from '../link/index.mjs'
@@ -567,6 +568,16 @@ const deploy = async (options, command) => {
567568
return triggerDeploy({ api, options, siteData, siteId })
568569
}
569570

571+
const isUsingEnvelope = siteData && siteData.use_envelope
572+
// if a context is passed besides dev, we need to pull env vars from that specific context
573+
if (isUsingEnvelope && options.context && options.context !== 'dev') {
574+
command.netlify.cachedConfig.env = await getEnvelopeEnv({
575+
api,
576+
context: options.context,
577+
env: command.netlify.cachedConfig.env,
578+
siteInfo: siteData,
579+
})
580+
}
570581
const { configMutations = [], newConfig } = await handleBuild({
571582
cachedConfig: command.netlify.cachedConfig,
572583
options,
@@ -595,7 +606,18 @@ const deploy = async (options, command) => {
595606
deployFolder,
596607
functionsFolder,
597608
})
598-
const siteEnv = get(siteData, 'build_settings.env')
609+
610+
const siteEnv = isUsingEnvelope
611+
? await getEnvelopeEnv({
612+
api,
613+
context: options.context,
614+
env: command.netlify.cachedConfig.env,
615+
raw: true,
616+
scope: 'functions',
617+
siteInfo: siteData,
618+
})
619+
: get(siteData, 'build_settings.env')
620+
599621
const functionsConfig = normalizeFunctionsConfig({
600622
functionsConfig: config.functions,
601623
projectRoot: site.root,

src/utils/env/index.mjs

+14-1
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,12 @@ export const formatEnvelopeData = ({ context = 'dev', envelopeItems = [], scope
131131
* @param {string} context - The deploy context or branch of the environment variable
132132
* @param {object} env - The dictionary of environment variables
133133
* @param {string} key - If present, fetch a single key (case-sensitive)
134+
* @param {boolean} raw - Return a dictionary of raw key/value pairs for only the account and site sources
134135
* @param {enum<any,builds,functions,runtime,post_processing>} scope - The scope of the environment variables
135136
* @param {object} siteInfo - The site object
136137
* @returns {object} An object of environment variables keys and their metadata
137138
*/
138-
export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scope = 'any', siteInfo }) => {
139+
export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', raw = false, scope = 'any', siteInfo }) => {
139140
const { account_slug: accountId, id: siteId } = siteInfo
140141

141142
const [accountEnvelopeItems, siteEnvelopeItems] = await Promise.all([
@@ -145,6 +146,18 @@ export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scop
145146

146147
const accountEnv = formatEnvelopeData({ context, envelopeItems: accountEnvelopeItems, scope, source: 'account' })
147148
const siteEnv = formatEnvelopeData({ context, envelopeItems: siteEnvelopeItems, scope, source: 'ui' })
149+
150+
if (raw) {
151+
const entries = Object.entries({ ...accountEnv, ...siteEnv })
152+
return entries.reduce(
153+
(obj, [envVarKey, metadata]) => ({
154+
...obj,
155+
[envVarKey]: metadata.value,
156+
}),
157+
{},
158+
)
159+
}
160+
148161
const generalEnv = filterEnvBySource(env, 'general')
149162
const internalEnv = filterEnvBySource(env, 'internal')
150163
const addonsEnv = filterEnvBySource(env, 'addons')

0 commit comments

Comments
 (0)