Groovy Jenkins:评估 BUILD_LOG_REGEX 并在发送电子邮件前检查是否为空

Groovy Jenkins: Evaluate BUILD_LOG_REGEX and check if empty before sending email

我的 JenkinsFile 中有一个 finally 子句,类似于

finally{
 def script_regex = '${BUILD_LOG_REGEX, regex="^The module", maxMatches=5, showTruncatedLines=false, escapeHtml=true}'
 if (script_regex){
        emailext to: "y@z.com",
                 subject: "some subj",
                 body: script_regex
    }
}

我尝试使用 echo(script_regex) 但它给出了

'${BUILD_LOG_REGEX, regex="^The module", maxMatches=5, showTruncatedLines=false, escapeHtml=true}' 

作为字符串而不是计算正则表达式。虽然,它在 exailext 中运行良好。有没有办法检查此正则表达式 returns 是否为空字符串?并且仅在正则表达式 returns 匹配字符串时发送电子邮件。提前致谢!

所以经过一番研究,我发现 BUILD_LOG_REGEX 变量是由 emailext 插件定义的。因此,我们不能在它外面使用。

相反,我们可以使用 manager.logContains('<regex>') 之类的东西,这将检查您的构建日志和 return 一个布尔值,指示是否存在所需的字符串。

该变量实际上是由 https://plugins.jenkins.io/token-macro/ plugin, which is part of several other plugins, including the https://plugins.jenkins.io/build-name-setter/ 插件定义的。