I'm trying to manipulate some variables in one of our jobs using a system groovy script as a build step as such:
//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**"
Now, when i try printing them as such, It prints the values fine:
println "OS_MACHINE = " + OS_MACHINE
println "DB_MACHINE = " + DB_MACHINE
But when i try the following, it just prints null:
println build.buildVariableResolver.resolve(OS_MACHINE)
Even when I try this it prints null:
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")
And also when i try this:
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")
In this case it dosen't even work if i replace OS_MACHINE and DB_MACHINE with a plain String
I've now reverted to running a very simple script on a new job, the script was taken from: 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}"
Which also prints null.
For the life of me, i can't fathom what is going wrong here... can anyone help me please?
def
keyword so the variables go in the binding?def relnum= manager.build.getEnvVars()["RELNUM"]
andprintln "${relnum}"