Groovy script success via script console but fails as a job

598 views
Skip to first unread message

Sunshine

unread,
Apr 19, 2018, 3:58:52 AM4/19/18
to Jenkins Users
Hi all ,

I tried writing a groovy script to list builds in queues assigned to offline slaves and it gives me list when I execute via script console but not via a Jenkins job.

I'm just a beginner in groovy but tried this below code which works fine in the script console but fails with below error as a job.

Any help would be appreciated.

import hudson.model.*

def queue = Hudson.instance.queue

println "Total Build Queue contains ${queue.items.length} items"

println("==============================================")

println("\n")

println("\n")

def q = Jenkins.instance.queue

String reason;

int count;

int time_in_ms ;

println("Below build queues are due to Offline slaves")

println("==============================================")

println("\n")

println("\n")

q.items.each {

reason = "${it.why}";

if(reason.contains("offline"))

{

println("Task Name: ${it.task.name}:")

println(" Job is : ${it.task.url}")

println("Reason for being in Queue: ${it.why}")

println("==============================================")

println("\n")

}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Started by user abcd
[EnvInject] - Loading node environment variables.
Building remotely on slave1 in workspace c:/jenkins_home/workspace/BuildStuckInQueue
No emails were triggered.
Total Build Queue contains 12 items
==============================================




ERROR: Build step failed with exception
groovy.lang.MissingPropertyException: No such property: Jenkins for class: Script1
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
at Script1.run(Script1.groovy:9)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:650)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:636)
at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:98)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:785)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
at hudson.model.Run.execute(Run.java:1741)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:408)
Build step 'Execute system Groovy script' marked build as failure
Started calculate disk usage of build

Sunshine

unread,
Apr 19, 2018, 6:31:20 AM4/19/18
to Jenkins Users
I got this working.
My bad forgot to import the necessary packages.


Any idea on how can't I send mails to the owner of the build in the above scenarios of stock jobs in que.
I couldn't find any owner option in the /yourhost/queue/API/xml

Reply all
Reply to author
Forward
0 new messages