大网数据广告活动
服务器运维问答

Jenkins execute shell return value commands vs file

· 52CE 我爱测

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 Execu…

问题原文

原帖链接 · abd · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · dkokmadis · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。