如何在 JenkinsFile 中使用 BUILD_LOG_REGEX 格式化匹配行

问题描述 投票:0回答:1

我有一个 JenkinsFile,我在其中使用 emailext 发送邮件以获取自动化结果:

def subject = "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
def color = currentBuild.currentResult == 'FAILURE' ? 'red' : 'green'

    emailext (
    mimeType: 'text/html',
    attachLog: true,
    subject: subject,
    body: 
    """
    <h3><font color='${color}'>${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} </h3></font><br> More info at: ${env.BUILD_URL}<br><br>
    Total test case:  \${TEST_COUNTS,var="total"} <br>
    Pass:               \${TEST_COUNTS,var="pass"}  <br>
    Fail:               \${TEST_COUNTS,var="fail"}  <br>
    <br>
    ${htmlTable}
    <br>
    \${BUILD_LOG_REGEX, regex="^.*Check out job at", showTruncatedLines="false",substText=" Access the test recording here:"}<br><br>
    <p>Click the link below to view the report:</p><a href="${env.BUILD_URL}/QmateReport">View Report</a>
    """,
    to: '[email protected]'
)       

对于这条线

\${BUILD_LOG_REGEX, regex="^.*Check out job at", showTruncatedLines="false",substText=" Access the test recording here:"}<br><br>

我收到了多个匹配项,并且全部以未格式化的方式排列在一行中。

有没有办法在 NextLine 中获取电子邮件中那些匹配的记录

这是我们在电子邮件中收到的结果。

我想在 NextLine 中看到这个

在此处访问录音:
在这里访问录音:

我尝试使用以下两点

  1. 在 substText 中使用 /n
\${BUILD_LOG_REGEX, regex="^.*Check out job at", showTruncatedLines="false",substText=" Access the test recording here: \n"}<br><be> 
  1. 使用实际的换行符:而不是 ,尝试在字符串中使用实际的换行符。您可以通过在冒号后按 Enter 并在下一行继续该字符串来实现此目的。例如:
\${BUILD_LOG_REGEX, regex="^.*Check out job at", showTruncatedLines="false",substText=" Access the test recording here: 
"}

但没有任何效果。

jenkins-pipeline jenkins-plugins jenkins-groovy email-ext jenkins-email-ext
1个回答
2
投票

序言

自 token-macro-plugin

2.0-beta
版本中引入以来,
BUILD_LOG_REGEX
宏始终支持默认值为
addNewline
true 属性。默认情况下,它应该在每个匹配的末尾添加一个新行字符。

如果提供的解决方案不起作用,我会要求以下内容:

  1. 完整、经过清理的 Jenkins 文件
  2. 完整、经过清理的构建日志
  3. 您的 Jenkins 版本、Email Ext 插件版本和令牌宏插件版本
  4. 生成的电子邮件中经过净化的原始 HTML

解决方案列表

我建议尝试两种不同的解决方案。

  1. addNewline="true"
    属性显式添加到 BUILD_LOG_REGEX 宏
  2. 添加
    addNewline="true"
    matchedLineHtmlStyle
    属性(
    matchedLineHtmlStyle
    允许您在表达式匹配上提供内联 CSS 样式)

如果这两个解决方案不起作用,您可以直接在将

Check out job at
打印到构建日志的代码后面添加 println(),然后将
linesAfter="1"
作为属性添加到 BUILD_LOG_REGEX 宏。但是,我无法让它发挥作用。

此外,我很幸运地将 BUILD_LOG_REGEX 包装在

<pre>
中,如下
<pre>\${BUILD_LOG_REGEX,....}</pre>
。您可能还想对此进行修改。但是,使用解决方案 2 中的两个属性,我可以生成符合您要求的格式良好的电子邮件。

解决方案 2 的实施示例

由于解决方案#2 是解决方案#1 的超集,因此我不提供解决方案#1 的示例代码。这是您的代码,其中包含这些建议的更改。

emailext (
    mimeType: 'text/html',
    attachLog: true,
    subject: subject,
    body: 
    """
    <h3><font color='${color}'>${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} </font></h3><br> More info at: ${env.BUILD_URL}<br><br>
    Total test case:  \${TEST_COUNTS,var="total"} <br>
    Pass:               \${TEST_COUNTS,var="pass"}  <br>
    Fail:               \${TEST_COUNTS,var="fail"}  <br>
    <br>
    ${htmlTable}
    <br>
    \${BUILD_LOG_REGEX, regex="^.*Check out job at", showTruncatedLines="false", matchedLineHtmlStyle="margin-bottom: 1em; display: inline-block;", addNewline="true", substText=" Access the test recording here:"}<br><br>
    <p>Click the link below to view the report:</p><a href="${env.BUILD_URL}/QmateReport">View Report</a>
    """,
    to: '[email protected]'
)       

这是我为模拟您的“环境”而编写的大型管道。值得注意的是,除了设置

addNewline=“false”
之外,我无法复制您的问题,因此在这里我明确将其设置为 true。此外,我破坏了你的一些令牌,因为它们不存在于我的系统中。

pipeline {
    
    agent any

    stages {
        stage('release app0') {
            steps {
                script {
                    def output = sh(script: 'echo Check out job at random1 enADFASDFASDFGASd  ASDFGASDGASGASDFGASDFG ASDFASDFAFA ASDFASDFA ASDFASDF ASDFASDFASDF ASDFASDFAS ', returnStdout: true).trim()
                    echo output
                }
            }
        }
        stage('release app1') {
            steps {
                script {
                    def output = sh(script: 'echo Check out job at random2 end KEJHAIDFA 349MQRG90U 1T9 QUER9GQ34TU 9034UT02U 34T', returnStdout: true).trim()
                    echo output
                }
            }
        }
        stage('release app2') {
            steps {
                script {
                    def output = sh(script: 'echo Check out job at random3 end  0U4 U13TU 1349-TU -134UTU 134TU134T-134TU-1-34TU-134 UT-1', returnStdout: true).trim()
                    echo output
                }
            }
        }
        stage('release app3') {
            steps {
                script {
                    def output = sh(script: 'echo Check out job at random4 end  903U90 349 U340U 909 39034 T034U030403040 34 4', returnStdout: true).trim()
                    echo output
                }
            }
        }
        stage('release app4') {
            steps {
                script {
                    def output = sh(script: 'echo Check out job at random5 end  490U39434T0 - V 4U-34U- 34 34-U-', returnStdout: true).trim()
                    echo output
                }
            }
        }
    }
    
    post { 
        success {
            emailext (
                mimeType: 'text/html',
                subject: "asdf",
                body: 
                """
                <h3><font color='#ff0000'>${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}</font></h3> <br> More info at: ${env.BUILD_URL}<br><br>
                Total test case:  {TEST_COUNTS,var="total"} <br>
                Pass:               {TEST_COUNTS,var="pass"}  <br>
                Fail:               {TEST_COUNTS,var="fail"}  <br>
                <br>
        
                <br>
                \${BUILD_LOG_REGEX, regex="^.*Check out job at", showTruncatedLines="false", matchedLineHtmlStyle="margin-bottom: 1em; display: inline-block;", addNewline="true", substText=" Access the test recording here:"}<br><br>
                <p>Click the link below to view the report:</p><a href="${env.BUILD_URL}/QmateReport">View Report</a>
                """,
                to: '[email protected]'
            )       
 
        }
    }
    
}

结果电子邮件

生成的电子邮件对我来说如下所示:

您看到行重复的原因是由于我的测试脚本(它获取 echo 命令和 stdout )。假设字符串是由不包含字符串本身的命令的输出生成的,您将不会这样做。

© www.soinside.com 2019 - 2024. All rights reserved.