微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
}

但这是空的……

有任何想法吗?

解决方法

您接近第一个解决方案.

实际上,你需要做的是根据它的类型迭代这个原因的祖先.

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

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)
}

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

希望能帮助到你,

最好

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐