Jenkins groovy regex match string : Error: java.io.NotSerializableException: java.util.regex.Matcher

烈酒焚心 提交于 2019-12-10 18:55:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/47646409/jenkins-groovy-regex-match-string-error-java-io-notserializableexception-jav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!