Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.NotSerializableException: hudson.model.FreeStyleProject

I am trying to execute pipeline script in Jenkins. Here is my script:

import com.cloudbees.groovy.cps.NonCPS
@NonCPS
def getJobVariable(jobName,varName){ 
    job = Jenkins.instance.getItemByFullName(jobName)
        newJob = job.getLastBuild()
    return newJob.getEnvVars().get(varName,null)
}




node{
    stage 'props'

    api = build job: 'Props'
    buildID =  api.getNumber()
    build job: 'Parameterized', parameters: [[$class: 'StringParameterValue', name: 'string_parameter', value: getJobVariable('Props','filename')]];

}

But I am constantly getting NotSerializableException. I guess, something wrong in my getJobVariable method. Please, help

like image 779
Sviatlana Avatar asked May 23 '16 10:05

Sviatlana


People also ask

What is NotSerializableException in Java?

His main interests include distributed systems, storage systems, file systems, and operating systems. In this tutorial we will discuss about NotSerializableException in Java. The exception is thrown when an instance of a class must implement the Serializable interface.

How to handle exception handling in Serializable interface?

The simplest solution is to find the class that throws the exception and make it implement the Serializable interface. However, this may not be feasible if the class that throws the exception belongs to a third-party library.

How to implement the Serializable interface of a pair class?

The SerializableClass implements the Serializable interface, but it refers to the Pair class which doesn’t. The simplest solution is to find the class that throws the exception and make it implement the Serializable interface. However, this may not be feasible if the class that throws the exception belongs to a third-party library.

Which methods are inherited from interface Hudson?

AbstractModelObject Methods inherited from interface hudson.model. Item Methods inherited from interface hudson.model. PersistenceRoot Methods inherited from interface hudson.search. SearchableModelObject Methods inherited from interface hudson.search. SearchItem Methods inherited from interface hudson.security. AccessControlled


1 Answers

I've made some tests, cause I've the same issue in my pipeline jobs (for Matrix).

The solution is to put def before job or the name of Object (here Job) before your declaration.

After the NotSerializableException disappear.

If this does not solve your problem, you can also add @NonCPS flag before your declaration. See the official documentation: serializing-local-variables.

like image 87
Algorys Avatar answered Oct 08 '22 13:10

Algorys