Im working in a pipeline with groovy and I found this problem with find operator where all solutions didn't work.
after this code:
stage('Get build artifacts') {
def buildNumber = (params.BUILD_VERSION =~ /^.*-b(\d*)-(.*$)/)
I do a wget to download artifacts, but appears this error
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@325d4820
in field com.cloudbees.groovy.cps.impl.CpsClosureDef.capture
in object com.cloudbees.groovy.cps.impl.CpsClosureDef@31fe6d7d
in field com.cloudbees.groovy.cps.impl.CpsClosure.def
in object org.jenkinsci.plugins.workflow.cps.CpsClosure2@68074406
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@4842ad69
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@4842ad69
Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: a9fb8438ab8f-4ab2-9ee7-83b8f48ed2d3
Caused: java.io.NotSerializableException: java.util.regex.Matcher
I need to use a second part to a build url with that parts, so I will have 3 values in this var.
code example will be:
wget https://mywebsite/''' + buildNumber[0][2] + '''/'''+ buildNumber[0][1] + '''/pkg1-''' + params.BUILD_VERSION + '''.tar.gz
wget https://mywebsite/''' + buildNumber[0][2] + '''/'''+ buildNumber[0][1] + '''/pkg2-''' + params.BUILD_VERSION + '''.tar.gz
My build number needs to be like 1.0.0-b1-master but fails when downlaods second package and I can't find a solution. Most curios is because this code is working in one jenkins and creating second now fails. Idees?
Thanks!
PD: Edited with more context after first answer received
Basically what the error says - Matcher
is not serializable. Why is it a problem? Here is the short answer from Jenkins documentation:
Local variables are captured as part of the pipeline’s state during serialization. This means that storing non-serializable objects in variables during pipeline execution will result in a NotSerializableException to be thrown.
If you want to know more you can google what CPS is, but that rabbit hole is pretty deep. In your case this is probably the quickest fix:
def buildNumber = (params.BUILD_VERSION =~ /^.*-b(\d*)-(.*$)/)[0][1]
Update
If you need all matches and groups you can convert Matcher
to a List
:
def buildNumber = (params.BUILD_VERSION =~ /^.*-b(\d*)-(.*$)/).collect()
echo buildNumber[0][1]
echo buildNumber[0][2]