Jenkins pipeline bubble up the shell exit code to fail the stage

Absolute Jenkins pipeline/groovy noob here, I have a stage

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

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

  • Jenkins Pipeline Plugin pass build Job parameters between jobs started in a pipeline
  • How to get currently running job's node name in jenkins using groovy
  • Jenkins - groovy script - get last successful build date in dd-mm-yyyy format
  • Jenkins Pipeline continue latest build at certain time
  • Replace classpath of a running Groovy Script (Jenkins Pipeline script)
  • Get gradle variables in jenkins pipeline script
  • def deploy(branch='master', repo='xxx'){
        if (env.BRANCH_NAME.trim() == branch) {
            def script = libraryResource 'build/package_indexes/python/'
            // TODO: Test out
            env.PYPI_REPO = repo
            sh script
        }else {
            echo "Not pushing to repo because branch is: "+env.BRANCH_NAME.trim()+" and not "+branch

    Problem is when failing to push the build to a remote repo (see below), the stage still ends up showing successful.

    running upload
    Submitting dist/xxx-0.0.7.tar.gz to
    Upload failed (403): Forbidden
    Finished: SUCCESS

    How do I bubble up the exit code of the shell script and fail the stage?

  • Apache basic authentication issue with reverse proxy
  • TAP::Harness timing issue with TAP::Formatter::JUnit
  • Ivy, Ant, Jenkins - Is it good idea to to a <ivy:cleancache> on Jenkins builds?
  • Queue TFS 2012 build from Jenkins
  • Parallel Test Execution Jenkins
  • Jenkins . Git Client Plugin
  • 2 Solutions collect form web for “Jenkins pipeline bubble up the shell exit code to fail the stage”

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

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

    You have to make sure that your script returns a nonzero status code when it fails. If you’re not sure what your script returns, you can check the return value using the returnStatus param of the sh step, which will not fail the build but will return the status code. E.g:

    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 build will still process the next steps.
    • error "Your error message" if you want the build to fail and exit immediately.

    Just stumbled onto this question again, it turned out to be a Python version problem, I cannot remember the exact version of the Python but it was a problem within setuptools, IIRC upgrading Python to 2.7.1x fixed it.

    Git Baby is a git and github fan, let's start git clone.