Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 345
Gleeb Avatar asked Aug 27 '14 14:08

Gleeb


People also ask

How do I set environment variable in Jenkins pipeline stage?

Setting Stage Level Environment Variable It is by using the env variable directly in the script block. We can define, let us say, USER_GROUP and display it. You will see that the underlying shell also has access to this environment variable. You can also set an environment variable using withEnv block.


1 Answers

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.
like image 192
Slav Avatar answered Oct 20 '22 08:10

Slav