The Jenkins pipeline throws a shell exit code so that the stage does not work

Jenkins absolute groovy noob pipeline here, I have a stage

stage('Building and Deploying'){ def build = new Build() build.deploy() } 

which uses a shared lib, the source of Build.groovy is here:

 def deploy(branch='master', repo='xxx'){ if (env.BRANCH_NAME.trim() == branch) { def script = libraryResource 'build/package_indexes/python/build_push.sh' // TODO: Test out http://stackoverflow.com/questions/40965725/jenkins-pipeline-cps-global-lib-resource-file-for-shell-script-purpose/40994132#40994132 env.PYPI_REPO = repo sh script }else { echo "Not pushing to repo because branch is: "+env.BRANCH_NAME.trim()+" and not "+branch } } 

The problem is that if it is impossible to push the assembly to the remote repo (see below), the stage still ends successfully.

 running upload Submitting dist/xxx-0.0.7.tar.gz to https://xxx.jfrog.io/xxx/api/pypi/grabone-pypi-local Upload failed (403): Forbidden ... Finished: SUCCESS 

How can I release the shell exit code and crash?

+6
source share
2 answers

Just stumbled upon this question again, it turned out to be a Python version problem, I can’t remember the exact version of Python, but it was a problem in setuptools, updating IIRC Python to 2.7.1x fixed it.

0
source

The sh step returns the same status code that your actual sh command returns (in this case, your script). From the sh documentation :

Typically, a script that exits with a nonzero status code will cause the step to fail with an exception.

You must ensure that your script returns a non-zero status code on failure. If you are not sure what your script will return, you can check the return value using the returnStatus parameter for step sh , which will not fail, but will return a status code. For instance:

 def statusCode = sh script:script, returnStatus:true 

You can then use this status code to set the result of your current build.

You can use:

  • currentBuild.result = 'FAILURE' or currentBuild.result = 'UNSTABLE' to mark the step as red / yellow, respectively. In this case, the assembly will still process the following steps.
  • error "Your error message" if you want the assembly to fail and immediately complete.
+15
source

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


All Articles