Jenkins return value from catch block

Viewed 45

I have a scripted jenkins pipeline with the code like this.

value=true
fun_stage_1()
println +value

def func_stage_1() {
    try {
        stage1{
              stage1 steps
                     } 
      }
   catch {
           value=false
           return value    
        }
}

Here I am expecting the value to be false in case of issues after calling the function , But it comes true. What I am doing wrong here?

1 Answers

Changing the value of a variable inside a function is only valid inside the function but not outside.. So if you want to override it you need to do value = func_stage_1()

This is working for me, if the "try" not succeed the function will return false:

def value = true
def func_stage_1() {
    try {
        echo "test"
     } catch (err) {
           value=false    
     }
    return value

}

pipeline {
    agent none
    stages{
        stage ('test'){
            steps{
                script{
                    value = func_stage_1()
                    echo "${value}"
                }
            }
        }
    }
}
Related