简体   繁体   English

Jenkins groovy 正则表达式匹配字符串:错误:java.io.NotSerializableException:java.util.regex.Matcher

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

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:我正在尝试从 groovy 中的正则表达式中获取匹配的字符串。匹配的字符串可以毫无问题地打印到控制台,但是当我尝试在 git 命令中使用匹配的字符串时,出现以下错误:

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?如何从正则表达式中获取匹配的字符串?

This problem is caused by Jenkins' CPS , which serializes all pipeline executions to store as resumable state. 此问题是由Jenkins的CPS引起的,该CPS将所有管道执行序列化以存储为可恢复状态。

Calls to non-serializable methods have to be wrapped in a method annotated with @NonCPS : 对非序列化方法的调用必须包含在使用@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. 如果您的Jenkins主机在执行此方法期间重新启动,它将完全贯穿它 - 这在很多情况下,例如您的,绝对没有问题。

The NonCPS annotation did not have any effect in my declarative pipeline library. NonCPS 注释对我的声明性管道库没有任何影响。 A workaround is to avoid ever returning a matcher as shown in this example:解决方法是避免返回匹配器,如本例所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NotSerializableException:java.util.regex.Matcher - NotSerializableException: java.util.regex.Matcher java.util.regex.Matcher混淆组 - java.util.regex.Matcher confused group 错误:java.io.NotSerializableException:groovy.util.slurpersupport.Attributes - Error: java.io.NotSerializableException: groovy.util.slurpersupport.Attributes java.lang.StringIndexOutOfBoundsException:来自java.util.regex.Matcher - java.lang.StringIndexOutOfBoundsException: from java.util.regex.Matcher java.util.regex.Matcher::useAnchoringBounds 如何工作? - How does java.util.regex.Matcher::useAnchoringBounds work? java.util.regex.Matcher中的groupCount()始终返回0 - groupCount() in java.util.regex.Matcher always returns 0 为什么 java.util.regex.matcher 不匹配此字符串中的所有实例? - Why isn't java.util.regex.matcher not matching all instances in this string? java.util.regex.Pattern和java.util.regex.Matcher的设计有什么好处? - What is benefit in design of java.util.regex.Pattern and java.util.regex.Matcher? 错误:java.io.NotSerializableException - An error: java.io.NotSerializableException java和android之间java.util.regex.Matcher中usePattern()的不同使此任务变得困难 - the difference of usePattern() in java.util.regex.Matcher between java and android makes this task difficult
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM