pipeline {
agent {
node { label 'linux' }
}
stages {
stage('1') {
steps {
script {
def number = tryGetNumber('123')
if (number) {
sh "echo \"it is a number!: ${number}\""
}
}
}
}
}
}
@NonCPS // commenting this line also works!?
def tryGetNumber(text) {
def matcher = text =~ /^\d+$/
if (matcher.matches()) {
return matcher[0]
}
return null
}
This last example code works without @NonCPS annotation
I've tried similar other non-serializable-local-variable cases, but all worked without @NonCPS annotation. Only introducing a method is suffice to fix the issue.
I'm not sure if I misread the documents. When should @NonCPS be used? Is it only for performance improvement? (so that non-necessary local variables not written to disk)