小编典典

获得Jenkins上游职位

jenkins

我想获得所有上游作业,就像在控制台输出中一样:

Started by upstream project "allocate" build number 31
originally caused by: 
Started by upstream project "start" build number 12
originally caused by:

我已经尝试使用以下命令进行groovy postbuild:

def build = Thread.currentThread().executable
def causes= manager.build.getCauses()
for (cause in causes)
{
manager.listener.logger.println "upstream build: " + cause.getShortDescription()

}

但是我只能得到“分配”,而不是“开始”工作。

我也尝试过

def build = Thread.currentThread().executable
def test = build.getUpstreamBuilds()
for (up in test)
{
manager.listener.logger.println "test build project: " + up
}

但这是空的…

有任何想法吗?


阅读 337

收藏
2020-07-25

共1个答案

小编典典

您已接近第一个解决方案。

实际上,您需要做的就是Cause根据其类型对它的祖先进行迭代。

这是一个示例代码片段,可以帮助您入门:

def printCausesRecursively(cause) {
     if (cause.class.toString().contains("UpstreamCause")) {
         println "This job was caused by " + cause.toString()
         for (upCause in cause.upstreamCauses) {
             printCausesRecursively(upCause)
         }
     } else {
         println "Root cause : " + cause.toString()
     }
}

for (cause in manager.build.causes)
{
    printCausesRecursively(cause)
}

您可能需要参考文档来处理所有Cause类型:http : //javadoc.jenkins-
ci.org/hudson/model/Cause.html

希望能帮助到你,

最好

2020-07-25