6

I have a jenkins build process and i use a python script to calculate the new version:

import string
import os
print 'Current version is ' + os.environ['POM_VERSION']
versionArr = string.split(os.environ['POM_VERSION'], '.')
versionArr[2] = str(int(versionArr[2]) + 1)
if  int(versionArr[2]) > 100:
    versionArr[2] = '0'
    versionArr[1] = str(int(versionArr[1]) + 1)
if int(versionArr[1]) > 100:
    versionArr[0] = str(int(versionArr[0]) + 1)
    versionArr[1] = '0'
print  versionArr
print 'New version will be: ' + versionArr[0] + '.' + versionArr[1] + '.' + versionArr[2]
os.environ['NEW_POM_VERSION'] =  versionArr[0] + '.' + versionArr[1] + '.' + versionArr[2]

And then i want to run

versions:set -DnewVersion=${NEW_POM_VERSION} -DgenerateBackupPoms=false

on a different step. but the ${NEW_POM_VERSION} stays the same and does not translate to the value i set.

Am i trying to call the variable in the wrong way. i also tried using $NEW_POM_VERSION which didn't work as well

so how am i supposed to export the variable correctly to my environment.

Thanks.

1 Answer 1

5

Jenkins spawns a new environment for each build step (and post-build step). Setting a value in one will not be persisted to the rest.

You need EnvInject plugin.

  • In your python script, write the final value to a properties file, in format param=value.
  • Next, configure an EnvInject build step to load variables from that properties file.
  • At this point, those loaded properties will be available as environment variables to all other build steps/post-build steps of this job.
Sign up to request clarification or add additional context in comments.

2 Comments

I know this is a bit off topic. but the environment variable is confined with in the build process right?. There is no way i am going to change some important system variables?
Yes, that's the whole reason for spawning a separate environment for build step: so that you cannot mess up the system (and other running Jenkins jobs). Of course you can purposely break it, if you use setx or something like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.