Gleeb
Gleeb

Reputation: 11289

how to set Jenkins env variable with python script

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.

Upvotes: 6

Views: 12213

Answers (1)

Slav
Slav

Reputation: 27485

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.

Upvotes: 5

Related Questions