在try()中的闭包内限定groovy变量以从catch()访问

时间:2017-08-28 18:05:00

标签: jenkins groovy

如何在catch()语句中的try()中访问闭包内指定的变量的值。这是我想要实现的,但我不断获得我指定的初始化值。

注意:这是针对Jenkins文件,同时使用groovy DSL并具有一些真正的常规功能。

node('linux') {

        def val_log = null;

    try{

        stage('abc') {

        val_log = //some steps that generates a log that i want 

        }  

        println '$val_log'  //this works

    }

    catch(err)
        stage('mail') {
        // code to send email notification with '$val_log'  
        //--> the $val_log returns 'null' and not the 
        //value assigned in the try() part.
        }
}

有没有办法在catch()语句中获取变量的值。

谢谢...

1 个答案:

答案 0 :(得分:0)

您可以尝试将值转储到文件中 -

echo $BUILD_VERSION >> build_version.txt

并加载文件值

def build_num = 
readFile("${env.WORKSPACE}/build_version.txt")