Jenkins inject environment variable

25,604

My solution:

Create a "Build a free-style software project".

  1. Jenkins > New Item > Build a free-style software project
  2. Add 1st step: Execute shell @ Build, and echo key=value pairs to a .properties file
  3. Add 2nd step: Inject environment variables, use the .properties file as defined in step 2
  4. Add 3rd step: Invoke top-level Maven targets

All custom environment variables are accessible with the key as defined in step #2. This was the only way I found to inject environment variables from shell to java.

As explained.

Share:
25,604
Velth
Author by

Velth

Updated on July 07, 2020

Comments

  • Velth
    Velth almost 4 years

    In a Jenkins job I'm doing a couple of actions that reside in the pre-step build, such as executing a shell script. With the use of the Jenkins plugin "EnvInject" I want to inject environment variables into my maven build (Unit tests) so that those can be used inside my Java unit tests. Inside the shell script im doing something similar as:

    echo "ip=$IP" >> unit-test.properties
    

    While building Jenkins outputs the following:

    [EnvInject] - Injecting environment variables from a build step.
    [EnvInject] - Injecting as environment variables the properties file path 'unit-test.properties'
    [EnvInject] - Variables injected successfully.
    

    But the "ip" variable is not available inside my Java code (unit test). When I do a full print of both System.getProperties() and System.getenv() I do not see the "ip" enlisted.

    Do I need to perform any special actions for maven to pass the variable to my Java code? Is something else wrong?

    I'm pretty much stuck from this point onward, I do want to inject a key=value from a pre-step into my Java code.

  • Velth
    Velth almost 9 years
    Whenever putting -DargLine="-DEnv=$IP" into the "Goals and options" I get the key/value pair "Env=$IP" available in Java. However the $IP is not resolved to its real value (i.e. 192.168.1.1). How do I get that variable resolved to the value that is set into that variable in my shell script?
  • Aditya
    Aditya almost 9 years
    Glad you can work out your problem, still if what I understand by "$IP is not resolved to its real value" is you were not able to pass some IP value into your variable. Well, you can do that by clicking checkbox "This build is parameterized", and define one String Parameter(IP in your case) when the list is opened. This helps in passing some specific IP everytime before you commence a build. Rest steps are same as defined in my comment. Also, I edited my comment. It was "getProperty()" by which you can fetch the value which u pass in parameter.
  • Velth
    Velth almost 9 years
    The IP is dynamic and determined by another (external) system. Thus the $IP value is resolved to its real value in my shell script (i.e. ip=192.168.1.1). And this key=value pair i'd like to be injected into my Java environment so I can use with with System.getEnv() or System.getProperty(). I wasn't able to achieve this with your proposed solution. A parameterized build isn't going to work either since the ip is dynamic and not known in advance.