在 groovy 中列出 Jenkins 上所有及时触发的作业
List all timely triggered jobs on Jenkins in groovy
我想列出我的 jenkins 中所有及时触发的作业。
我尝试了下面的代码。
#!/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)
我不知道是什么导致了这个错误。
我提到了链接:
经过以下修改后正确:
#!/usr/local/bin/groovy
import hudson.model.*
import jenkins.model.*
import com.cloudbees.hudson.plugins.folder.Folder
void log(msg) {
manager.listener.logger.println(msg)
}
Jenkins.instance.getAllItems(AbstractProject.class).each { job ->
if (job instanceof Folder) {
log("[i] Folder found:" + job.getName())
}
else if(job.getLastBuild())
{
def cause = "${job.getLastBuild().getCauses()}"
if (cause =~ "UserIdCause") {
log("Job triggered by user: "+job.getName())
}
if (cause =~ "TimerTriggerCause") {
log("Timely triggered Job: "+ job.getName())
}
}
}
我想列出我的 jenkins 中所有及时触发的作业。 我尝试了下面的代码。
#!/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)
我不知道是什么导致了这个错误。
我提到了链接:
经过以下修改后正确:
#!/usr/local/bin/groovy
import hudson.model.*
import jenkins.model.*
import com.cloudbees.hudson.plugins.folder.Folder
void log(msg) {
manager.listener.logger.println(msg)
}
Jenkins.instance.getAllItems(AbstractProject.class).each { job ->
if (job instanceof Folder) {
log("[i] Folder found:" + job.getName())
}
else if(job.getLastBuild())
{
def cause = "${job.getLastBuild().getCauses()}"
if (cause =~ "UserIdCause") {
log("Job triggered by user: "+job.getName())
}
if (cause =~ "TimerTriggerCause") {
log("Timely triggered Job: "+ job.getName())
}
}
}