Home
Support Forums

Build successful despite error code 1

Site: comedian-leopard-30163.netlify.app

Hello there,

I triggered a deploy on my site and despite an error code from a node.js script, it successfully deployed my page. I use a bash shell script with multiple commands, this one as build command in my build configuration on netlify (the error code stems from the nod.js call on line 15). I wonder what the reason is why this deploy did not fail ?

  • Build problems? Link or paste the FULL build log & build settings screenshot:

Build Log: Netlify App

  • On Line 176 my node script returning an error message and error code 1 through process.exit(1)

hmm interesting. does the build fail appropriately locally? :thinking:

Yes, my shell gave me the 1 exit code.

Hi, @eimfach. The way that bash scripts behave is that you must explicitly provide an exit code (for example exit 127) or they will return the exit code of the last statement in the script.

This is the content of the script you linked to in the repo:

#!/bin/bash
pip install pipenv

rm -rf stylesheets/inline/critical
mkdir stylesheets/inline/critical

pipenv install

npm i -g pnpm

pnpm i

pipenv run python compile.py

node build-critical-css.js

echo '------> Rerun .journal complilation to include inline css'
pipenv run python compile.py

With the script above, the exit code for the node build-critical-css.js statement has no impact on the exit code of the script. Only the exit code of the final statement pipenv run python compile.py determines the exit code at this script is currently written.

For example, if the script contained this:

#!/bin/bash
echo 'testing exit codes'
echo
false 

then the exist code will be 1 (because false returns 1):

$ cat ./build.sh
#!/bin/bash
echo 'testing exit codes'
echo
false

username@sandbox-ubuntu : ~/tmp/1 : 2021-06-29 08:04:16 :
$ ./build.sh ; echo $?
testing exit codes

1

However, if I add one more echo statement, it will exit with a zero. So this:

#!/bin/bash
echo 'testing exit codes'
echo
false 
echo

will exit 0 like this:

$ cat ./build.sh
#!/bin/bash
echo 'testing exit codes'
echo
false
echo

username@sandbox-ubuntu : ~/tmp/1 : 2021-06-29 08:04:55 :
$ ./build.sh ; echo $?
testing exit codes


0

The exit code of the false command is swallowed by the script and only the exit code of echo (which is 0) is returned by the script.

I would recommend adding tests for each statement and exiting if there are errors. One method to do this would be to append || exit 1 to the end fo each statement like so:

#!/bin/bash
pip install pipenv || exit 1

rm -rf stylesheets/inline/critical || exit 1
mkdir stylesheets/inline/critical || exit 1

pipenv install || exit 1

npm i -g pnpm || exit 1

pnpm i || exit 1

pipenv run python compile.py || exit 1

node build-critical-css.js || exit 1

echo '------> Rerun .journal complilation to include inline css'
pipenv run python compile.py || exit 1

The || is the logical OR operator so the exit statement only gets run if the previous command returns an error.

Now, there are likely better ways to test the exit codes of each command but this will work. If there are other questions about this, please let us know.

1 Like

Thank you, that was very helpful. I think I gonna try it with trap "echo Build Failed; exit 1" ERR at my first line of the build script