在Groovy脚本中访问当前的Jenkins构建 [英] Accessing the current Jenkins build in Groovy script
问题描述
我创建了一个Groovy脚本,用于需要访问当前作业的当前版本的Jenkins作业中的 System Groovy Script
步骤。
使用Hudson.model Cause.UpstreamCause
来连接当前版本的当前版本工作到我正在安排的依赖工作。
由于代码更加简洁:
my-job-step.groovy
:
def scheduleDependentJob(jobName){
def fooParam = new StringParameterValue('foo' ,'bar');
def paramsAction = new ParametersAction(fooParam)
println调度相关作业
def currentJob = ???
def cause = new Cause.UpstreamCause(currentBuild)
def causeAction = new hudson.model.CauseAction(cause)
instance.queue.schedule(job,0,causeAction,paramsAction)
$ / code>
CauseAction
构造函数在 http://javadoc.jenkins-ci.org/hudson/model/上Cause.UpstreamCause.html )需要一个运行
对象,当前的构建对象应该是一个实例。我只是找不到一个很好的方式来获取Groovy脚本中正在运行的作业。
Jenkins在您使用 Groovy插件时工作,然后在执行系统Groovy脚本
步骤插件已经为您提供了一些预定义变量的访问权:
build
当前的AbstractBuild。
启动器
启动器。
监听器
一个BuildListener。
out
PrintStream(listener.logger)。
例如:
println build.getClass()
输出:
class hudson.model.FreeStyleBuild
I have created a Groovy script which is used in a System Groovy Script
step in a Jenkins job which needs to access the current build of the current job.
The current build is required when using an Hudson.model Cause.UpstreamCause
to link the current build of my current job to a dependent job that I am scheduling.
Since code is more concise:
my-job-step.groovy
:
def scheduleDependentJob(jobName) {
def fooParam = new StringParameterValue('foo', 'bar');
def paramsAction = new ParametersAction(fooParam)
println "Scheduling dependent job"
def currentJob = ???
def cause = new Cause.UpstreamCause(currentBuild)
def causeAction = new hudson.model.CauseAction(cause)
instance.queue.schedule(job, 0, causeAction, paramsAction)
}
The CauseAction
constructor (Seen on http://javadoc.jenkins-ci.org/hudson/model/Cause.UpstreamCause.html) requires a Run
object, which the current build object should be an instance of. I just can't find a good way to get the current running job build inside of a Groovy script.
If in your Jenkins job you are using Groovy plug-in, then inside Execute system Groovy script
step the plug-in already provides you access to some predefined variables:
build
The current AbstractBuild.
launcher
A Launcher.
listener
A BuildListener.
out
A PrintStream (listener.logger).
For example:
println build.getClass()
Outputs:
class hudson.model.FreeStyleBuild
这篇关于在Groovy脚本中访问当前的Jenkins构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!