deploy and build have different behaviours when run sequentially and as one command
Opened this issue · 0 comments
Describe the bug
I'm seeing inconsistencies in the way that the deploy and build commands work when called as one (deploy --build
) or in sequence (deploy && build
).
When I run deploy --build
I'm finding that the --context
argument is being ignored, and my builds are always created using the cli local dev set of environment variables. On the other hand, when I run netlify build
as a standalone command, I can see that the emitted build respects the context variable.
I've done some digging in the source, and can see that both the deploy command and the build command execute the same runBuild
function to do the actual work of creating the build, however in the build
command file there's an injectEnv
command which reads the value of the context argument and populates the cachedConfig ready to be red later on during the build, whereas the deploy command file doesn't do this step first.
Have a look at
build.ts and deploy.ts
I'm using Next.JS.
Steps to reproduce
I'm using Next.JS.
I've got an included TS file that looks a bit like this
const config = {
context: process.env.NEXT_PUBLIC_BUILD_CONTEXT,
};
if (typeof window === "object") {
window.nextjsConfig = config;
}
In the Netlify UI I've created an env var called NEXT_PUBLIC_BUILD_CONTEXT
and set a different value for each context.
When I run NETLIFY_SITE_ID=*** NETLIFY_AUTH_TOKEN=*** netlify deploy --build --context=deploy-preview
, then visit the site and inspect the value of window.nextjsConfig
in the console, I see the value "local" I set for the dev context. Changing to different contexts has no effect. Prod deployments also carry the local
value.
If I run netlify build --context=deploy-preview
and then inspect the contents of the .next
folder, I can see that the correct values are being build into the compiled JS files. Unfortunately, if I then run netlify deploy
, due to what I assume is an unrelated bug, the deployed site always shows a 404 error, I suspect because the netlify.toml file is being incorrectly deployed and the various Next.JS SSR redirect rules aren't being deployed - I haven't had a chance to fully investigate this yet.
Configuration
[[plugins]]
package = "@netlify/plugin-nextjs"
[build]
command = "yarn build"
publish = ".next"
Environment
System:
OS: macOS 14.2.1
CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Memory: 302.13 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 21.7.1 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.5.0 - /usr/local/bin/npm