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

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 $

Related

Jenkins auto-prepends PATH with path to java and ant. How to disable?

In a Free-form project I use "Inject environment variables":
JAVA_HOME=/u01/jenkins/jdk1.8.0_181/jre
PATH=/u01/jenkins/jdk1.8.0_181/jre/bin:/u01/jenkins/apache-maven-3.0.5/bin:${PATH}
However, in shell scripts $PATH gets an additional prefix:
++ echo PATH=/u01/jenkins/jdk1.7.0_55/bin:\
/u01/jenkins/apache-ant-1.9.6/bin:/u01/jenkins/apache-maven-3.0.5/bin:\
/u01/jenkins/DependencyFinder-1.2.1-beta4/bin:\
/bin:/u01/jenkins/fly:/u01/jenkins/jdk1.7.0_55/bin:\
/u01/jenkins/jdk1.8.0_181/jre/bin:<the-original-path>
How to find what's causing it and finally have my java 8 in path?
Upd: all entries except /u01/jenkins/jdk1.7.0_55/bin were being added by jenkins_shell script. This is now fixed. But I still don't know who's adding the first entry - path to java.
If a JDK is configured in Manage Jenkins -> Global Tool Configuration then a global environment variable is created: PATH+JDK=/u01/jenkins/jdk1.7.0_55/bin and right before the execution of shell scripts Jenkins prepends PATH with PATH+JDK (actually, any variable that starts with "PATH+"
https://github.com/jenkinsci/jenkins/blob/c904989067aa699ea63d043c44f6ea905cb9c5d5/core/src/main/java/hudson/EnvVars.java#L144
The workaround is to inject an empty PATH+JDK= variable to disable the prepending completely or to inject PATH+JDK=/path/to/proper/jdk.
Additionally, since EnvVars extends TreeMap you can inject another variable: PATH+ZZZ=/path/to/something which is prepended later than PATH+JDK because Jenkins iterates such vars in alphabetic order.
Finally, it's possible to configure a dummy JDK without executables and select this dummy JDK in a dropdown in the Job.

How to set Global Environment Variables from a file in 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}"
}

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 declare a global variable in Jenkins and use it in an MSBuild task within each individual project

I am converting our CI platform from CruiseControl to Jenkins, and can't seem to figure something out that seems like it should be relatively simple to do (Disclaimer - I'm no CI or build automation expert, but this was dumped into my lap and I find it interesting)
In CruiseControl, I am able to declare variables like this:
<cb:define rootdir="J:\SOURCES\" />
<cb:define logdir="J:\SOURCES\buildlogs" />
<cb:define iisdir="J:\IIS\" />
<cb:define artifacts="artifacts\" />
Then use them as part of an MSBuild task
<msbuild>
<executable>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</executable>
<workingDirectory>$(rootdir)$(ProjectName)</workingDirectory>
<projectFile>$(ProjectName).sln</projectFile>
<buildArgs>/p:BuildDate="1";OutDir="$(iisdir)$(ProjectName)\bin\\";WebProjectOutputDir="$(iisdir)$(ProjectName)\\"</buildArgs>
<targets>Rebuild;$(ProjectName)</targets>
<timeout>180</timeout>
<logger>C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MsBuild.dll</logger>
</msbuild>
If the root or IIS directories change, it can easily be applied to all projects at once. We have ~60 projects setup, so doing this project by project would be very time consuming. Migrating this to Jenkins, the MSBuild command line arguments now look like this (partial sample but includes what is relevant):
OutDir="J:\IIS\ProjectName\bin\\";WebProjectOutputDir="J:\IIS\ProjectName\\"
The IIS directory is hard coded. I need that to be something more like this:
OutDir="${IIS_DIR}\ProjectName\bin\\";WebProjectOutputDir="${ITEM_ROOTDIR}\ProjectName\\"
Is there a way to do that? I tried the configuration slicing plugin, which is useful, but doesn't fit this need from what I see
You can do this with built-in Jenkins' functionality:
Then you need to expand your variable. This, actually, depends on where you would use it.
For example: %MSBuild% and %IIS_DIR% for "Execute windows batch command" build step. Other build steps (and plugins) may use it differently.
For global variables, you need EnvInject plugin. This allows you (among other things) to setup variables at the Global (node) level, at job level or as a step. You can set variables directly, or from properties file, or from scripts.
Once set, the variables are available as environment variables to the rest of Jenkins and its steps (within scope).
For passing arguments to MSBuild, when you configure an MSBuild step, there is an option to pass "Command line arguments" in the format /p:Param=Value.
The "value" could be an environment variable. On Windows environment you would reference it as %myvar%
So, if you configure a global GLOBAL_IIS_DIR=C:\path\to\IIS using EnvInject, you can then reference it on command line with /p:IIS_DIR=%GLOBAL_IIS_DIR%

Jenkins deploy plugin - pass parameters from properties file

I'm using Jenkins and deploy plugin, with which I'm deploying to tomcat server. How can I pass parameters from properties file to this deploy plugin?
for example I want to pass my property app.server.url to Tomcat URL field.
I tried to pass $app.server.url also %app.server.url%, but that doesn't work.
I am assuming you have a properties file in the format app.server.url=somevalue
You then can inject these properties into the Jenkins environment using EnvInjec Plugin. Use Inject environment variables for your job build step, and just specify your file path (leave the content field blank). After that, you can reference it like this $app.server.url for *nix, or %app.server.url% for Windows. However this only works on the shell level. On the plugin properties level, a lot of plugins are expecting properties only in *nix-style format.
The further problem is that *nix-style variables do not allow dots .. So $app.server.url is not valid on *nix. I do not know if it will work on plugin-level on Windows though.
So after setting up EnvInject plugin, try the following two:
Try using $app.server.url in your deploy plugin.
Else change the property file to something like app_server_url=somevalue, and then try $app_server_url in deploy plugin.

Resources