我試圖使用系統Groovy腳本爲構建步驟這樣來操縱我們的工作之一,一些變量:總是解析爲null
//Current Thread run
def thr = Thread.currentThread()
def current_build = thr?.executable
//Get upstream job
def parent_build = current_build.getCause(hudson.model.Cause.UpstreamCause).getUpstreamRun()
//Upstream job build vars
def parentMap = parent_build.getBuildVariables()
println "ParentbuildMap.get(OSType) = " + parentMap.get("OSType")
println "ParentbuildMap.get(DBType) = " + parentMap.get("DBType")
//TODO insert SQL script here to populate OS_MACHINE and DB_MACHINE
def OS_MACHINE = parentMap.get("OSType") + ": ##Machine_Name##"
def DB_MACHINE = parentMap.get("DBType") + ": **Machine_Name**"
現在,當我嘗試打印它們的方式,它輸出值正常:
println "OS_MACHINE = " + OS_MACHINE
println "DB_MACHINE = " + DB_MACHINE
但是,當我嘗試以下方法,它只是打印空:
println build.buildVariableResolver.resolve(OS_MACHINE)
即使當我嘗試這個它打印空:
def thr = Thread.currentThread()
def current_build = thr?.executable
def buildMap = build.getBuildVariables()
println "buildMap.get(OS_MACHINE) = " + buildMap.get("OS_MACHINE")
println "buildMap.get(DB_MACHINE) = " + buildMap.get("DB_MACHINE")
而且還當我試試這個:
def params = [
new StringParameterValue('JOB_OS_MACHINE', OS_MACHINE),
new StringParameterValue('JOB_DB_MACHINE', DB_MACHINE),
]
build.addAction(new ParametersAction(params))
println "buildMap.get(JOB_OS_MACHINE) = " + buildMap.get("JOB_OS_MACHINE")
println "buildMap.get(JOB_DB_MACHINE) = " + buildMap.get("JOB_DB_MACHINE")
在這種情況下,如果我用一個簡單的字符串替換OS_MACHINE和DB_MACHINE忽略了最低甚至工作 https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script
import hudson.model.*
// get current thread/Executor
def thr = Thread.currentThread()
// get current build
def build = thr?.executable
// ... or if you want the parameter by name ...
def hardcoded_param = "FOOBAR"
def resolver = build.buildVariableResolver
def hardcoded_param_value = resolver.resolve(hardcoded_param)
println "param ${hardcoded_param} value : ${hardcoded_param_value}"
:
現在我已經恢復了運行在一個新的工作很簡單的腳本,該腳本是取自
這也打印空。
對於我的生活,我無法理解這裏出了什麼問題......任何人都可以幫助我嗎?
你可以嘗試沒有'def'關鍵字,所以變量進入綁定? –
它引發了一個NPE在groovy.lang.Binding.setVariable – danf
我不太瞭解Groovy,但任何機會這些是環境變量,而不是構建變量,因此無法訪問「buildVariableResolver」?我用'高清relnum = manager.build.getEnvVars() 「RELNUM」]'和'調用println 「$ {} relnum」' – Slav