访问Groovy脚本中的当前Jenkins构建

Ian*_*ski 6 groovy jenkins

我创建了一个Groovy脚本,该脚本用于System Groovy ScriptJenkins作业中的一个步骤,该作业需要访问当前作业的当前版本.

当使用Hudson.model Cause.UpstreamCause将当前作业的当前构建链接到我正在调度的依赖作业时,需要当前构建.

由于代码更简洁:

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)
}
Run Code Online (Sandbox Code Playgroud)

CauseAction构造(见于http://javadoc.jenkins-ci.org/hudson/model/Cause.UpstreamCause.html)需要一个Run对象,该对象的当前生成对象应的一个实例.我只是找不到一个很好的方法来获取Groovy脚本中当前正在运行的作业.

luk*_*a5z 9

如果你在Jenkins的工作中使用的是Groovy插件,那么在Execute system Groovy script插件内部,插件已经可以访问一些预定义的变量:

build
    The current AbstractBuild.
launcher
    A Launcher.
listener
    A BuildListener.
out
    A PrintStream (listener.logger).
Run Code Online (Sandbox Code Playgroud)

例如:

println build.getClass()
Run Code Online (Sandbox Code Playgroud)

输出:

class hudson.model.FreeStyleBuild
Run Code Online (Sandbox Code Playgroud)


Ian*_*ski 5

这是我一直在寻找的片段!

import hudson.model.*
def currentBuild = Thread.currentThread().executable
Run Code Online (Sandbox Code Playgroud)

这适合我的上述脚本,如下所示:

import hudson.model.*


def scheduleDependentJob(jobName) {
  def fooParam = new StringParameterValue('foo', 'bar');
  def paramsAction = new ParametersAction(fooParam)

  println "Scheduling dependent job"
  def currentBuild = Thread.currentThread().executable
  def cause = new Cause.UpstreamCause(currentBuild)
  def causeAction = new hudson.model.CauseAction(cause)
  instance.queue.schedule(job, 0, causeAction, paramsAction)
}
Run Code Online (Sandbox Code Playgroud)