是否可以从groovy脚本中运行jenkins作业?
我知道可以从这样的管道中运行詹金斯工作
构建作业:“测试”
这也不起作用
build('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
编辑后,执行系统Groovy脚本并添加
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)
我在这条线上出现错误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)
构建步骤'Execute system Groovy script'将构建标记为失败
答案 0 :(得分:0)
对我来说,我需要在主服务器上运行的代码才能快速访问Jenkins对象,并构建一个具有适当权限的作业,以便在所获取的信息内发送邮件。
我使用了构建模块执行系统Groovy脚本来运行脚本并附加了以下代码:
// 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', "<team@example.com>")
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)
这基于问题how-do-i-dynamically-trigger-downstream-builds-in-jenkins