从 Jenkins Groovy 脚本执行 bash 脚本copy_file.sh
并尝试根据 bash 脚本生成的退出代码发送邮件。
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
部分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: "xyz@abc.com"
System.exit(0)
}
else
{
mail body: 'Passed!',
subject: 'File copy successful',
to: "xyz@abc.com"
}
}
现在,不管exit 1
bash 脚本中的 s 是什么,groovy 脚本总是在获取返回码0
并rc
发送Passed!
邮件!
有什么建议为什么我无法在这个 Groovy 脚本中接收来自 bash 脚本的退出代码?
我需要使用返回码而不是退出码吗?