Reputation: 2709
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?
Upvotes: 3
Views: 7766
Reputation: 1
Sorry to add an answer only now but i've got the same issue and reading this post give me a solution:
Thus, the resolver will find the value in the env parameter, as a jenkins build variable.
Doing this resolves my issue.
Upvotes: 0
Reputation: 982
A solution can be found in the Jenkins Users list archive. You are on the right track with creating new StringParameterValue objects and adding them to the builds ParametersAction.
The solution linked above did not work exactly as written for me (possibly because I'm on an older version of Jenkins 1.466.2). The createUpdated method was not recognized, so I replicated it in my code, which I'll post below.
// sets build parameters based on the given map
// only supports StringParameterValue
def setBuildParameters(parmMap) {
def npl = new ArrayList<StringParameterValue>()
for (k in parmMap.keySet()) {
npl.add(new StringParameterValue(k, parmMap.get(k)))
}
def newPa = null
def oldPa = build.getAction(ParametersAction.class)
if (oldPa != null) {
for (oldp in oldPa) {
if (! parmMap.containsKey(oldp.name)) { //oldp was not overridden by the new list
npl.add(oldp)
}
}
build.actions.remove(oldPa)
}
newPa = new ParametersAction(npl)
build.actions.add(newPa)
}
Upvotes: 2