Skip to content

Environment variables aren't injected properly for specific branch deploys #6891

@petervmeijgaard

Description

@petervmeijgaard

Describe the bug

When using a specific branch context for running netlify build or netlify deploy, no environment variables are being injected. This is because the API call for getting the correct branch specific environment variables is failing. The context that's provided, isn't allowed as the API returns the following error message:

{
  "code": 400,
  "message": "invalid request: non-compatible context"
}

Steps to reproduce

  1. Create a new Netlify site
  2. Add a environment variable (i.e. FOO=BAR)
  3. Add a new branch, like test and use a unique value for the environment variable (i.e. FOO=HELLO)
  4. Add a console.log within your application so that the environment variable is visible when running netlify build (i.e. console.log(process.env.FOO))
  5. Run netlify build for the specific branch (i.e. netlify build --context="branch:test)
  6. See that the log shows undefined
  7. Run netlify build for the generic branch-deploy context: netlify build --context="branch-deploy
  8. See that the logs are showing the correct environment variable

CLI command and flags

netlify build --context="branch:test"

Configuration

[build]
command = "pnpm build"
publish = ".next"

[dev]
command = "pnpm dev"
port = 8888
targetPort = 3000
autoLaunch = false

CLI output

Netlify Build
────────────────────────────────────────────────────────────────

❯ Version
  @netlify/build 35.5.10

❯ Flags
  accountId: [REDACTED]
  deployId: [REDACTED]
  packagePath: [REDACTED]

❯ Current directory
  [REDACTED]

❯ Config file
  [REDACTED]/netlify.toml

❯ Context
  branch:test

❯ Using Next.js Runtime - v5.15.5

❯ Loading plugins
   - [REDACTED] from netlify.toml and package.json
   - [REDACTED] from netlify.toml and package.json

Plugin "[REDACTED]" internal error
─────────────────────────────────────────────────────────────────────

  Error message
  Could not import plugin:
  ZodError: [
    {
      "expected": "string",
      "code": "invalid_type",
      "path": [
        "[REDACTED]"
      ],
      "message": "Invalid input: expected string, received undefined"
    },
    {
      "expected": "number",
      "code": "invalid_type",
      "received": "NaN",
      "path": [
        "[REDACTED]"
      ],
      "message": "[REDACTED] is required"
    }
  ]

  Plugin details
  Package:        [REDACTED]
  npm link:       [REDACTED]

Environment

  System:
    OS: macOS 15.7.3
    CPU: (12) arm64 Apple M4 Pro
    Memory: 74.17 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.21.1 - /Users/pvanmeijgaard/.local/state/fnm_multishells/76682_1769157887853/bin/node
    npm: 10.9.4 - /Users/pvanmeijgaard/.local/state/fnm_multishells/76682_1769157887853/bin/npm
    pnpm: 10.22.0 - /Users/pvanmeijgaard/.local/state/fnm_multishells/76682_1769157887853/bin/pnpm
    bun: 1.3.6 - /opt/homebrew/bin/bun

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugcode to address defects in shipped code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions