2
votes

is it possible to run jenkins job from groovy script?

I know it is possible to run jenkins job from pipeline like this

build job: 'test'

this also doesn't work

build('test')

error for

build job: 'test'

    Caught: groovy.lang.MissingMethodException: No signature of method: hudson8298793087824999032.build() is applicable for argument types: (LinkedHashMap) values: [[job:test]]
Possible solutions: find(), find(groovy.lang.Closure), wait(), run(), run(), dump()
groovy.lang.MissingMethodException: No signature of method: hudson8298793087824999032.build() is applicable for argument types: (LinkedHashMap) values: [[job:test]]
Possible solutions: find(), find(groovy.lang.Closure), wait(), run(), run(), dump()
    at hudson8298793087824999032.run(hudson8298793087824999032.groovy:32)
Build step 'Execute Groovy script' marked build as failure

After edit Execute system Groovy script and added

def currentBuild = Thread.currentThread().executable
def job = hudson.model.Hudson.instance.getJob("jobname")
println(job)
def params = new StringParameterValue('version', '1.0.0')  
println(params)
def params1 = new StringParameterValue('target', "dev")  
println(params1)
def paramsAction = new ParametersAction(params, params1) 
println(paramsAction)
def cause = new hudson.model.Cause.UpstreamCause(currentBuild)
println(cause)
def causeAction = new hudson.model.CauseAction(cause) 
println(causeAction)
println('test')
Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction)

I got error on this line Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction)

java.lang.NullPointerException
at hudson.model.queue.Tasks.getOwnerTaskOf(Tasks.java:60)
at com.cloudbees.hudson.plugins.folder.ItemDeletion.getItemOf(ItemDeletion.java:197)
at com.cloudbees.hudson.plugins.folder.ItemDeletion.shouldSchedule(ItemDeletion.java:174)
at hudson.model.Queue.schedule2(Queue.java:589)
at hudson.model.Queue.schedule2(Queue.java:712)
at hudson.model.Queue.schedule(Queue.java:705)
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.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:192)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
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:149)
at Script1.run(Script1.groovy:47)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:343)
at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
at hudson.model.Build$BuildExecution.build(Build.java:206)
at hudson.model.Build$BuildExecution.doRun(Build.java:163)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1818)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)

Build step 'Execute system Groovy script' marked build as failure

1
I am sorry but I don't understand your question. In which scope or where do you want to execute the script? Do you want to start a Jenkins build with a groovy script?Michael Kemmerzell
build job: 'test' should work. what error do you have?daggett
The build job: step is supposed to work only with Pipelines. For triggering jobs using Groovy script outside Pipelines, see stackoverflow.com/questions/8884166/….Dibakar Aditya
@MateuszSobczak You might have to approve the scripts from Manage Jenkins > In-process Script Approval.Dibakar Aditya

1 Answers

1
votes

For me, I needed the code to run on the master to have quick access to the Jenkins objects and build a job that has the appropriate permissions to send a mail within the information acquired.

I used the build module Execute system Groovy Script to run the script and appended the following code:

// The job you want to run from groovy
def job = hudson.model.Hudson.instance.getJob("SendEmailJob")

// jobsList is a list of jobs of interest formated as HTML
def body = jobsList

def params = []

params += new StringParameterValue('TO', "<[email protected]>")
params += new StringParameterValue('BODY_AS_HTML', body)
params += new StringParameterValue('SUBJECT', "List of jobs of interest")

def paramsAction = new ParametersAction(params)
def cause = new hudson.model.Cause.UpstreamCause(build)
def causeAction = new hudson.model.CauseAction(cause)
hudson.model.Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction)

This is based on question how-do-i-dynamically-trigger-downstream-builds-in-jenkins