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.
Below I will show how to do it at job level. From the Jenkins web interface, go to Manage Jenkins > Manage Plugins and install the plugin. Go to your job Configure screen. Find Add build step in Build section and select Inject environment variables. Set the desired environment variable as VARIABLE_NAME=VALUE pattern.
EnvInject plugin provides the following features: Removes inherited environment variables by the Jenkins Java process. Injects environment variables at node (master/slave) startup. Executes a setup script before or/and after a SCM checkout for a run. Injects environment variables before or/and after a SCM checkout for a run.
For example: If you go to http://localhost:8080/env-vars.html/ on a Jenkins installation, you should see a list like this: If your job also has parameters, then these will be available just like environment variables.
We can set global properties by navigating to “Manage Jenkins -> Configure System -> Global properties option”. Let's first check the “Environment variables” checkbox and then add the variables and their respective values inside the “List of Variables” section:
My solution:
Create a "Build a free-style software project".
key=value
pairs to a .properties fileAll 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With