11

I'm trying to get the matched string from a regex in groovy. The matched string prints to the console without problems, but when I try use the matched string in a git command I get the following error:

Err: Incremental Build failed with Error: java.io.NotSerializableException: java.util.regex.Matcher

Here is the code:

                def binaryName = "298_application_V2_00_Build_07.hex"

                def matches = (binaryName =~ /(V)(\d+)(_)(\d+)(_)(Build)(_)(\d+)/)
                versionTag = ""+matches[0].getAt(0)                 
                echo "${matches}"
                echo "$versionTag"
                bat("git tag $versionTag")
                bat("git push origin --tags")

How can I get the matched string from the regex?

2 Answers 2

17

This problem is caused by Jenkins' CPS, which serializes all pipeline executions to store as resumable state.

Calls to non-serializable methods have to be wrapped in a method annotated with @NonCPS:

@NonCPS
String getVersion(String binaryName) {
  def matches = (binaryName =~ /(V)(\d+)(_)(\d+)(_)(Build)(_)(\d+)/)
  versionTag = ""+matches[0].getAt(0)
  versionTag
}

this method can now be called from your pipeline. In case your Jenkins master restarts during execution of this method, it would just run through it completely - which is in many cases, such as yours, absolutely no problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, I've been struggling with this for some hours now. How is one supposed to know about this :\
2

The NonCPS annotation did not have any effect in my declarative pipeline library. A workaround is to avoid ever returning a matcher as shown in this example:

version = ("release/1.0.0" =~ /(?:release|hotfix)\/(.*)/)[0][1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.