简体   繁体   English

Jenkins Groovy在控制台日志上发布构建插件多行正则表达式

[英]Jenkins Groovy post build plugin multi line regex on console log

I'm trying to create a multi-line regex using the groovy post build plugin in Jenkins. 我正在尝试使用Jenkins中的groovy post build插件创建多行正则表达式。 I can make this work in the normal Jenkins script console, but I'm having trouble translating that to the post build plugin. 我可以在普通的Jenkins脚本控制台中完成这项工作,但是在将其转换为post build插件时遇到了麻烦。

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)/

I've tried several things with the post build plugin including the following: 我已经尝试使用post build插件进行以下操作:

manager.logContains((?ms)(".*TEST SUMMARY:.*?failures"))

and

def log = manager.build.logFile
def summary = log =~ /(?ms)(TEST SUMMARY.*?failures)/

and

def log = manager.build.logFile.text
def summary = log =~ /(?ms)(TEST SUMMARY.*?failures)/

It turns out that the issue was a typo which was causing the regex to return 0 matches. 事实证明,问题出在拼写错误,导致正则表达式返回0个匹配项。 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/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM