4

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

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
}

但这是空的......

有任何想法吗?

4

1 回答 1

9

你很接近你的第一个解决方案。

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

希望能帮助到你,

最好的

于 2013-02-06T11:51:37.763 回答