[英]Accessing the current Jenkins build in Groovy script
我創建了一個Groovy腳本,該System Groovy Script
在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 "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)
}
CauseAction
構造函數(見http://javadoc.jenkins-ci.org/hudson/model/Cause.UpstreamCause.html )需要一個Run
對象,當前構建對象應該是該對象的實例。 我只是找不到一個很好的方法來獲取Groovy腳本中當前正在運行的作業。
如果在Jenkins作業中使用的是Groovy插件 ,那么在Execute system Groovy script
步驟中,插件已經為您提供了一些預定義變量的訪問權限:
build
The current AbstractBuild.
launcher
A Launcher.
listener
A BuildListener.
out
A PrintStream (listener.logger).
例如:
println build.getClass()
輸出:
class hudson.model.FreeStyleBuild
這是我一直在尋找的片段!
import hudson.model.*
def currentBuild = Thread.currentThread().executable
這適合我的上述腳本,如下所示:
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)
}
這完成了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*/)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.