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

22 views
Skip to first unread message

niristotle okram

unread,
Aug 28, 2017, 3:23:47 PM8/28/17
to jenkins...@googlegroups.com

how do i access the value of a variable assigned inside a closure within a try() in the catch() statement. here is what i am trying to achieve but i keep getting the initialization value that i assigned.

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.

Thank you...



Slide

unread,
Aug 28, 2017, 3:32:50 PM8/28/17
to jenkins...@googlegroups.com
You don't need to initialize to null outside the try, just "def val_log" is fine. I've used this in my pipeline and it works fine. Can you verify that your pipeline is getting to the point where val_log is assigned, and also verify it's not assigned anywhere else?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPzcO4iK8F4vJ_D-0n2Gpnxph99Km9RCFQ52jtj9OqKY-E7K-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

niristotle okram

unread,
Aug 28, 2017, 4:00:39 PM8/28/17
to jenkins...@googlegroups.com
thanks, that seems to make sense with what i am noticing. Seems like the root cause is that, the variable is not assigned if the 'shell' step fails / non 0 exit status. 
i am trying to assign the variable with the shell scripts stdout. I would like to capture the stacktract so that it prints the shell scripts stdout/stderr in the mail, rather than the generic "'hudson.AbortException: script returned exit code 1'"


On Mon, Aug 28, 2017 at 2:32 PM, Slide <slide...@gmail.com> wrote:
You don't need to initialize to null outside the try, just "def val_log" is fine. I've used this in my pipeline and it works fine. Can you verify that your pipeline is getting to the point where val_log is assigned, and also verify it's not assigned anywhere else?

On Mon, Aug 28, 2017 at 12:23 PM niristotle okram <nirish...@gmail.com> wrote:

how do i access the value of a variable assigned inside a closure within a try() in the catch() statement. here is what i am trying to achieve but i keep getting the initialization value that i assigned.

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.

Thank you...



--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAPiUgVcpNOzJnS0GEktkFaONiHHOfFFfdB6LBJZmkBrB14JYzg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Regards
nirish okram
Reply all
Reply to author
Forward
0 new messages