From 766acc1b0b23aca828580ab16d1368fbbedac25c Mon Sep 17 00:00:00 2001 From: erezrokah Date: Tue, 3 Nov 2020 15:22:57 +0100 Subject: [PATCH 1/3] fix(build): pass env variables to build command --- src/commands/build/index.js | 10 ++++------ src/commands/deploy.js | 4 ++-- src/lib/build.js | 26 +++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/commands/build/index.js b/src/commands/build/index.js index da62016e5b7..f41f16cb9f3 100644 --- a/src/commands/build/index.js +++ b/src/commands/build/index.js @@ -9,8 +9,8 @@ class BuildCommand extends Command { // Run Netlify Build async run() { // Retrieve Netlify Build options - const options = getBuildOptions({ - netlify: this.netlify, + const options = await getBuildOptions({ + context: this, token: this.getConfigToken()[0], flags: this.parse(BuildCommand).flags, }) @@ -28,13 +28,11 @@ class BuildCommand extends Command { checkOptions({ cachedConfig, token }) { const { siteInfo = {} } = JSON.parse(cachedConfig) if (!siteInfo.id && process.env.NODE_ENV !== 'test') { - console.error('Could not find the site ID. Please run netlify link.') - this.exit(1) + this.error('Could not find the site ID. Please run netlify link.') } if (!token) { - console.error('Could not find the access token. Please run netlify login.') - this.exit(1) + this.error('Could not find the access token. Please run netlify login.') } } } diff --git a/src/commands/deploy.js b/src/commands/deploy.js index 452f4909f3a..66ab003198f 100644 --- a/src/commands/deploy.js +++ b/src/commands/deploy.js @@ -389,8 +389,8 @@ class DeployCommand extends Command { } if (flags.build) { - const options = getBuildOptions({ - netlify: this.netlify, + const options = await getBuildOptions({ + context: this, token: this.getConfigToken()[0], flags, }) diff --git a/src/lib/build.js b/src/lib/build.js index 72dc4352c67..628a728fa9b 100644 --- a/src/lib/build.js +++ b/src/lib/build.js @@ -1,14 +1,34 @@ const build = require('@netlify/build') +const { getSiteInformation } = require('../utils/dev') + +const getBuildEnv = async ({ context }) => { + const { warn, error, netlify } = context + const { site, api } = netlify + const { teamEnv, addonsEnv, siteEnv, dotFilesEnv } = await getSiteInformation({ + api, + site, + warn, + error, + }) + const env = { ...teamEnv, ...addonsEnv, ...siteEnv, ...dotFilesEnv } + return env +} + // We have already resolved the configuration using `@netlify/config` // This is stored as `netlify.cachedConfig` and can be passed to // `@netlify/build --cachedConfig`. -const getBuildOptions = ({ netlify, token, flags }) => { - const cachedConfig = JSON.stringify(netlify.cachedConfig) +const getBuildOptions = async ({ context, token, flags }) => { + const cachedConfig = JSON.stringify(context.netlify.cachedConfig) const { dry, debug } = flags // buffer = true will not stream output const buffer = flags.json || flags.silent - return { cachedConfig, token, dry, debug, mode: 'cli', telemetry: false, buffer } + + const env = await getBuildEnv({ + context, + }) + + return { cachedConfig, token, dry, debug, mode: 'cli', telemetry: false, buffer, env } } const runBuild = async (options) => { From f91617561b504411e3ddb32f579d849a05828aec Mon Sep 17 00:00:00 2001 From: erezrokah Date: Tue, 3 Nov 2020 15:44:23 +0100 Subject: [PATCH 2/3] test: fix tests --- src/commands/build/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/build/index.js b/src/commands/build/index.js index f41f16cb9f3..84f8fabf2b3 100644 --- a/src/commands/build/index.js +++ b/src/commands/build/index.js @@ -28,11 +28,11 @@ class BuildCommand extends Command { checkOptions({ cachedConfig, token }) { const { siteInfo = {} } = JSON.parse(cachedConfig) if (!siteInfo.id && process.env.NODE_ENV !== 'test') { - this.error('Could not find the site ID. Please run netlify link.') + this.error('Could not find the site ID. Please run netlify link.', { exit: 1 }) } if (!token) { - this.error('Could not find the access token. Please run netlify login.') + this.error('Could not find the access token. Please run netlify login.', { exit: 1 }) } } } From 1556c5694df980e1b53ffe8eb4fb2592d444ec76 Mon Sep 17 00:00:00 2001 From: erezrokah Date: Tue, 3 Nov 2020 21:28:11 +0100 Subject: [PATCH 3/3] fix: don't pass .env envs --- src/lib/build.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/build.js b/src/lib/build.js index 628a728fa9b..95e8f9102cc 100644 --- a/src/lib/build.js +++ b/src/lib/build.js @@ -5,13 +5,13 @@ const { getSiteInformation } = require('../utils/dev') const getBuildEnv = async ({ context }) => { const { warn, error, netlify } = context const { site, api } = netlify - const { teamEnv, addonsEnv, siteEnv, dotFilesEnv } = await getSiteInformation({ + const { teamEnv, addonsEnv, siteEnv } = await getSiteInformation({ api, site, warn, error, }) - const env = { ...teamEnv, ...addonsEnv, ...siteEnv, ...dotFilesEnv } + const env = { ...teamEnv, ...addonsEnv, ...siteEnv } return env }