2015-04-01 78 views
2

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

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

任何帮助,非常感谢。

+0

我期待着做同样的事情。你有没有想出一个groovy脚本来读取构建输出日志并显示提取的警告/错误? – 2016-01-25 13:59:59

回答

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>