Jenkins执行shell返回值命令vs文件

我在Jenkins中使用了Execute shell函数和一组不同的命令(调用newmantesting应用程序)。 当其中一个testing用例失败时,我的构build被标记为失败。

上一个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 

由于我移动了所有这些命令到一个脚本文件,并使用“Execute shell”来执行这个脚本文件(这是以前直接在“Execute shell”中的命令的副本),当我的构build不再被标记为失败其中一个newmantesting用例失败。

新buildExecute shell

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

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 

为什么当我通过文件使用它们时,命令的返回值( newman )没有触发jenkins失败的任何原因?

当testing失败时,您必须将shell环境set -e

-e当此选项打开时,当任何命令失败时(由于Shell错误后果中列出的任何原因,或通过返回大于零的退出状态),shell立即退出,就好像通过执行退出特殊的内置函数在没有参数的实用程序中,有以下例外:多命令pipe道中任何单个命令的失败都不会导致shell退出。 只有pipe​​道本身的失效才会被考虑。

执行后面的复合列表时,-e设置应该被忽略,直到,如果或者elif保留字, 保留字,或除最后一个以外的AND-OR列表的任何命令。

如果sub-shell命令以外的复合命令的退出状态是-e被忽略时的失败结果,则-e不适用于此命令。

您可以在set – set或者unset选项和位置参数中find更多关于bash选项的信息

所以你的bash脚本应该成为

 #!/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