获取 Jenkins 上游工作 [英] Get Jenkins upstream jobs

查看:29
本文介绍了获取 Jenkins 上游工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to get all the upstream jobs, just like in the console output:

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

我已经尝试了以下内容的 groovy postbuild:

I've tried groovy postbuild with the following:

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

}

但后来我只得到分配",而不是开始"工作.

but then I only get "allocate", not the "start" job.

我也试过

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

但这是空的...

有什么想法吗?

推荐答案

您已经完成了第一个解决方案.

You were close with your first solution.

实际上,您需要做的是根据其类型迭代此Cause 的祖先.

Actually, what you need to do is iterate over the ancestor of this Cause depending on it's type.

以下是可以帮助您入门的示例代码片段:

Here is a sample snippet of code that could get you started :

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

You may want to refer to the documentation to handle all Cause types : http://javadoc.jenkins-ci.org/hudson/model/Cause.html

希望能帮到你,

最佳

这篇关于获取 Jenkins 上游工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆