0

So, I have got a Jenkins job which is a parameterized build. I allow the users to select one of the environments where they want to execute the build. This parameter is stored in variable ${my_env}. Then I use "Execute Windows Batch Command" component in Job's configuration to execute the following command :

setx /M envi %my_env%

This allows me to set the environment variable on my windows slave machine as envi=my_env. Now I have my project on this slave machine which is python based, and I fetch the environment variable in my project using os.environ['envi'] and execute the code further accordingly based on the value of 'envi'. Problem is when I first execute this job, it is all fine. It fetches the correct value of environment variable 'envi' and execute the correct code as per the environment selected. But when I execute it second time choosing the different environment from build parameters, it does not update in my project and it picks the old value of environment variable 'envi'. I have verified that the actual value of environment is updated using the Setx command, but my python project still fetches the old value. Any ideas anyone?

1 Answers1

0

As per the working of setx and set variables in windows, setx will update the value permanently but not make it available immediately. set will make it available immediately but not permanent. So you have to use both to work immediately. So code will be like :

SET XYZ=<value>
SETX /M XYZ <value>

This will make XYZ value to be set in this session and also makes it permanent. I am adding some useful links to study for yourself. Hope this helps.

  1. https://superuser.com/a/916652/652923
  2. https://stackoverflow.com/a/34777506/5003256
  3. https://superuser.com/a/176788/652923
SV Madhava Reddy
  • 1,858
  • 2
  • 15
  • 33