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

Mat*_*ias 5 continuous-integration jenkins jenkins-pipeline

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

即:

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

        parallel projectBranches
Run Code Online (Sandbox Code Playgroud)

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

Gui*_*ume 6

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

  • 以编程方式获取日志

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

      [Pipeline] 管道开始
      [Pipeline] parallel
      [Pipeline] { (Branch: branch1)
      [Pipeline] { (Branch: branch2)
      [Pipeline] }
      [Pipeline] echo
      [branch1] in branch1
      [Pipeline] sleep
      [branch1] Sleeping for 1 sec
      [Pipeline] echo
      [branch2] in branch2
      [Pipeline] sleep
      [branch2]休眠 1 sec

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

      [branch2]在 branch2
      [branch2]休眠 1 秒

  • 归档日志(在 $JOB_URL/<runId>/artifacts 中)以将它们作为链接供以后使用

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

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

  • 我用过这个工具,真的太神奇了!但是,您自己的 API 中有一个拼写错误:您的过滤器函数接收如下分支:`String logsBranch2 = logparser.getLogsWithBranchInfo(filter: ['branch2'])` 而不是这样:`String logsBranch2 = logparser.getLogsWithBranchInfo(filter : 'branch2')` 这让我痛苦了几个小时哈哈 (3认同)

hak*_*iri 3

您可以使用 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/"}
Run Code Online (Sandbox Code Playgroud)

因此,对于您的情况,您对分支的 StepAtomNode 类型和给定名称(本例为 0-9)的子项感兴趣。由此,您只需在地址中添加日志即可获取控制台输出地址(例如:job/test/1/execution/node/37/log)。

现在这有点难看,你需要解析 html 以从

<pre class="console-output">log here
</pre>
Run Code Online (Sandbox Code Playgroud)