Jenkins Groovy正则表达式匹配字符串:错误:java.io.NotSerializableException:java.util.regex.Matcher [英] Jenkins groovy regex match string : Error: java.io.NotSerializableException: java.util.regex.Matcher

查看:788
本文介绍了Jenkins Groovy正则表达式匹配字符串:错误:java.io.NotSerializableException:java.util.regex.Matcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是代码:

                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?

推荐答案

此问题是由Jenkins的 CPS ,可将所有管道执行序列化以存储为可恢复状态.

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

对不可序列化方法的调用必须包装在以@NonCPS注释的方法中:

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
}

现在可以从您的管道中调用此方法.如果您的Jenkins主服务器在执行此方法期间重新启动,它将完全通过它运行-在很多情况下,例如您的情况,绝对没有问题.

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 Groovy正则表达式匹配字符串:错误:java.io.NotSerializableException:java.util.regex.Matcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆