问题描述
我试图在Jenkins中使用groovy post build插件创建一个多行正则表达式。我可以在正常的Jenkins脚本控制台中完成这项工作,但我无法将其转换为后期构建插件。
I'm trying to create a multi-line regex using the groovy post build plugin in Jenkins. I can make this work in the normal Jenkins script console, but I'm having trouble translating that to the post build plugin.
以下是我想要从中获取的文本控制台日志:
Here is the text I want to grab from the console log:
def string """
TEST SUMMARY:
[java] ------------------------------------------------------------
[java] 268 tests in 69 groups
[java] 1 errors
[java] 0 failures
"""
这行代码将与我在脚本控制台中的以上内容匹配:
This line of code will match what I have above in the script console:
def match = string =~ /(?ms)(TEST SUMMARY.*?failures)/
我已经使用post build插件尝试了几件事情,包括以下内容:
I've tried several things with the post build plugin including the following:
manager.logContains((?ms)(".*TEST SUMMARY:.*?failures"))
和
def log = manager.build.logFile
def summary = log =~ /(?ms)(TEST SUMMARY.*?failures)/
和
def log = manager.build.logFile.text
def summary = log =~ /(?ms)(TEST SUMMARY.*?failures)/
推荐答案
事实证明,这个问题是一个错误,导致正则表达式返回0个匹配项。作为参考,如果其他人需要这样做,正确的语法是:
It turns out that the issue was a typo which was causing the regex to return 0 matches. For reference, in the event that someone else needs to do this the correct syntax is:
def log = manager.build.logFile.text
def summary = log =~ /(?ms)(TEST SUMMARY.*?failures)/
从这里你可以提取匹配,或者在我的情况下进一步解析匹配:
From there you can extract the matches or as in my case further parse the match:
def total = summary[0] =~ /\d+ tests/
这篇关于Jenkins Groovy在控制台日志上构建插件多行正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!