3

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?

4
  • Can you try without the def keyword so the variables go in the binding?
    – tim_yates
    Mar 13, 2014 at 14:37
  • It throws an NPE at groovy.lang.Binding.setVariable
    – danf
    Mar 13, 2014 at 14:51
  • I don't know much about Groovy, but any chance those are environment variables and not build variables, and thus not accessible to "buildVariableResolver"? I use def relnum= manager.build.getEnvVars()["RELNUM"] and println "${relnum}"
    – Slav
    Mar 13, 2014 at 18:08
  • My issue specifically is that this block does not work: def params = [ new StringParameterValue('JOB_OS_MACHINE', OS_MACHINE), new StringParameterValue('JOB_DB_MACHINE', DB_MACHINE), ] build.addAction(new ParametersAction(params))
    – danf
    Mar 16, 2014 at 8:45

2 Answers 2

2

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)
}
2
  • Thank you for the answer, but i don't understand how is this different from what i'm doing - in order for the new parameters to be appended to the build parameters do i HAVE to make a whole new list containing both like you suggested?
    – danf
    Mar 16, 2014 at 8:49
  • Turns out the answer to my question is yes: you have to replace the old ParametersAction with a new one (that can contain the old one + whatever new parameters you want to insert) the script @EricP suggested works for me.
    – danf
    Mar 18, 2014 at 9:38
0

Sorry to add an answer only now but i've got the same issue and reading this post give me a solution:

  • just add in the jenkins jobs, a string parameter with the same name of your variable.

Thus, the resolver will find the value in the env parameter, as a jenkins build variable.

Doing this resolves my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.