From 8c8afc78c8adab359b87980660ac5230b06cf4dd Mon Sep 17 00:00:00 2001 From: Jason Barry Date: Wed, 8 Mar 2023 09:46:42 -0800 Subject: [PATCH 1/2] fix: fix the wrong env context for `netlify deploy --build --context` --- src/commands/deploy/deploy.mjs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/commands/deploy/deploy.mjs b/src/commands/deploy/deploy.mjs index e8371ecee64..5aa05175426 100644 --- a/src/commands/deploy/deploy.mjs +++ b/src/commands/deploy/deploy.mjs @@ -30,6 +30,7 @@ import { } from '../../utils/command-helpers.mjs' import { DEFAULT_DEPLOY_TIMEOUT } from '../../utils/deploy/constants.mjs' import { deploySite } from '../../utils/deploy/deploy-site.mjs' +import { getEnvelopeEnv } from '../../utils/env/index.mjs' import { getFunctionsManifestPath, getInternalFunctionsDir } from '../../utils/functions/index.mjs' import openBrowser from '../../utils/open-browser.mjs' import { link } from '../link/index.mjs' @@ -566,6 +567,16 @@ const deploy = async (options, command) => { return triggerDeploy({ api, options, siteData, siteId }) } + const isUsingEnvelope = siteData && siteData.use_envelope + // if a context is passed besides dev, we need to pull env vars from that specific context + if (isUsingEnvelope && options.context && options.context !== 'dev') { + command.netlify.cachedConfig.env = await getEnvelopeEnv({ + api, + context: options.context, + env: command.netlify.cachedConfig.env, + siteInfo: siteData, + }) + } const { newConfig, configMutations = [] } = await handleBuild({ cachedConfig: command.netlify.cachedConfig, options, From b558a0d706e602de5f701aa695d0d891db784e4a Mon Sep 17 00:00:00 2001 From: Jason Barry Date: Wed, 8 Mar 2023 12:06:16 -0800 Subject: [PATCH 2/2] fix: only send env vars of the functions scope to functions --- src/commands/deploy/deploy.mjs | 13 ++++++++++++- src/utils/env/index.mjs | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/commands/deploy/deploy.mjs b/src/commands/deploy/deploy.mjs index 5aa05175426..dfffe313284 100644 --- a/src/commands/deploy/deploy.mjs +++ b/src/commands/deploy/deploy.mjs @@ -605,7 +605,18 @@ const deploy = async (options, command) => { deployFolder, functionsFolder, }) - const siteEnv = get(siteData, 'build_settings.env') + + const siteEnv = isUsingEnvelope + ? await getEnvelopeEnv({ + api, + context: options.context, + env: command.netlify.cachedConfig.env, + raw: true, + scope: 'functions', + siteInfo: siteData, + }) + : get(siteData, 'build_settings.env') + const functionsConfig = normalizeFunctionsConfig({ functionsConfig: config.functions, projectRoot: site.root, diff --git a/src/utils/env/index.mjs b/src/utils/env/index.mjs index 3793e52615d..42bccc36900 100644 --- a/src/utils/env/index.mjs +++ b/src/utils/env/index.mjs @@ -131,11 +131,12 @@ export const formatEnvelopeData = ({ context = 'dev', envelopeItems = [], scope * @param {string} context - The deploy context or branch of the environment variable * @param {object} env - The dictionary of environment variables * @param {string} key - If present, fetch a single key (case-sensitive) + * @param {boolean} raw - Return a dictionary of raw key/value pairs for only the account and site sources * @param {enum} scope - The scope of the environment variables * @param {object} siteInfo - The site object * @returns {object} An object of environment variables keys and their metadata */ -export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scope = 'any', siteInfo }) => { +export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', raw = false, scope = 'any', siteInfo }) => { const { account_slug: accountId, id: siteId } = siteInfo const [accountEnvelopeItems, siteEnvelopeItems] = await Promise.all([ @@ -145,6 +146,18 @@ 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' }) + + if (raw) { + const entries = Object.entries({ ...accountEnv, ...siteEnv }) + return entries.reduce( + (obj, [envVarKey, metadata]) => ({ + ...obj, + [envVarKey]: metadata.value, + }), + {}, + ) + } + const generalEnv = filterEnvBySource(env, 'general') const addonsEnv = filterEnvBySource(env, 'addons') const configFileEnv = filterEnvBySource(env, 'configFile')