NotSerializableException in jenkinsfile

11,767

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 :

Share:
11,767

Related videos on Youtube

Jaime Alcántara Arnela
Author by

Jaime Alcántara Arnela

I've been learning programming since 16. I started with python back then. I entered in the computer engineering degree in 2015-2016 and since then I've learnt deeper concepts, ways of work and new technologies (we started with c++ and then java, also learning about database with SQL). In 2016 I learnt swift and started developing an app with Oscar Caballero (he did it for android and I did it for iOS).

Updated on June 04, 2022

Comments

  • Jaime Alcántara Arnela
    Jaime Alcántara Arnela almost 2 years

    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?

  • Jaime Alcántara Arnela
    Jaime Alcántara Arnela almost 6 years
    Sorry to take so long to comment but the expresion is now always returning false with the ==~ (when the artifact name is "artifact.x.y.z-TEST.jar" and when it is "artifact.x.y.z.jar"), so the has_snapshot is always false. Am I skiping something?
  • mkobit
    mkobit almost 6 years
    @JaimeAlcántaraArnela make sure you look at the full documentation of the match operator. It "requires a strict match of the input string". So, if you change your pattern from /-TEST\.jar/ to something like /.*-TEST\.jar/ you may/may not get the expected result. I would recommend trying it out in a Groovy Shell to get a feel for you it works.
  • Lee Meador
    Lee Meador about 4 years
    I had a similar error when I was iteration over a map with "for (def entry in myMap)" and calling another Jenkins build job. The other job would run but this job would fail with this error immediately, before the other job completed. I thought the wait option was being ignored.