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

Article ID:230612967
Last Reviewed:2025-07-31()
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')
This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.