Jenkins sh transition step does not return another status code

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 run the script directly in the shell:

sh push.sh

It returns status code 1 when it was not possible to click on 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 it was not possible to click on 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 can I make status code 1 return if it does not work in Jenkins' job?

UPDATE

It seems the problem is not Jenkins, I can replicate the same problem on the Jenkins server shell.

0
source share
2 answers

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

As for your output, it looks like you have different results, are you sure you should get the same exit code? echo is called only in the second case.

0
source

This is not a Jenkins pipeline problem, but a bash problem. You can read this article to find out how bash script output script works .

On Linux, any script launched from the command line has an exit code. With bash, 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 internal Python script returns non-zero code, but the echo command, which is the last command executed, succeeds, and your script returns 0 / success code.

You can remove echo or add exit 1 if necessary.

0
source

Source: https://habr.com/ru/post/1015231/


All Articles