简体   繁体   English

在try()的闭包内部对Groovy变量进行作用域设置以从catch()访问

[英]Scoping of a groovy variable inside a closure in try() to access from catch()

how do i access the value of a variable assigned inside a closure within a try() in the catch() statement. 如何在catch()语句的try()中访问闭包内分配的变量的值。 here is what i am trying to acheive but i keep getting the initialization value that i assigned. 这是我要达到的目标,但是我一直在获取分配的初始化值。

Note: this is for a Jenkinsfile, while uses a groovy DSL and also with some real groovy capabilities. 注意:这是针对Jenkinsfile的,同时使用groovy DSL并具有一些实际的groovy功能。

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.
        }
}

Is there a way, to get the value for the variable inside the catch() statement. 有没有一种方法可以获取catch()语句中变量的值。

Thank you... 谢谢...

you can try and dump the value into a file - 您可以尝试将值转储到文件中-

echo $BUILD_VERSION >> build_version.txt

and load the file value 并加载文件值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM