从 jenkins groovy 脚本中的 bash 脚本中捕获退出代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48174857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capture exit code from bash scripts in jenkins groovy scripts
提问by Foobar-naut
Executing a bash script copy_file.sh
from Jenkins Groovy script and trying to shoot mail depending upon the exit code generated form the bash script.
copy_file.sh
从 Jenkins Groovy 脚本执行 bash 脚本并尝试根据从 bash 脚本生成的退出代码发送邮件。
copy_file.sh
:
copy_file.sh
:
#!/bin/bash
$dir_1=/some/path
$dir_2=/some/other/path
if [ ! -d $dir ]; then
echo "Directory $dir does not exist"
exit 1
else
cp $dir_2/file.txt $dir_1
if [ $? -eq 0 ]; then
echo "File copied successfully"
else
echo "File copy failed"
exit 1
fi
fi
Portion of the groovy script
:
的部分groovy script
:
stage("Copy file") {
def rc = sh(script: "copy_file.sh", returnStatus: true)
echo "Return value of copy_file.sh: ${rc}"
if (rc != 0)
{
mail body: 'Failed!',
subject: 'File copy failed',
to: "[email protected]"
System.exit(0)
}
else
{
mail body: 'Passed!',
subject: 'File copy successful',
to: "[email protected]"
}
}
Now, irrespective of the exit 1
s in bash script, groovy script is always getting return code 0
in rc
and shooting Passed!
mails!
现在,无论的exit 1
在bash脚本S,Groovy脚本总是得到返回代码0
中rc
,投篮命中率Passed!
邮件!
Any suggestions why I can not receive the exit code from bash script in this Groovy script?
有什么建议为什么我无法在此 Groovy 脚本中从 bash 脚本接收退出代码?
DO I NEED TO USE RETURN CODE INSTEAD OF EXIT CODE?
我是否需要使用返回代码而不是退出代码?
回答by natansun
Your groovy code is OK.
你的常规代码没问题。
I created a new pipeline job to check your problem, but altered it a bit.
我创建了一个新的管道作业来检查您的问题,但对其进行了一些更改。
Instead of running your shell script copy_file.sh
I created ~/exit_with_1.sh
script, that only exits with an exit code of 1.
copy_file.sh
我创建的~/exit_with_1.sh
脚本没有运行你的 shell 脚本,它只以退出代码 1 退出。
The job has 2 steps:
作业有 2 个步骤:
Creation of
~/exit_with_1.sh
scriptRun the script and check the exit code that stored in
rc
.
~/exit_with_1.sh
脚本的创建运行脚本并检查存储在
rc
.
I got 1
as an exit code in this example.
If you think something wrong with your groovy <-> bash
configuration, consider replace your copy_file.sh
content with only exit 1
then try to print the result (before posting the emails).
1
在这个例子中,我得到了一个退出代码。如果您认为您的groovy <-> bash
配置有问题,请考虑替换您的copy_file.sh
内容,exit 1
然后尝试打印结果(在发布电子邮件之前)。
The jenkins job I created:
我创建的詹金斯工作:
node('master') {
stage("Create script with exit code 1"){
// script path
SCRIPT_PATH = "~/exit_with_1.sh"
// create the script
sh "echo '# This script exits with 1' > ${SCRIPT_PATH}"
sh "echo 'exit 1' >> ${SCRIPT_PATH}"
// print it, just in case
sh "cat ${SCRIPT_PATH}"
// grant run permissions
sh "chmod +x ${SCRIPT_PATH}"
}
stage("Copy file") {
// script path
SCRIPT_PATH = "~/exit_with_1.sh"
// invoke script, and save exit code in "rc"
echo 'Running the exit script...'
rc = sh(script: "${SCRIPT_PATH}", returnStatus: true)
// check exit code
sh "echo \"exit code is : ${rc}\""
if (rc != 0)
{
sh "echo 'exit code is NOT zero'"
}
else
{
sh "echo 'exit code is zero'"
}
}
post {
always {
// remove script
sh "rm ${SCRIPT_PATH}"
}
}
}