12

我正在使用两个 Jenkins 插件,即 Email-ExtLog Parser。我有我想要的 Log Parser 插件的正则表达式,我想在构建后发送给用户的电子邮件中包含 Log Parser 插件的输出。

Email-Ext 插件可以访问控制台输出,我可以为电子邮件中的控制台输出重写我的正则表达式,但是由于 Log Parser 插件已经完成了艰苦的工作,我希望有一些方法可以拉其输出到电子邮件中。

有谁知道可以做到这一点的任何方式(如 Jenkins 环境变量)?

4

2 回答 2

1

一位同事告诉我,Jenkins 中的每个构建都有与之相关的“动作”,并且 Jenkins 插件通过动作发挥作用。我能够找到我的所有动作build.getActions()。然后我循环执行这些操作,直到我得到LogParserActionJenkins Log Parser 插件提供的操作。

然后我查看了源代码LogParserAction.class以找到方法getErrorLinksFile()。使用这种方法,我能够获取已解析日志的文本。一种类似的方法getWarningLinksFile()可用于警告,另一种可用于信息。

然后我遍历字符上的文本\n并应用一些正则表达式使其看起来像我想要的那样。代码的重要部分如下。如果您在 Notepad++ 中将其视为 HTML,看起来会更好

%>
    <TABLE width="100%">
        <TR>
            <TD class="bg1" colspan="2">ERRORS</TD>
        </TR>
<%
    def publisher = null
    for(iter in project.getPublishersList()){
        if(iter.getDescriptor().getDisplayName().equals("Editable Email Notification")){
            publisher = iter
            break
        }
    }
    if(publisher != null){
        def logParserResult
        //Get the LogParserAction from Jenkins
        for(action in build.getActions()){
            if(action.toString().contains("LogParserAction")){
                //Get the LogParserResult from the LogParserAction
                logParserResult = action.getResult()
                break
            }
        }

        //Get the ErrorLinksFile from the LogParserResult
        def errorLinksFile = new File(logParserResult.getErrorLinksFile())

        //Rewrite the URL so it directs to something useful
        pattern = ~/<a.*?><font.*?>/
        def errorList = []
        for(line in errorLinksFile.getText().split("\n")){
            //All errors have a link, so this makes sure no superfluous text is displayed
            if(!line.contains("href")){
                continue
            }
            errorList.add(line.replaceAll(pattern, "<a href="+ rooturl + build.url + "parsed_console/?\">").minus("</font>"))
        }
%>
        <TR>
            <TD class="bg2" colspan="2">Total : ${errorList.count{it}} error(s)</TD>
        </TR>
<%
        for(error in errorList){
%>
        <TR>
            <TD class="errors" colspan="2">${error}</TD>
        </TR>
<%
        }
%>
    </TABLE>
于 2012-01-05T02:08:14.500 回答
0

如果您能够提取日志并写入文件。您可以使用电子邮件分机将该文件作为附件附加到您的电子邮件中。

于 2012-01-04T21:42:48.870 回答