Why does Travis CI report a build success even though the npm publish failed?

2.4k Views Asked by At

I noticed on build https://travis-ci.org/neverendingqs/openssl-self-signed-certificate/builds/187723295 that I forgot to increase the patch version when tagging the repo. However, the build reported as passing even though the npm publish failed due to the version already existing.

Here's the tail end of the log:

Deploying application
NPM API key format changed recently. If your deployment fails, check your API key in ~/.npmrc.
http://docs.travis-ci.com/user/deployment/npm/
~/.npmrc size: 48
npm ERR! publish Failed PUT 403
npm ERR! Linux 4.8.12-040812-generic
npm ERR! argv "/home/travis/.nvm/v0.10.48/bin/node" "/home/travis/.nvm/v0.10.48/bin/npm" "publish"
npm ERR! node v0.10.48
npm ERR! npm  v2.15.1
npm ERR! code E403
npm ERR! "You cannot publish over the previously published version 1.1.5." : openssl-self-signed-certificate
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR!     /home/travis/build/neverendingqs/openssl-self-signed-certificate/npm-debug.log
No stash found.
Done. Your build exited with 0.

In case it's important, I have the test script in packages.json set to exit 0, but that occurs before the publish phase, so that shouldn't be the problem(?).

Why didn't Travis CI report the build a failure when the publish failed?

EDIT:

I used the Travis CI CLI to set up NPM publishing by running travis setup npm, based on https://docs.travis-ci.com/user/deployment/npm/.

My .travis.yml looks like this:

language: node_js
deploy:
  provider: npm
  email: myemail
  api_key:
    secure: blahblahblah
  on:
    tags: true
    repo: neverendingqs/openssl-self-signed-certificate
3

There are 3 best solutions below

2
On

Your script must have exited with an exit status of 0. This is the only thing that Travis cares about.

If you are running a script that does:

npm publish

Then it should do:

npm publish || exit 1

or something like that, to make sure that the script with that command exits with a non-zero status if the npm publish command fails.

You didn't include any example of your code but this is what I suspect may be happening here.

Actually it's even more complicated that that. Let's say that you have one script, script1 that fails:

#!/bin/sh
exit 1

And you have another script, script2 that runs it:

#!/bin/sh
./script1

then running ./script2 will also result in error - running this:

./script2 && echo OK || echo ERROR

will print ERROR. But when you have another command later:

#!/bin/sh
./script1
echo

then running ./script2 this time will not return error this time. Running:

./script2 && echo OK || echo ERROR

will print OK.

So if your npm publish is the last command in your script then it should result in the entire script returning an error status to the system, but if it's not then the system will get a status of 0 meaning success.

It all depends on how the script that Travis is running actually looks like.

0
On

A short answer is that Travis CI does not check the exit status from the npm publish command.

https://github.com/travis-ci/dpl/blob/a255a0d8efe897f8ea5a194a8a2ef73556e27817/lib/dpl/provider/npm.rb#L35

This is currently a general problem with the deployment. See https://github.com/travis-ci/dpl/issues/143

0
On

I ran into a different problem as we had some of our packaging and minification scripts located in the after_success section and even concatenating the commands did not fail the build until I later found out this was by design.

https://docs.travis-ci.com/user/customizing-the-build/#Breaking-the-Build

"If any of the commands in the first four stages of the build lifecycle return a non-zero exit code, the build is broken: If before_install, install or before_script return a non-zero exit code, the build is errored and stops immediately. If script returns a non-zero exit code, the build is failed, but continues to run before being marked as failed. The exit code of after_success, after_failure, after_script and subsequent stages do not affect the build result. However, if one of these stages times out, the build is marked as a failure."

So I moved those commands to install and non-zero exit codes did start failing the build.