编写groovy电子邮件模板以显示电子邮件正文中已解析的构建错误

时间:2015-04-01 14:49:25

标签: groovy jenkins

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

有人可以指出一个示例文件,可以搜索控制台输出或解析的控制台输出,只显示包含"错误"或"警告"?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

不幸的是,这里的大部分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>