Jenkins pipeline sh step not returning different status code [duplicate]

冷暖自知 提交于 2019-12-25 08:46:18

问题


I have the following code inside the pipeline

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
    }
}

And the script looks like this:

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

When I execute the script directly in shell:

sh push.sh

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

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

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

UPDATE

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


回答1:


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

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.

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.

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




回答2:


To return the status code, you need to invoke sh like this: sh returnStatus: true, script: 'echo "test"'

Regarding to your output, it looks like you have different results here, are you sure that you should get the same exit code? The echo is only invoked in the second case.



来源:https://stackoverflow.com/questions/42475073/jenkins-pipeline-sh-step-not-returning-different-status-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!