Bash – Jenkins execute shell return value commands vs file

bashJenkinsscriptingshell-scripting

I was using the Execute shell function in Jenkins with a different set of commands (calling the newman testing application). When one of the test cases was failing through newman, my build was marked as a failure.

Previous Execute shell

newman run ./test/postman_collection_1.json --environment ./test/postman_environment.json --bail
newman run ./test/postman_collection_2.json --environment ./test/postman_environment.json --bail

Since I move moved all these commands to a script file, and used "Execute shell" to execute this script file (which are a copy of the commands previously directly in the "Execute shell", my build is not marked as a failure anymore when one of the newman test cases fails.

New Execute shell:

chmod +x ./docs/ci_tests.sh
./docs/ci_tests.sh

Content of ci_tests.sh:

#!/bin/bash
newman run ./test/postman_collection_1.json --environment ./test/postman_environment.json --bail
newman run ./test/postman_collection_2.json --environment ./test/postman_environment.json --bail

Any reason why the return value of the commands (newman) are not triggering a jenkins fail when I am using them through a file?

Best Answer

You have to set the shell environment to exit when a test failed set -e

-e When this option is on, when any command fails (for any of the reasons listed in Consequences of Shell Errors or by returning an exit status greater than zero), the shell immediately shall exit, as if by executing the exit special built-in utility with no arguments, with the following exceptions: The failure of any individual command in a multi-command pipeline shall not cause the shell to exit. Only the failure of the pipeline itself shall be considered.

The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline beginning with the ! reserved word, or any command of an AND-OR list other than the last.

If the exit status of a compound command other than a subshell command was the result of a failure while -e was being ignored, then -e shall not apply to this command.

You can find more info regarding bash options at set - set or unset options and positional parameters

So your bash script should become

#!/bin/bash
set -e 
newman run ./test/postman_collection_1.json --environment     ./test/postman_environment.json --bail
newman run ./test/postman_collection_2.json --environment ./test/postman_environment.json --bail