2

嗨,我在 jenkins 的 Post-build Actions 中添加了以下 groovy 代码。

import java.util.*
import hudson.model.*

upstreamBuilds = manager.build.getUpstreamBuilds();
upstreamJob = upstreamBuilds.keySet().iterator().next();
lastUpstreamBuild = upstreamJob.getLastBuild();
if(lastUpstreamBuild.getResult().isBetterThan(manager.build.result)) {
lastUpstreamBuild.setResult(manager.build.result);
}

但是当我执行上述代码时出现以下错误:

ERROR: Failed to evaluate groovy script.
java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:897)
at java.util.HashMap$KeyIterator.next(HashMap.java:928)
at java_util_Iterator$next.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at Script1.run(Script1.groovy:5)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
at org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.perform(GroovyPostbuildRecorder.java:276)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:19)
at hudson.model.AbstractBuild$AbstractRunner.perform(AbstractBuild.java:710)
at hudson.model.AbstractBuild$AbstractRunner.performAllBuildSteps(AbstractBuild.java:685)
at hudson.model.Build$RunnerImpl.post2(Build.java:162)
at hudson.model.AbstractBuild$AbstractRunner.post(AbstractBuild.java:632)
at hudson.model.Run.run(Run.java:1463)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:239)
Recording fingerprints

由于我是 groovy 的新手,我不知道如何进行?

4

1 回答 1

1

我相信这是因为您正在调用next()一个没有元素的迭代器。

我相信这做同样的事情,但处理upStreamBuilds地图为空:

upstreamJob = manager.build.upstreamBuilds.find()
if( upstreamJob != null ) {
  lastUpstreamBuild = upstreamJob.key.lastBuild
  if( lastUpstreamBuild.result.isBetterThan( manager.build.result ) ) {
    lastUpstreamBuild.result = manager.build.result
  }
}

PS:你不需要java.util.*用groovy导入

于 2012-12-10T13:13:37.240 回答