Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy and build have different behaviours when run sequentially and as one command #6511

Open
angry-dan opened this issue Apr 15, 2024 · 0 comments
Labels
type: bug code to address defects in shipped code

Comments

@angry-dan
Copy link

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.

Screenshot 2024-04-15 at 16 54 03

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
@angry-dan angry-dan added the type: bug code to address defects in shipped code label Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

1 participant