1

I have a pipeline with some stages and logic. The only possible debugger is adding try-catch to each stage:

stage ("distribution"){
            steps{
                script{
                    try{
    amd_distribution_distribute_bundle
                    }
                   catch (Exception e) {
                    echo e.toString()
                    }
            }
        }
    }

however, if one stage fails, I need to stop the pipeline:

status = sh script: '''
                set +x
                python3.4 main.py --command create_bundle
                ''',  returnStatus:true
             }
        }
     }

    if (status == 1) {
    error("returning 1 from func")
    }

but the try-catch from the pipeline actually cancel it (it proceed to other stages. is there a way to combine them?

4

You may use and update a flag to false in the catch and then use that flag in the when directive of other stage(s) that you want to skip because of any failure in the given stage. Example:

boolean flagStageOneSuccess = false

pipeline {
    agent any

    stages {
        stage ('stage-1') {
            steps {
                try {
                    // Do something that might fail

                    flagStageOneSuccess = true

                } catch (Exception e) {
                    // Handle the exception
                    flagStageOneSuccess = false
                }
            }
        }

        stage ('stage-2') {
            when {
                equals expected: true, actual: flagStageOneSuccess
            }
            steps {
                // Do something in stage-2
            }
        }
    }
}

It would also benefit you in viewing the status of your stage in Blue Ocean. If the stage is skipped, it shows as 'Not built'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.