Interview Prep/Jenkins/Jenkinsfile syntax, Groovy & CPS errors

`java.io.NotSerializableException: java.util.regex.Matcher`

AdvancedTroubleshootingJenkinsfile syntax, Groovy & CPS errors
Answer

Likely causes → A Matcher from =~ is held in a variable while a step (sh, echo) runs, and CPS tries to serialize it.

Diagnose → The stack trace shows the variable's location in WorkflowScript or the library class.

Fix →

@NonCPS
String extractVersion(String s) {
  def m = (s =~ /version=(\S+)/)
  m ? m[0][1] : null
}

Or use s.find(/version=(\S+)/) { full, v -> v } and don't keep the matcher.

Prevent → Rule in code review: no Matcher, LazyMap or iterators in pipeline variables.

All Jenkins questions Roadmap: Jenkins

More on Jenkinsfile syntax, Groovy & CPS errors