1

I am trying to read the environment variables in Groovy Postbuild step. I am able to read the values of parameters passed to builds but unable to read the values of parameters which are set in one of my Execute Windows batch command.

In one example of my Execute Windows batch command I do this:

SET custom_param=myValue

if I use ${custom_param} in other jenkins steps/jobs, it gets my value. So I am sure it has the value. I just can't get it in groovy script

I have tried followings to do so, none of them have worked:

  • manager.envVars['custom_param']
  • build.buildVariableResolver.resolve('custom_param')
  • build.getEnvironment(listener).get('custom_param')

Any help here would be great

Community
  • 1
  • 1
abdulH
  • 375
  • 1
  • 6
  • 19
  • Are you executing a "groovy script" or a "system groovy script"? Apparently the the groove script has no access to environment variables. More here: http://stackoverflow.com/questions/21236268/access-to-build-environment-variables-from-a-groovy-script-in-a-jenkins-build-st – hakamairi Apr 07 '16 at 15:29
  • 1
    Try to echo it in another batch, it usually doesnt work. Check if EnvInject plugin can help. – Dominik Gebhart Apr 07 '16 at 17:59

3 Answers3

1

(Assuming you're not running your script in groovy sandbox)
Try the bellow:

build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["YOUR_ENV_VAR"]
Vano
  • 1,416
  • 1
  • 13
  • 26
0

When you set some custom variables in your "Windows command batch" step, these variables are available only during this Jenkins step.

Once Jenkins move on the next step, your variables are lost...

If you want to set some variables permanently, you can try to use the SETX command: What is the difference between setx and set in environment variables in Windows?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Bruno Lavit
  • 10,184
  • 2
  • 32
  • 38
0

Groovy Post build step run as separate process. It has access to environment as normal JVM process.

You could use EnvInject plugin as a a build step. Subsequent steps in build will able to read this via normal environment access (System.env in your groovy script)

Jayan
  • 18,003
  • 15
  • 89
  • 143