Home
Support Forums

Build command failed using the Netlify CLI (on GitLab runner)

Hi there,

I’ve been having an issue that I can’t for the life of me figure out.

I am trying to set up a GitLab CI/CD pipeline to build and deploy a Next JS app with the netlify-plugin-nextjs build plugin. I’m using GitLab’s CI/CD to create deployments because I don’t want to use Netlify’s default branch deploys as I want to deploy from tags instead.

I have a separate pipeline job that is responsible for only creating the build and a separate jobs for deploying to different environments. The build relies on external dependencies so I have the separate build job so in the case of needing to redeploy a previous build, I don’t have to rely on those dependencies to still be available.

Currently, to create the build:

I have installed netlify-plugin-nextjs as a dependency,

I have a netlify.toml file:

netlify.toml
[build]
  command = "yarn build"
  publish = "out"

[[plugins]]
  package = "@netlify/plugin-nextjs"

I am using the node:15.14.0-alpine Docker image to create the build

I have the NETLIFY_SITE_ID and NETLIFY_AUTH_TOKEN set as environment variables and to create the build I am running the following command: netlify build

Running this command results in "build.command" failed with the following error message:

Error message
Command failed with ENOENT: yarn build
spawn bash ENOENT

Error location
In build.command from netlify.toml:
yarn build

Due to the ‘ENOENT’ I assume it is because yarn doesn’t exist, but it should as it is included in the node:15.14.0-alpine image and yarn install and yarn build work fine when executed directly in the shell. It just doesn’t seem to work when netlify build attempts to execute it.

Full GitLab runner (Docker container) log:

Build log
Running with gitlab-runner 14.2.0-rc1 (0ac3a46e)
  on docker-auto-scale 72989761
Preparing the "docker+machine" executor
Using Docker executor with image node:15.14.0-alpine ...
Pulling docker image node:15.14.0-alpine ...
Using docker image sha256:75631da67663db1d5f3518892db3259b52f105d4b692f88cf2457a0b27abf0e1 for node:15.14.0-alpine with digest node@sha256:6edd37368174c15d4cc59395ca2643be8e2a1c9846714bc92c5f5c5a92fb8929 ...
Preparing environment
Running on runner-72989761-project-27605950-concurrent-0 via runner-72989761-srm-1630479377-15936afa...
Getting source from Git repository
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/nextjs-app/.git/
Created fresh repository.
Checking out 90a330c6 as test/configure-cd-test...
Skipping Git submodules setup
Restoring cache
Checking cache for b419df2c3c894c3cb1b395add663c852927bfdfb...
Downloading cache.zip from <cache url>
Successfully extracted cache
Executing "step_script" stage of the job script
Using docker image sha256:75631da67663db1d5f3518892db3259b52f105d4b692f88cf2457a0b27abf0e1 for node:15.14.0-alpine with digest node@sha256:6edd37368174c15d4cc59395ca2643be8e2a1c9846714bc92c5f5c5a92fb8929 ...
$ echo "Creating build..."
Creating build...
$ yarn install
yarn install v1.22.5
[1/4] Resolving packages...
success Already up-to-date.
$ husky install
husky - not a Git repository, skipping hooks installation
Done in 1.19s.
$ yarn netlify:ci build
yarn run v1.22.5
$ netlify build
​
────────────────────────────────────────────────────────────────
  Netlify Build                                                 
────────────────────────────────────────────────────────────────
​
❯ Version
  @netlify/build 18.7.2
​
❯ Flags
  dry: false
  offline: false
​
❯ Current directory
  /builds/nextjs-app
​
❯ Config file
  /builds/nextjs-app/netlify.toml
​
❯ Context
  production
​
❯ Loading plugins
   - @netlify/plugin-nextjs@3.9.0 from netlify.toml and package.json
​
────────────────────────────────────────────────────────────────
  1. onPreBuild command from @netlify/plugin-nextjs             
────────────────────────────────────────────────────────────────
​
Using Next.js 11.0.1
info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
No Next.js cache to restore.
​
(@netlify/plugin-nextjs onPreBuild completed in 803ms)
​
────────────────────────────────────────────────────────────────
  2. build.command from netlify.toml                            
────────────────────────────────────────────────────────────────
​
$ yarn build
​
────────────────────────────────────────────────────────────────
  "build.command" failed                                        
────────────────────────────────────────────────────────────────
​
  Error message
  Command failed with ENOENT: yarn build
  spawn bash ENOENT
​
  Error location
  In build.command from netlify.toml:
  yarn build
​
  Resolved config
  build:
    command: yarn build
    commandOrigin: config
    publish: /builds/nextjs-app/out
    publishOrigin: config
  plugins:
    - inputs: {}
      origin: config
      package: '@netlify/plugin-nextjs'
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Cleaning up file based variables
ERROR: Job failed: exit code 2

package.json:

package.json
{
  "name": <app name>,
  "version": "1.5.9",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "type-check": "tsc --project tsconfig.json --pretty --noEmit",
    "lint": "eslint --ext js,jsx,ts,tsx --fix",
    "postinstall": "husky install",
    "netlify:ci": "netlify"
  },
  "dependencies": {
    "@fontsource/baloo-2": "^4.4.5",
    "@fontsource/open-sans": "^4.4.5",
    "@netlify/plugin-nextjs": "^3.9.0",
    "classnames": "^2.3.1",
    "next": "11.0.1",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@types/react": "17.0.11",
    "@typescript-eslint/eslint-plugin": "^4.28.0",
    "@typescript-eslint/parser": "^4.28.0",
    "eslint": "7.29.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-prettier": "^3.4.0",
    "eslint-plugin-react": "^7.24.0",
    "eslint-plugin-react-hooks": "^4.2.0",
    "husky": "^6.0.0",
    "lint-staged": "^11.0.0",
    "netlify-cli": "^6.8.1",
    "prettier": "^2.3.1",
    "sass": "^1.35.1",
    "typescript": "4.3.4"
  },
  "browserslist": [
    "last 2 version",
    ">0.2%"
  ]
}

I have found similar issues in this forum but none of which had the same error or fixes that worked for me. Most of them seemed to be because yarn wasn’t installed which isn’t the case for me.

I’m very confused by this and any help will be much appreciated.

Cheers!

Hi @dallan,

I can’t comment on what’s happening in that Docker image as that’s something I have no knowledge about/never tested.

But, what happens if you add yarn as a dependency to your package.json?

Hey @hrishikesh,

Thanks for the response!

I just tried that and I’m getting the exact same error unfortunately. I also tried changing the build command to npm install -g yarn && yarn build and npm run build but got the same error both times.

The Docker image is just an image that has node and a bunch of commonly used node packages so you don’t have to install them every time you want to run a simple script.

Thank you for trying that.

I understand, but there’s no way for us to debug it except for cloning it ourselves and trying it out locally.

Would it be possible for you to try a different image or use npm?

I figured out the problem, the node:15.14.0-alpine Docker image doesn’t have bash included which netlify build relies on for running the build command on Linux.

The spawn bash ENOENT error was because it couldn’t find bash.

It seems that the ‘alpine’ Node Docker images don’t have bash and this was fixed by using the standard image: node:15.14.0 instead.

Got there in the end.

Thanks for your help, much appreciated!