Jenkins管道SH步骤不返回不同的状态代码 [英] Jenkins pipeline sh step not returning different status code

查看:327
本文介绍了Jenkins管道SH步骤不返回不同的状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在管道中有以下代码

def deploy(branch='master', repo='xxx'){
    if (env.BRANCH_NAME.trim() == branch) {
        def script = libraryResource 'build/package_indexes/python/build_push.sh'
        env.PYPI_REPO = repo
        sh script
    }else {
        echo "Not pushing to repo because branch is: "+env.BRANCH_NAME.trim()+" and not "+branch
    }
}

脚本如下:

if [ -f "setup.cfg" ]; then
  # only build a wheel if setup.cfg exists, so we can disable wheel build if need be
  echo "Build package ${PACKAGE_NAME} tar.gz and wheel, and push to jfrog repo: $PYPI_REPO"
  python setup.py sdist upload -r jfrog
  echo "status code: $?"
fi

当我直接在shell中执行脚本时:

When I execute the script directly in shell:

sh push.sh

当无法推送到仓库时,它将返回状态代码1,请参见输出:

It returns the status code 1 when failed to push to the repo, see output:

Submitting dist/xxx-xxxx-0.0.7.tar.gz to https://xxx.jfrog.io/xxx/xxx-pypi/
Upload failed (405): Method Not Allowed
error: Upload failed (405): Method Not Allowed
status code: 1

但是当我在Jenkins中执行管道时,如果无法推送到仓库,状态代码为0:

But when I execute the pipeline in Jenkins, the status code is 0 when failed to push to the repo:

Submitting dist/xxx-xxxx-0.0.7.tar.gz to https://xxx.jfrog.io/xxx/xxx-pypi/
Upload failed (403): Forbidden
+ echo status code: 0
status code: 0

当Jenkins作业失败时,如何使其返回状态代码1?

How do I make it to return status code 1 when it fails in Jenkins job?

更新

看起来问题不在Jenkins内部,我可以在Jenkins服务器的外壳上复制相同的问题.

Looks like the problem is not inside Jenkins, I can replicate the same problem on the shell of the Jenkins server.

推荐答案

这不是Jenkins管道问题,而是bash问题.您可以阅读这篇文章,以了解 bash脚本退出代码的工作方式.

This is not a Jenkins pipeline issue but a bash issue. You can read this article to learn how bash scripts exit codes work.

在Linux中,从命令行运行的任何脚本都具有退出代码.和 Bash脚本(如果未在脚本本身中指定退出代码) 使用的退出代码将是最后一次运行命令的退出代码.

In Linux any script run from the command line has an exit code. With Bash scripts, if the exit code is not specified in the script itself the exit code used will be the exit code of the last command run.

基本上,您的内部Python脚本返回一个非零代码,但最后执行的命令echo命令成功,使您的脚本返回0/成功代码.

Basically, your inner Python script returns a non-zero code but the echo command, which is the last command executed, is successful, making your script return a 0/success code.

您可以删除echo或在需要时添加exit 1.

You can either remove the echo or add an exit 1 when needed.

这篇关于Jenkins管道SH步骤不返回不同的状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆