`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.
More on Jenkinsfile syntax, Groovy & CPS errors
`WorkflowScript: 1: Missing required section "agent" @ line 1, column 1.`Beginner`Unknown stage section "step". Starting with version 0.5, steps in a stage must be in a ‘steps’ block.`Beginner`java.lang.NoSuchMethodError: No such DSL method 'withSonarQubeEnv' found among steps [archive, bat, build, ...]`Beginner`java.io.NotSerializableException: groovy.json.internal.LazyMap`Intermediate`org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object`Intermediate`groovy.lang.MissingPropertyException: No such property: IMAGE_TAG for class: groovy.lang.Binding`Beginner