Cra*_*ger 5 jenkins jenkins-pipeline jenkins-blueocean
给定一个运行一系列步骤的 Jenkins 管道,一些在parallel块内,有没有办法在管道内获取给定步骤或最近步骤的 Flow id?
什么是流 ID?如果您查看流水线作业的运行,您可以看到指向flowGraphTable/. 那里有指向特定工作步骤的链接,例如execution/node/113/. 这些似乎代表了一个FlowNode.
有没有办法从管道中获取这些 ID,用于生成链接等?
特别是我想获得一个链接到我的并行分支的子流,以便我可以链接到它们的 BlueOcean 视图。(内置的 Jenkins 视图没有用,因为它没有显示子树)。
我可以看到 BlueOcean 链接对应于 /execution/ 链接,它们具有相同的 id 值。如果我的管道分支myjob/9/execution/node/78/在 blueocean 上,它将是jobname/9/pipeline/78.
但是,如果我想使用构建摘要插件或类似插件来生成链接并将它们添加到构建结果页面,我该如何获取该 ID?
特别是,我想获得并行分支的子流程的链接,以便我可以链接到它们的 BlueOcean 视图。
您可以使用 获取当前线程(也称为分支)的头流节点(最近的步骤)。然后,您可以通过检查块起始节点是否包含 的实例来查找父分支。CpsThread.current().head.get()FlowNode.iterateEnclosingBlocks()ThreadNameAction
完整的管道示例:
import org.jenkinsci.plugins.workflow.cps.CpsThread
import org.jenkinsci.plugins.workflow.graph.FlowNode
import org.jenkinsci.plugins.workflow.actions.LabelAction
import org.jenkinsci.plugins.workflow.actions.ThreadNameAction
pipeline {
agent any
stages {
stage("parallel start"){
parallel {
stage("A"){
steps{
showCurrentBranchUrls()
echo "Another step"
}
}
stage("B"){
steps{
echo "Some stuff"
showCurrentBranchUrls()
echo "More stuff"
}
}
}
}
}
}
void showCurrentBranchUrls() {
// Get the most recent FlowNode of current branch
FlowNode headNode = CpsThread.current().head.get()
// Find the nearest parent branch
FlowNode branchNode = getFlowNodeOfParentBranch( headNode )
if( branchNode ) {
// Print some useful URLs based on branchNode
echo "Blue Ocean branch view: ${JENKINS_URL}blue/organizations/jenkins/${JOB_NAME}/detail/${JOB_BASE_NAME}/${BUILD_ID}/pipeline/${branchNode.id}"
echo "Blue Ocean branch log: ${JENKINS_URL}blue/rest/organizations/jenkins/pipelines/${JOB_NAME}/runs/${BUILD_ID}/nodes/${branchNode.id}/log"
echo "Pipeline steps: ${JENKINS_URL}${branchNode.url}"
}
}
// Get FlowNode of parent branch
@NonCPS
FlowNode getFlowNodeOfParentBranch( FlowNode node ) {
node.iterateEnclosingBlocks().find{ enclosing ->
enclosing != null &&
enclosing.getAction( LabelAction.class ) != null &&
enclosing.getAction( ThreadNameAction.class ) != null
}
}
Run Code Online (Sandbox Code Playgroud)
在沙盒管道脚本中,代码可能会触发一些安全错误,因此我建议将其放入共享库中没有此类限制的
如果您想查找另一个分支而不是当前分支的节点 ID,该类PipelineNodeGraphVisitor会派上用场。SO 上已经有很多例子了,例如这个我的
为了进一步阅读,这里有关于使用Jenkins Flow Graph 的很好的概述。
| 归档时间: |
|
| 查看次数: |
1287 次 |
| 最近记录: |