列出groovy中Jenkins上所有及时触发的工作

时间:2018-09-14 12:30:40

标签: jenkins groovy hudson

我想列出我的詹金斯及时触发的所有工作。 我尝试了以下代码。

#!/usr/local/bin/groovy

import hudson.model.*
import jenkins.model.*

void log(msg) {
    manager.listener.logger.println(msg)
}

hudson.model.Hudson.instance.getItems().each  { job ->
  def cause = "${job.getPreviousBuild().getCauses()}"
  if (cause =~ "UserIdCause") {
        log("Triggered by user")
    }
  if (cause =~ "TimerTriggerCause") {
        log("Timely triggered Job name:"+ job.getName())
  }
}

这给了我下面的错误:

Groovy script failed:
groovy.lang.MissingMethodException: No signature of method: hudson.model.FreeStyleProject.getPreviousBuild() is applicable for argument types: () values: []
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at Script1$_run_closure1.doCall(Script1.groovy:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)

我不知道是什么导致了此错误。
我已推荐链接:
Jenkins library: java.lang.NullPointerException: Cannot invoke method xxx on null object
Jenkins Groovy: What triggered job

1 个答案:

答案 0 :(得分:0)

在进行以下修改后立即得到它:

_id