Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotSerializableException in jenkinsfile

I'm working on a jenkinsfile and I'm getting and exception in the third stage:

an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@7bbae4fb
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.CaseEnv@6896a2e3
in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@605ccbbc
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@7b8ef914
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@11e73f3c
in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
in object org.jenkinsci.plugins.workflow.cps.CpsThread@b2df9bb
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@2b30596a
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@2b30596a
Caused: java.io.NotSerializableException: java.util.regex.Matcher
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)

I've been reading about it and I know I can't create non-serializable variables. So, I think it has to be with this part of my code:

def artifact_name = sh (
        script: "ls -b *.jar | head -1",
        returnStdout: true
).trim()
def has_snapshot = artifact_name =~ /-TEST\.jar/
if (has_snapshot) {
    //Do something
}

My question is, how do I define that two variables in order to avoid that exception?

like image 209
Jaime Alcántara Arnela Avatar asked Jun 14 '18 11:06

Jaime Alcántara Arnela


People also ask

What is Java IO NotSerializableException?

In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface. The exception is thrown by either the serialization runtime, or by the instance of the class. The argument for the NotSerializableException is the name of the class.

What is NonCPS in Jenkins?

The @NonCPS annotation is useful when you have methods which use objects which aren't serializable. Normally, all objects that you create in your pipeline script must be serializable (the reason for this is that Jenkins must be able to serialize the state of the script so that it can be paused and stored on disk).

What is LazyMap in groovy?

public class LazyMap extends AbstractMap<String,Object> This maps only builds once you ask for a key for the first time. It is designed to not incur the overhead of creating a map unless needed.


1 Answers

Your problem is this line:

def has_snapshot = artifact_name =~ /-TEST\.jar/

The =~ is the Groovy find operator. It returns a java.util.regex.Matcher instance, which is not Serializable. If Jenkins decides to pause your script after you have stored the result in a local variable that is serialized by Jenkins that is when you get the exception. This can be easily tested by immediately adding a sleep(1) step after your invocation and watch as that same exception is thrown.

To resolve this, you should :

  • Not store the java.util.regex.Matcher result in CPS transformed code
  • Move the usage into a @NonCPS annotated method or use the match operator (==~) which returns a boolean (if it fits your use case)
like image 171
mkobit Avatar answered Sep 20 '22 00:09

mkobit