获得Jenkins上游工作

时间:2013-02-06 09:56:57

标签: groovy 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
}

但这是空的......

有什么想法吗?

1 个答案:

答案 0 :(得分:8)

你接近第一个解决方案。

实际上,你需要做的是根据它的类型迭代这个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

希望它有所帮助,

最佳