Jenkins Pipeline - 如何从并行构建中获取日志

时间:2016-07-11 10:15:15

标签: jenkins continuous-integration jenkins-pipeline

是否可以,如果是,如何?,分别获取每个并行步骤的日志输出?

即:

def projectBranches = [:]
        for (int i = 0; i < projects.size(); i++) {
            def _i = i
            projectBranches[_i] = {
                someFunction(_i)
            }
        }

        parallel projectBranches

现在是否可以获取每个projectBranches [_i]的日志?

3 个答案:

答案 0 :(得分:3)

我需要从管道代码中访问日志
因此我实施了proposed(非常有帮助)提出的算法,并做了一些调整(在每行上添加branchName前缀,以便能够获取整个日志,并仍然找出对应于每行的分支;并支持嵌套分支,我需要)在https://github.com/gdemengin/pipeline-logparser中:

  • 以编程方式获取日志

    • 以获取带有分支前缀的完整日志(类似于currentBuild.rawBuild.log在工作流作业插件版本2.2.5之前返回的内容。但是在版本2.26中,它们摆脱了分支信息,并且我找不到具有相同信息的任何内置函数
      String logs = logparser.getLogsWithBranchInfo()

        

      [管道]管道的开始
        [管道]平行
        [管道] {(分支:branch1)
        [管道] {(分支:branch2)
        [管道]}
        [管道]回声
         [branch1] 在branch1中
        [管道]睡觉
         [branch1] 睡眠1秒
        [管道]回声
         [branch2] 在branch2中
        [管道]睡觉
         [branch2] 睡眠1秒

    • 仅从“ branch2”获取日志
      String logsBranch2 = logparser.getLogsWithBranchInfo(filter: 'branch2')

        

      [branch2] 在branch2中
         [branch2] 睡眠1秒

  • 归档日志(以$ JOB_URL / / artifacts格式),以使它们可作为链接供以后使用

    • 存档完整日志(带有分支前缀)
      logparser.archiveLogsWithBranchInfo('consoleText.txt')

    • 仅归档来自branch2的日志
      logparser.archiveLogsWithBranchInfo('logsBranch2.txt', [filter: 'branch2'])

答案 1 :(得分:2)

我找到了一种方法来实现此目的,但是您需要直接访问build文件夹(例如,使用currentBuild.rawBuild.getLogFile().getParent())。

  • 解析flowNodeStore.xml目录中的xml文件(或单个workflow文件):
    • 使用<id><parentIds>值构建节点的层次结构。
    • 如果定义了<branchName>,则将其关联到当前节点,然后递归地关联到将该节点作为父节点的所有节点。如果一个节点有多个父节点,则不为其分配任何分支值。
  • log的格式读取byte[]文件。
  • 读取log-index的每一行以查找分配给每个节点的日志范围。行的格式可以是以下之一:
    • offset nodeId->新节点范围的开始,前一个节点的结束(如果存在)。
    • offset:当前节点范围的结尾。
  • 将字节范围转换回utf8字符串(new String(range, "UTF-8"))。
    • 您可能想用replaceAll("\u001B.*?\u001B\\[0m", "")
    • 剥离所有嵌入式代码

答案 2 :(得分:0)

您可以使用Jenkins REST API获取节点:job / test / 1 / api / json?depth = 2

结果应包含以下内容:

{"_class":"org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode","actions":[{},{},{}],"displayName":"Branch: 0","iconColor":"blue","id":"13","parents":["3"],"running":false,"url":"job/test/1/execution/node/13/"},
{"_class":"org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode","actions":[{},{},{}],"displayName":"Allocate node : Start","iconColor":"blue","id":"23","parents":["13"],"running":false,"url":"job/test/1/execution/node/23/"},
{"_class":"org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode","actions":[{},{}],"displayName":"Allocate node : Body : Start","iconColor":"blue","id":"33","parents":["23"],"running":false,"url":"job/test/1/execution/node/33/"},
{"_class":"org.jenkinsci.plugins.workflow.cps.nodes.StepAtomNode","actions":[{},{}],"displayName":"Print Message","iconColor":"blue","id":"37","parents":["33"],"running":false,"url":"job/test/1/execution/node/37/"}

因此,对于您的情况,您感兴趣的是您的分支的StepAtomNode类型的孩子,其名称为(本案例为0-9)。通过简单地将日志添加到地址,您就可以获得控制台输出地址(例如:job / test / 1 / execution / node / 37 / log)。

现在这是它有点难看的地方,你需要解析html以从

获取实际的日志
<pre class="console-output">log here
</pre>
相关问题