mangusbrother mangusbrother - 5 months ago 125
Linux Question

Java not finding environment variables defined in Jenkins

I have a snippet of code that is obtaining an environment variable as follows:

System.getenv("MY_VAL")


locally on my windows machine this works fine.

However, on my Jenkins CI Server which is running CentOS I am encountering some issues

I have tried setting the value of MY_VAL through both the envinject plugin as well as the global jenkins settings

If i do a pre-build step to echo the value out, it works fine, however inside my java code this is not being resolved.

How do I get this to be resolved?

Answer

You can achieve that by installing EnvInject plugin.

1) After installing check the Prepare an environment for the job option in the job configuration screen. This option will display several field for you to fill.

UPDATE

2) Fill the Script Content area with a command touch env.properties to create the file.

3) Fill the Properties Contentt field with the variables you want to inject inside your recently created env.properties file by doing so. Place one variable per line ex:

     VARIABLE1=value
     VARIABLE2=value

4) Reference env.properties file you've just created in the file path area.

5) At runtime Jenkins will inject those variables and they will be available to your program.