Skip to content

Commit

Permalink
feat: support env context in dev:exec and build (#4949)
Browse files Browse the repository at this point in the history
* feat: support --context in dev:exec

* feat: support envelope env vars with --context in `netlify build`

* chore: update contributors field

* chore: npm run docs

* chore: support context env var

* chore: wip

* chore: support branches as build context

* docs: npm run docs

Co-authored-by: jasonbarry <jasonbarry@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 17, 2022
1 parent 7a2af72 commit 6fad2a8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/commands/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/commands/dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions src/commands/build/build.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -48,20 +55,21 @@ 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,
})

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)
Expand All @@ -77,8 +85,8 @@ const createBuildCommand = (program) =>
program
.command('build')
.description('(Beta) Build on your local machine')
.option('--context <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)
Expand Down
18 changes: 15 additions & 3 deletions src/commands/dev/dev-exec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const { Option } = require('commander')
const execa = require('execa')

const { injectEnvVariables } = require('../../utils')
const { getEnvelopeEnv, injectEnvVariables } = require('../../utils')

/**
* The dev:exec command
* @param {import('commander').OptionValues} options
* @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',
Expand All @@ -25,6 +32,11 @@ const createDevExecCommand = (program) =>
program
.command('dev:exec')
.argument('<...cmd>', `the command that should be executed`)
.addOption(
new Option('--context <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',
)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

Expand Down

1 comment on commit 6fad2a8

@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: 222 MB

Please sign in to comment.