本文介绍了获取Jenkins上游工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我希望获得所有上游作业,就像在控制台输出中一样: 由上游项目启动allocate 内部编号31 最初由以下原因引起:由上游项目start启动内部编号12 最初由以下项引起: 我已经试过了groovy postbuild,内容如下: def build = Thread.currentThread()。可执行文件 def causes = manager.build.getCauses() for(cause in cause) { manager.listener.logger.printlnupstream build :+ cause.getShortDescription() } 但我只获得分配,而不是开始工作。 我也试过了 def build = Thread.currentThread()。executable def test = build.getUpstreamBuilds() for(up in test) { manager.listener .logger.printlntest build project:+ up } 但是这是空的.. 。 有什么想法? 解决方案。实际上,你需要做的是根据它的类型遍历这个原因的祖先。 以下是一段可让您开始使用的代码片段: def printCausesReferenceively(cause){ if(cause.class.toString()。contains(UpstreamCause)){ println这个工作是由+ cause.toString() for(upCause in cause.upstreamCauses){ printCausesRecursively(upCause)} } else { println根本原因:+ cause.toString() for(cause in manager.build.causes) { printCausesRecursively(cause)} 您可能想引用文档来处理所有原因类型: http://javadoc.jenkins-ci.org/hudson/model/Cause.html p> 希望它有帮助, 最好 I would like to get all the upstream jobs, just like in the console output:Started by upstream project "allocate" build number 31originally caused by: Started by upstream project "start" build number 12originally caused by: I've tried groovy postbuild with the following:def build = Thread.currentThread().executabledef 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.I've also tried def build = Thread.currentThread().executabledef test = build.getUpstreamBuilds()for (up in test){manager.listener.logger.println "test build project: " + up}but this is empty...Any ideas? 解决方案 You were close with your first solution.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)}You may want to refer to the documentation to handle all Cause types : http://javadoc.jenkins-ci.org/hudson/model/Cause.htmlHope it helps,Best 这篇关于获取Jenkins上游工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 12:02