4

I have a Jenkins build with one parameter called VERSION.

Based on the length of the variable i am modifying its value in Windows Powershell and then in the next build step, i want to use it.

But the modified value is not being reflected in the next build step execution, it still refer to the intial value entered as a parameter. I tried ENV,script,global none of them seems to work.

Windows powershell build step

input VERSION=1810(via jenkins build)

 if ("$ENV:VERSION".length -eq 4) { $ENV:VERSION = "$ENV:VERSION",3 -join "" (here it will be 18103) } Write-Output "$ENV:VERSION" (18103 here aswell) 

later in the Nexus artifact uploader i refer to this variable as ${VERSION} and the above updated value is not being reflected

 (here it is 1810 and not 18103) 

Please help

4 Answers 4

2

This is a general issue with environment variable scope. Every process inherits the environment variables from its parent, but has its own copy, and any modifications you make will only be reflected in the current process and child processes.

I think you will have to find some way to pass the value to a future step that doesn't rely on environment variables.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try to use EnvInject Plugin and set additional PROJ_VERSION=$ENV:VERSION variable in your job. In this case it should be working correctly. If it isn't working within Properties Content directly, try to use injection via file as in this example.

Comments

0

I found another option which works in Freestyle project jobs.

the 1st powershell step:

[Environment]::SetEnvironmentVariable('IMAGE_VERSION', $imageVersion, 'Machine') 

the 2nd powershell step:

$imageVersion = [Environment]::GetEnvironmentVariable('IMAGE_VERSION', 'Machine') 

1 Comment

This looks like it runs at the machine level. If so, then running the script multiple times at the same time creates a race condition.
0

With the EnvInject plugin you can retrieve the environment variables you want to inject from a file. So if you use Powershell (or Python, etc.) to save your environment var to a file first and then use the plugin, it should work fine.

Powershell:

$env_var="MY_ENV_VAR=12345" $env_var | Out-File -FilePath "$ENV:WORKSPACE/file.properties" -Encoding ASCII 

Environment Injection Plugin (after Powershell build step):

enter image description here

You can now use MY_ENV_VAR it in other build steps, e.g. in another Powershell command window with $ENV:MY_ENV_VAR

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.