Jenkins environment variables from file for nodes group - jenkins

Is there a way to set environment variables from a file to a group of nodes in Jenkins?
Let's say there are two groups: build nodes with the label build and test nodes with the label test. The idea is whenever a job is scheduled to build nodes, set the environment variables from file build.properties file and the same logic is for test nodes.
I found a plugin - Environment Injector that almost does what I need - it could inject environment variables from a file. But it does that on a single node level, while I need to be able to assign it on a node group level. Also, I'm not sure if the plugin could append values to the PATH variable using a file.

Environment Injector should solve the issue.
to set the variables at the global level, which will apply to all nodes. make sure to Go to the Manage Jenkins page and click on the Configure System link. Scroll down to the Global Properties section and click the Add button next to the "Environment variables" field.
as they mentioned in there docs, it can set At node (controller and agent) level. At node (controller and agent) level.
https://plugins.jenkins.io/envinject/#plugin-content-at-node-controller-and-agent-level
when you add In the Name field, enter the name of the environment variable. In the Value field, enter the value for the variable. You can also use the Properties File Path field to specify a file that contains the environment variables you want to set.

Related

Jenkins Extended Choice Parameter associated to multiple environments variable

I am using in a Test Jenkins configuration the Choice Parameter in order to select the target environment against the tests are running.
Like this:
I use the selected value to pass to the maven test run as selected profile: -Dprofile=${TargetEnv}.
I would like to extend the implementation to make some additional git merge operation before the repository is build and the tests are triggered to run (I don't want to go into detail).
My question is how can I use the "Extended Choice Parameter" Jenkin plugin to have multiple environment variable set when a each choice value.
Example: In case the 'dev' is select then I would like to have two environment variables: targetEnv: qa and lowerEnvBranchName: develop-dev.
Somebody know how is possible to specify this kind of variables?

i want to make generic parameters in jenkins

How can I make generic parameters in jenkins that will be updated automatically for example I want to be able to create a parameter which hold today's date and it will be update automatically and not manually ?
thanks!
You might want to use EnvInject.
Setup your job and then add a build step "Inject envornment variables" from a file with an absolute path or the path relative to current job's workspace, which will contain something like:
DATE_VARIABLE="20150708"
OTHER_OPTIONAL_VARIABLE="value"
In previous execute shell step you may for example do:
echo "DATE_VARIABLE="`date +"%d%m%Y"` > env_inject.txt
After all that you don't need Parameters for a build, since you will have the needed parameter injected as an environment variable.

Inject environment variables plugin is passing value as "12345"

I am using "Inject environment variables" Jenkins plugin to pass values to child jobs. Property file name is config.Today_date(eg. config.0403) and it has two parameters in it 1. change list(eg. 175444) 2. today's date(eg. 04032014). In child job Properties file path is config.${Today_date}($Today_date is predefined parameter in parent job).
First issue is: it error out saying config."Today_date" do not exist. I went to location and found property file is there and realized that child job is searching properties file as config."Today_date"(config."0403")and actual name of file is config.today_date
Second issue is: if i put property file name as config.0403(today_date) in Properties file path, Then it successfully locate the file but where ever it use parameters from this file, it use them as "0403" & "175444"
Please advise how i can solve "" issue at both locations.
Thanks !!

Jenkins Parameterized build to use key/value pairs

I have a Jenkins parameterized build. I tick the "this build is parameterized" and I set a "Choice" environment name to be "ENVIRONMENT" and then as choices I define human readable names such as "Test env1", "Test env2", etc. However I want these keys to actually contain different values, for example "Test env1" key would container a file path as its value. How can this be done?
I have managed to get the keys/values with a dropdown select parameter working with the Active Choices Plugin, the answer was actually buried in the comments on the plugin page itself.
You simply need to use a map rather than a list when writing your groovy script to define the dropdown options. The map key is what the parameter will be set to if the user selects this option. The map value is what will be actually displayed to the user (i.e something human readable) in the dropdown list.
Here are the steps.
Ensure you have the Active Choices Plugin installed.
Open the configuration of your Jenkins job, select This project is parameterised.
Click Add Parameter and select Active Choices Parameter.
Name your parameter ENVIRONMENT and click the Groovy Script check box.
In Groovy Script enter content: return ['env1 file path value':'Test env1', 'env2 file path value':'Test env2'] For this example the user will see a dropdown with 2 options: Test env1 and Test env2. The keys: env1 file path value and env2 file path value are what the Jenkins build parameter will be set to if the option is selected. Modify these as necessary.
In this case ENVIRONMENT is the key and "Test env1", "Test env2", etc. are the possible values. Choice parameter is to restrict the possible inputs.
Based on the value of %ENVIRONMENT% you can execute multiple pathways in your batch scripts or whichever scripts you are executing

How to set Environment Variable so that it can be used in Jenkins

I am using an Environment Variable so that that it can be modified and Recipient List will consume that environment variable.
So this value is passed as a build parameter:
Followed to that I am modifying it. Just as an example:
Now I am accessing this value in the recipient list:
Unfortunately Jenkins is not able to get this new value. It is using the old value. How this behavior can be fixed?
We need to use the EnvInject Plugin. One of the features is a build step that allows you to "inject" parameters into the build job from a settings file.
Create a property for the email list in the env.properties file:
echo "email_list=`dummy#test.com`"> env.properties
It will create the properties file in the job workspace directory.
env.properties
In Recipient list access this variable using the following:
"$email_list"

Resources