1

In my declarative Jenkins pipeline I have added code which should approve scripts as suggested here:

...
} catch (Exception jobFailed) {
   if (jobFailed.getMessage() == "script not yet approved for use") {
         echo("[WARNING] Changes in delivery job were automatically approved")
         approveDeliveryJob()
         return false
   }
...
@NonCPS
def approveDeliveryJob() {
    toApprove = ScriptApproval.get().getPendingScripts().collect()
    toApprove.each { pending -> ScriptApproval.get().approveScript(pending.getHash())} }
}
...

As suggested here

to solve this put all the code that works with non serializable variables into @NonCPS annotated function

What I am missing?

papanito
  • 2,349
  • 2
  • 32
  • 60

1 Answers1

3
toApprove = ScriptApproval.get().getPendingScripts().collect()

Here you are storing the result in the script binding, which is affected by serialization.

You need a local variable instead:

 def toApprove = ScriptApproval.get().getPendingScripts().collect()
zett42
  • 25,437
  • 3
  • 35
  • 72