How to set Global Environment Variables from a file in Jenkins - jenkins

I am new to Jenkin pardon me for any wrong statements.
I have to set Global Environment Variables from a File(say config or txt) and I want to place that file either in my local or somewhere on the server.
I have gone through some documentations but it all says to use EnvInject Plugin which is basically helps for a specific job when we are building it.
But I want a solution where Global env variables can fetch from the file so that Configure System page can load quickly.

I'm assuming that you are looking to read properties from a specific file and inject them as environment variables to your jenkins job.
You can follow below approach:
Create property file like env.properties with below content:
testvalue='mytest'
Sample code to read the file and inject read variables as environment variables
node`{load "${WORKSPACE}\env.properties"
echo "test value: ${testvalue}"
}

Related

Jenkins/Groovy move variables out to a config file

I've been asked to move some variable from a Groovy script out into a configuration file. I'm fine using something like :-
readFile('../xx-software.cfg').split('\n').each { fileName ->
sh "wget ${theURL}${fileName}"
}
However, even though I have added xx-software.cfg into the same directory as my Groovy script it does become available for use within that groovy script.
I hope this makes sense!?
How can I move my variables out into a config file to make it easier for the application support team to make future edits without changing the code?
There are a few approaches you could use.
Firstly, file format for the configuration and how to read the data into variables. You could use Java Properties format, YAML or JSON and these are all handled by the Pipeline Utility Steps plugin with steps here. You can read the file with these steps:
readProperties
readYaml
readJSON
Next problem, how to get the file available to your pipeline so it can be read from the workspace using these steps. Possibilities are:
In source control with your pipeline code. It can be fetched with the pipeline.
In a separate source control for configuration, your pipeline will need a step to fetch it.
Use the Jenkins Config File Provider plugin. It has a step to provide a config file managed in Jenkins.
Provide it as a Custom Tool zipped archive from a binary server like Artifactory. You can use custom tool definition pipeline steps to make this available to the pipeline.
The Config File Provider option might provide any easy way to have a file that can be updated, but there won't be any version control of it.

Read TFS build variables from file

Is it possible to read a file from the git source control and set values to TFS build variables so that we can use them in other steps?
We have a file with the version info and the branch (VER_TYPE=3 is DEV) that we use to set up assembly version together with the build number
VER_MAJOR=2018
VER_MINOR=1
VER_TYPE=3
Tks in advance!
It's possible to define or modify a variable from a script, use the task.setvariable logging command.
Sets a variable in the variable service of taskcontext. The first
task can set a variable, and following tasks are able to use the
variable. The variable is exposed to the following tasks as an
environment variable.
When issecret is set to true, the value of the variable will be saved
as secret and masked out from log. Secret variables are not passed
into tasks as environment variables and must be passed as inputs.
Examples:
##vso[task.setvariable variable=testvar;]testvalue
##vso[task.setvariable variable=testvar;issecret=true;]testvalue
More details please refer Define and modify your variables in a script
You can run a script on windows agent using either a Batch script task or PowerShell script task. You just need to read the specific file in source control, download it in the workspace on the build agent. Then read the file, a way using powershell for your reference: Read file line by line in PowerShell

Use Environment variable in job description

I'm trying to set dynamically a job description with a global environment variable.
But I cannot use $MYVAR or ${MYVAR}.
Does anybody have an idea ?
Thanks !
As a temporary solution, I am using the "Anything Goes" formatter plugin.
In my job description, i'm using :
<iframe src="http:/JENKINS_URL/Docs/DocsJenkins/MY_DATA.html"></iframe>
I created an alias in IIS referencing a folder with some html pages. And i'm setting dynamicaly this pages with my global enviromnent variables.
Hope this tricks can help somebody else.
You should install Environment Injector Plugin.
After installation, go to your job configuration page,then
Build Environment ==> Inject environment variables to the build process ==> Properties Content
Define your environment with KEY=VALUE format:
Then, this variable can be accessed by surrounded by ${}

How to edit a file with environment variables in jenkins

I'm bit new to jenkins. What I want is to edit config files with environment variables of jenkins. I tried ant scripts, Nant plugin but all ended up with errors. If anyone knows something about this please help. For example lets think we want to replace word "hello" in C:\files\test.txt to "bye". word "bye" should be taken from a environment variable of jenkins. Thanks
Best is to utilize Jenkins EnvInject plugin. With that you can have a build step to manipulate/generate your properties, and then you can add another build step to load these properties into project environment variables.
See this stackoverflow question for a more detailed example.

Jenkins EnvInject from a file which is a Job parameter by itself

I've got a Job Parameter, for example PROPERTIES_FILE.
I want to inject all Env Variables from that file into my job session, using the EnvInject plugin.
Is there a way to do that?
I managed to inject variables
only for a hard coded file path, and not from a parameter.
EnvFile plugin (not EnvInject) seems to support variables in the properties file path (as it uses $WORKSPACE in the example).
Also, if you are on Windows, maybe all you need for EnvInject plugin to work is to use $param for variable, not %param%. While the scripts need %% notation, inside Jenkins still uses $

Resources