Pipeline - The pipeline, even if successful, ends with java.io.NotSerializableException

Article ID:230612967
1 minute readKnowledge base

Issue

A pipeline always ends with a java.io.NotSerializableException, even if the result is the expected one.

Resolution

By design a pipeline can only keep records of Serializable objects. If you still need to keep an intermediate variable with a non serializable object, you need to extract it into a method and annotate this method with @NonCPS. For example, you need to transform

def job = Jenkins.instance.getItemByFullName('test') def sourceBuild = job.getLastBuild().getNumber()

into

@NonCPS def getBuildNumber(String jobName) { def job = Jenkins.instance.getItemByFullName(jobName) return job.getLastBuild().getNumber() } def build = getBuildNumber('test')

References