diff --git a/docs/commands/build.md b/docs/commands/build.md index 34a5af36b4f..2a56df235a0 100644 --- a/docs/commands/build.md +++ b/docs/commands/build.md @@ -15,7 +15,7 @@ netlify build **Flags** -- `context` (*string*) - Build context +- `context` (*string*) - Specify a build context - `dry` (*boolean*) - Dry run: show instructions without running them - `offline` (*boolean*) - disables any features that require network access - `debug` (*boolean*) - Print debugging information diff --git a/docs/commands/dev.md b/docs/commands/dev.md index bdd4c5ef545..1a225538df2 100644 --- a/docs/commands/dev.md +++ b/docs/commands/dev.md @@ -74,6 +74,7 @@ netlify dev:exec **Flags** +- `context` (*production | deploy-preview | branch-deploy | dev*) - Specify a deploy context for environment variables - `debug` (*boolean*) - Print debugging information - `httpProxy` (*string*) - Proxy server address to route requests through. - `httpProxyCertificateFilename` (*string*) - Certificate file to use when connecting using a proxy server diff --git a/src/commands/build/build.js b/src/commands/build/build.js index 3813c9eae38..14974c41d29 100644 --- a/src/commands/build/build.js +++ b/src/commands/build/build.js @@ -1,6 +1,8 @@ +const process = require('process') + // @ts-check const { getBuildOptions, runBuild } = require('../../lib/build') -const { error, exit, generateNetlifyGraphJWT, getToken } = require('../../utils') +const { error, exit, generateNetlifyGraphJWT, getEnvelopeEnv, getToken } = require('../../utils') /** * @param {import('../../lib/build').BuildConfig} options @@ -15,9 +17,14 @@ const checkOptions = ({ cachedConfig: { siteInfo = {} }, token }) => { } } -const injectNetlifyGraphEnv = async function (command, { api, buildOptions, site }) { - const siteData = await api.getSite({ siteId: site.id }) - const authlifyTokenId = siteData && siteData.authlify_token_id +const injectEnv = async function (command, { api, buildOptions, context, site, siteInfo }) { + const isUsingEnvelope = siteInfo && siteInfo.use_envelope + const authlifyTokenId = siteInfo && siteInfo.authlify_token_id + + const { env } = buildOptions.cachedConfig + if (isUsingEnvelope) { + buildOptions.cachedConfig.env = await getEnvelopeEnv({ api, context, env, siteInfo }) + } if (authlifyTokenId) { const netlifyToken = await command.authenticate() @@ -48,12 +55,12 @@ const injectNetlifyGraphEnv = async function (command, { api, buildOptions, site */ const build = async (options, command) => { command.setAnalyticsPayload({ dry: options.dry }) - // Retrieve Netlify Build options const [token] = await getToken() + const { cachedConfig, siteInfo } = command.netlify const buildOptions = await getBuildOptions({ - cachedConfig: command.netlify.cachedConfig, + cachedConfig, token, options, }) @@ -61,7 +68,8 @@ const build = async (options, command) => { if (!options.offline) { checkOptions(buildOptions) const { api, site } = command.netlify - await injectNetlifyGraphEnv(command, { api, site, buildOptions }) + const context = { options } + await injectEnv(command, { api, buildOptions, context, site, siteInfo }) } const { exitCode } = await runBuild(buildOptions) @@ -77,8 +85,8 @@ const createBuildCommand = (program) => program .command('build') .description('(Beta) Build on your local machine') + .option('--context ', 'Specify a build context', process.env.CONTEXT || 'production') .option('--dry', 'Dry run: show instructions without running them', false) - .option('--context [context]', 'Build context') .option('-o, --offline', 'disables any features that require network access', false) .addExamples(['netlify build']) .action(build) diff --git a/src/commands/dev/dev-exec.js b/src/commands/dev/dev-exec.js index 86454f73f85..cc53e8448f0 100644 --- a/src/commands/dev/dev-exec.js +++ b/src/commands/dev/dev-exec.js @@ -1,6 +1,7 @@ +const { Option } = require('commander') const execa = require('execa') -const { injectEnvVariables } = require('../../utils') +const { getEnvelopeEnv, injectEnvVariables } = require('../../utils') /** * The dev:exec command @@ -8,8 +9,14 @@ const { injectEnvVariables } = require('../../utils') * @param {import('../base-command').BaseCommand} command */ const devExec = async (cmd, options, command) => { - const { cachedConfig, config, site } = command.netlify - await injectEnvVariables({ devConfig: { ...config.dev }, env: cachedConfig.env, site }) + const { api, cachedConfig, config, site, siteInfo } = command.netlify + + let { env } = cachedConfig + if (siteInfo.use_envelope) { + env = await getEnvelopeEnv({ api, context: options.context, env, siteInfo }) + } + + await injectEnvVariables({ devConfig: { ...config.dev }, env, site }) await execa(cmd, command.args.slice(1), { stdio: 'inherit', @@ -25,6 +32,11 @@ const createDevExecCommand = (program) => program .command('dev:exec') .argument('<...cmd>', `the command that should be executed`) + .addOption( + new Option('--context ', 'Specify a deploy context for environment variables') + .choices(['production', 'deploy-preview', 'branch-deploy', 'dev']) + .default('dev'), + ) .description( 'Exec command\nRuns a command within the netlify dev environment, e.g. with env variables from any installed addons', ) diff --git a/src/commands/dev/dev.js b/src/commands/dev/dev.js index 52334b2f061..e82fa0abaff 100644 --- a/src/commands/dev/dev.js +++ b/src/commands/dev/dev.js @@ -430,7 +430,7 @@ const dev = async (options, command) => { } let { env } = command.netlify.cachedConfig - if (siteInfo.use_envelope) { + if (!options.offline && siteInfo.use_envelope) { env = await getEnvelopeEnv({ api, context: options.context, env, siteInfo }) }