ashish behl
ashish behl

Reputation: 339

Parameterised build in jenkins: Not getting parameters as shell env variables

Jenkins version: 2.6, Linux

Problem: The parameterized build variables are not are not visible (as env variables) in the Execution step "shell script", They used to be visible in the older 1.x jenkins version.

Steps:

  1. Create a parameterized build with a multi configuration project.
  2. Add a parameter to the build (using This project is parameterized-> string parameter, {if that matters} ).
  3. Add a build step "Execute shell" to the job.
  4. Access these parameters in this shell script as env variables.

    echo "++++++++++++ building $lib_name ($lib_version) ++++++++++++++"

To solve this, I have tried to create a groovy script in "Prepare an environment for the run" section. I created env variables using hardcoded values which are pased to shell script as env vars.

def map = ['lib_name':'lib1']
map['lib_version'] = 'master'
return map

But, without hardcoding, I cannot access these variable values even when using solution from How to retrieve Jenkins build parameters using the Groovy API?

I dont know what else has to be done. Can some one please suggest?

---> Updating based on the comments on this question: When I run the following lines in jenkins, I get exception:

def buildVariablesMap = Thread.currentThread().executable.buildVariables 
buildVariablesMap.each{ k, v -> println "${k}:${v}" }




FATAL: No such property: executable for class: hudson.model.OneOffExecutor
groovy.lang.MissingPropertyException: No such property: executable for class: hudson.model.OneOffExecutor
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:66)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)

Upvotes: 0

Views: 4056

Answers (4)

Zoltan
Zoltan

Reputation: 11

I have had also similar problem. This is a solution which worked for me. I created a method which always takes and delivers exactly one build parameter which I need.

method:

String readingBuildParameters(VariableResolver varRes, String paramName){
    return varRes.resolve(paramName)
}

the line how I use it in a code:

Build currentBuild        = Executor.currentExecutor().currentExecutable
VariableResolver varResolver    = currentBuild.getBuildVariableResolver()

df_parameter    = readingBuildParameters(varResolver, "parameter_name")

BR, Zoltan

Upvotes: 1

ashish behl
ashish behl

Reputation: 339

This was a bug in jenkins, probably in the credentials plugin: https://issues.jenkins-ci.org/browse/JENKINS-35921

Thanks for all your help!

Upvotes: 0

Rogério Peixoto
Rogério Peixoto

Reputation: 2257

To retrieve build variables from groovy script as a build step:

def buildVariablesMap = Thread.currentThread().executable.buildVariables 
println buildVariablesMap['BUILD_NUMBER']

But please note that for your custom/altered environment variables to be visible for the next steps you should use EnvInject plugin, with it you can define a step that will export new env variables as key-value pair just like properties file.

Upvotes: 0

mrkernelpanic
mrkernelpanic

Reputation: 4471

To access the parameters in your shell script:

to evaluate them in echo: e.g. echo "${myParam}"

to use them in code: def myNewvalueParam = ${myOtherParam}

Upvotes: 0

Related Questions