Search code examples
jenkinsgroovyjenkins-pipeline

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


Solution

  • 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.