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

Issue

The pipeline always ends with a java.io.NotSerializableException, even if I can have the expected result.

Environment

  • Jenkins 1.580+
  • Pipeline 1.0+

Resolution

By design the 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 hide 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

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.