[declarative pipeline] Variable persistance after Jenkins restart

45 views
Skip to first unread message

Bill Dennis

unread,
Mar 27, 2017, 8:07:49 PM3/27/17
to Jenkins Users
Hi -

I have an issue with variables being restored in a pipeline job after a Jenkins restart.

Say I have a pipeline like this:

// Local var set to 'env'
def localEnv = env

// Simple local var
def foo = "Bar"

pipeline
{
    agent any
   
    environment
{
       
       
// You can do it here but I really want the whole env in my own var
        myBuildNumber
= "${env.BUILD_NUMBER}"
   
}
   
    stages
{
       
        stage
('Init') {
           
            steps
{
                echo
"${localEnv.BUILD_NUMBER}"    
           
}
       
}
       
        stage
('Hello') {
            steps
{
               
                echo
"hello"
               
                input
'Waiting for input...'
               
               
// Lets see what we have
                echo
"localEnv.BUILD_NUMBER = ${localEnv.BUILD_NUMBER}"
                echo
"env.BUILD_NUMBER = ${env.BUILD_NUMBER}"
                echo
"foo = ${foo}"
                echo
"myBuildNumber = ${myBuildNumber}"
           
}
       
}
   
}
}

I am making my own local variable as copy of the 'env' variable (localEnv). I do this a lot in my jobs because I call custom library DSL-type structure similar to this:

myCustomThing {
   reference
= localEnv.BUILD_NUMBER
   message
= "Something about build ${localEnv.BUILD_NUMBER}"
}

If I use 'env' there, I get a null pointer exception on this closure - something to do with groovy Closure delegate (that is another question I guess), so I did this 'def localEnv = env' thing to get around that. Anyway, for the normal case my job above outputs this after selecting approve on the input and all is well:

Waiting for input...

Proceed or Abort

Approved by Bill Dennis

[Pipeline] echo

localEnv.BUILD_NUMBER = 21

[Pipeline] echo

env.BUILD_NUMBER = 21

[Pipeline] echo

foo = Bar

[Pipeline] echo

myBuildNumber = 21


When I restart Jenkins while the input is waiting then approve the input the output is like this (note the null):

Waiting for input...

Proceed or Abort

Resuming build at Mon Mar 27 23:48:21 UTC 2017 after Jenkins restart

Ready to run at Mon Mar 27 23:48:32 UTC 2017

Approved by Bill Dennis

[Pipeline] echo

localEnv.BUILD_NUMBER = null

[Pipeline] echo

env.BUILD_NUMBER = 22

[Pipeline] echo

foo = Bar


As can be seen, 'localEnv.BUILD_NUMBER' comes back as null after the Jenkins restart. So it looks like I need to do a deep-copy of the 'env' or set up many more variables in my environment section for everything that needs to be available from 'env' surviving a restart, so that things I need cans be passed to any of my shared library stuff. 


Does anyone have any deeper understanding of this?


Thanks,

--Bill





Reply all
Reply to author
Forward
0 new messages