Groovy代码在jenkins上抛出错误

时间:2012-12-10 12:40:19

标签: groovy jenkins

您已经在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的新手,我不知道如何继续?

1 个答案:

答案 0 :(得分: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:您不需要使用groovy

导入java.util.*