【问题标题】:write groovy email template to display parsed build errors in email body编写 groovy 电子邮件模板以在电子邮件正文中显示已解析的构建错误
【发布时间】:2015-04-01 14:49:25
【问题描述】:

我在 Jenkins 中使用已解析的控制台日志插件和 Email-ext 插件来发送每日构建状态,仅在构建失败或编译器警告时发送。我想在电子邮件正文中显示提取的错误/警告消息。我从“https://github.com/jenkinsci/email-ext-plugin/blob/master/src/main/resources/hudson/plugins/emailext/templates/groovy-html.template”获得了 groovy 电子邮件模板。它显示控制台输出而不是特定的错误/警告消息。 我对 groovy 或 html 等的知识为零,我需要一些时间来学习并能够修改模板以快速满足我的需求。

谁能给我一个示例文件,它可以搜索控制台输出或解析的控制台输出,并且只显示包含“错误”或“警告”的行?

非常感谢任何帮助。

【问题讨论】:

  • 我也想做同样的事情。你有没有想出一个 groovy 脚本来读取构建输出日志并显示提取的警告/错误?

标签: groovy jenkins


【解决方案1】:

不幸的是,这里的大部分 HTML 都很难兼容 Outlook 有限的 HTML 支持。

<%

def paddingForDepth(int depth)
{
    return "padding-left:${depth * 30}px";
}

def insertErrorPaneRow(int depth, Closure contents)
{
    %>
        <tr class="${tableLineClass()}">
            <td class="icon_cell"></td>
            <td class="console_cell"></td>
            <td class="phase_name_cell" style="${paddingForDepth(depth)}">
                <table width="100%" class="errorsPane">
    <%
    contents()
    %>
                </table>
            </td>
        </tr>
    <%
}

def insertConsoleSummary(def build, int depth)
{
    if (build.result != hudson.model.Result.FAILURE)
        return;

    final BeforeSummary = 0
    final SummaryStarted = 1
    final SummaryEnded = 2

    BufferedReader logReader = new BufferedReader(build.getLogReader());
    List<String> errorLines = new LinkedList<String>();
    List<String> errorSummary = new LinkedList<String>();
    Boolean msBuildDetected = false;
    int scanStage = BeforeSummary;

    try
    {
        for (String line = logReader.readLine(); line != null; line = logReader.readLine())
        {
            if (line.contains(' error ') || line.contains(' warning '))
                errorLines.add(line);

            if (line.contains('Microsoft (R) Build Engine version '))
                msBuildDetected = true;

            if (msBuildDetected)
            {
                switch (scanStage)
                {
                case BeforeSummary:
                    if (line.equals('Build FAILED.') || line.equals('Build succeeded.'))
                        scanStage = SummaryStarted;
                    if (line.equals('Attempting to cancel the build...'))
                    {
                        scanStage = SummaryEnded;
                        msBuildDetected = false;
                    }
                    break;

                case SummaryStarted:
                    if (line ==~ /^\s*\d+ Warning\(s\)/)
                        scanStage = SummaryEnded;
                    else
                        errorSummary.add(line);
                    break;
                }
            }
        }
    }
    finally
    {
        logReader.close();
    }

    if ((msBuildDetected && (errorSummary.size() > 0)) || (errorLines.size() > 0))
    {
        insertErrorPaneRow(depth) {
            %><tr><td><pre><%
                if (msBuildDetected)
                    errorSummary.each { l -> println l }
                else
                    errorLines.each { l -> println l }
            %></pre></td></tr><%
        }
    }
}
%>

<STYLE>
.icon_cell { padding: 3px; padding-left: 5px; padding-right: 5px; height:16px; vertical-align:middle; }
.console_cell { padding: 3px; padding-left: 5px; padding-right: 15px; height:16px; vertical-align:middle; }
.phase_name_cell { height:16px; vertical-align:middle; }
.errorsPane { background-color:#ffe0e0; }

</STYLE>
<BODY>
<!-- CONSOLE OUTPUT -->
<TABLE width="100%">
<TR><TD class="bg1"><B>BUILD SUMMARY</B></TD></TR>
</TABLE>
<BR/>

<table border="0" class="phasesTable"><tbody>
<%
insertConsoleSummary(build, 0); 
%>
</tbody></table>
<BR/>

</BODY>

【讨论】:

    猜你喜欢
    • 2010-09-21
    • 2018-12-01
    • 1970-01-01
    • 2021-04-23
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    相关资源
    最近更新 更多