1

I am trying to set job environment variables. Some variables are set from other environment variables. For instance on Windows:

var1=this
var2=that
var3=other
var4=%var1%_%var2%_%var3%

And I'd like to see var4 set as 'this_that_other'. I tried setting the first three as job variables and the fourth as a build variable. No joy.

Jeff Hutton
  • 156
  • 1
  • 2
  • 11

3 Answers3

1

Because you set the variable only in the current process scope. As you said if you don't want to use file to store and retrieve, another option is making the environment variable setting machine wise, if that serve your need and won't have any side effects.

If you could install PowerShell plugin, use PowerShell script is very easy to do this:

[Environment]::SetEnvironmentVariable("VAR4", $WhatEverYouWant, "Machine")

This makes the VAR4 variable available immediately on machine level, meaning in all the other processes.

Andy Chen
  • 361
  • 2
  • 9
  • Thanks Andy, I didn't know that, but in this case there may be multiple processes running on that machine with different values for VAR4. – Jeff Hutton Feb 07 '14 at 13:07
1

You can do this using the EnvInject Plugin for Jenkins. Here var1, var2 and var3 are build parameters, or predefined environment variables. The same thing can also be done using Groovy.

enter image description here

EricP
  • 980
  • 5
  • 8
  • We are using the EnvInject Plugin (1.83) and maybe I'm being stupid about its use, but it does not help. – Jeff Hutton Feb 07 '14 at 21:17
  • I was not precise in my problem statement but I had: $var1=this $var2=that $var3=$var1_$var2. It would not see the _ as a literal, instead looking for $var1_. It does work using \ instead of _. The way out was to set $sep=_ and $var3=$var1$sep$var2. – Jeff Hutton Feb 10 '14 at 16:26
0

It does work to set var4 in a batch command if that is where var4 is needed:

set var4=%var1%_%var2%_%var3%

var4 now has the desired value for the duration of the batch command, but not for any subsequent build steps. This doesn't help me because I need to use var4 in an ant command.

Jeff Hutton
  • 156
  • 1
  • 2
  • 11