简体   繁体   English

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

[英]Accessing the current Jenkins build in Groovy script

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. 我创建了一个Groovy脚本,该System Groovy Script在Jenkins作业的System Groovy Script步骤中使用,该作业需要访问当前作业的当前版本。

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. 当使用Hudson.model Cause.UpstreamCause将当前作业的当前构建链接到我正在调度的依赖作业时,需要当前构建。

Since code is more concise: 由于代码更简洁:

my-job-step.groovy : 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. CauseAction构造函数(见http://javadoc.jenkins-ci.org/hudson/model/Cause.UpstreamCause.html )需要一个Run对象,当前构建对象应该是该对象的实例。 I just can't find a good way to get the current running job build inside of a Groovy script. 我只是找不到一个很好的方法来获取Groovy脚本中当前正在运行的作业。

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: 如果在Jenkins作业中使用的是Groovy插件 ,那么在Execute system Groovy script步骤中,插件已经为您提供了一些预定义变量的访问权限:

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

This is the snippet I have been looking for! 这是我一直在寻找的片段!

import hudson.model.*
def currentBuild = Thread.currentThread().executable

This fits in with my above script like so: 这适合我的上述脚本,如下所示:

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)
}

This completes the answer from luka5z to exemplify how scriptler passes the listener to the groovy script: 这完成了luka5z的答案,以举例说明scriptler如何将侦听器传递给groovy脚本:

import jenkins.model.*;
import hudson.model.*;
import hudson.util.*;
import hudson.tasks.*;
import hudson.plugins.git.*;
import hudson.scm.*
import jenkins.scm.*

def build = Thread.currentThread()?.executable
//def svn_branch = build.buildVariableResolver.resolve("SVN_BRANCH")

if (build instanceof AbstractBuild){
    def workspace = build.workspace
    //def job = build.parent
    def scm = build.parent.scm;
    //println "scm: $scm"

    if (scm instanceof hudson.scm.SubversionSCM) {
        scm.checkout(build, null/*Launcher*/, workspace /*workspace*/, listener/*listener*/, null /*changelogfile*/,null/*baseline*/)
    }else if (scm instanceof hudson.plugins.git.GitSCM) {
        scm.checkout(build, null/*Launcher*/, workspace /*workspace*/, listener/*listener*/, null /*changelogfile*/,null/*baseline*/)
    }
}

listener in Scriptler Scriptler中的监听器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM