How to declare a global variable in Jenkins and use it in the MSBuild task in each individual project

I am converting our CI platform from CruiseControl to Jenkins and cannot imagine something that looks like it should be relatively easy to do (Disclaimer - I am not an expert on CI automation or assembly, but it has been dropped on my knees, and I find it interesting)

In CruiseControl, I can 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 your 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, they can be easily applied to all projects at once. We have ~ 60 projects, so this project project will be very time-consuming. Migrating this to Jenkins, the MSBuild command line arguments now look like this (partial fetch, but includes what matters):

 OutDir="J:\IIS\ProjectName\bin\\";WebProjectOutputDir="J:\IIS\ProjectName\\" 

The IIS directory is hard-coded. I need it to be something like this:

 OutDir="${IIS_DIR}\ProjectName\bin\\";WebProjectOutputDir="${ITEM_ROOTDIR}\ProjectName\\" 

Is there any way to do this? I tried the section settings plugin, which is useful, but does not meet this need from what I see.

+7
build-automation jenkins msbuild
source share
2 answers

For global variables, you need the EnvInject plugin . This allows you (among other things) to configure variables at the Global (node) level, work level, or as a step. You can set variables directly either from a properties file or from scripts.

After installation, the variables are available as environment variables for the rest of Jenkins and its steps (within the scope).

To pass arguments to MSBuild when setting up the MSBuild step, there is an option to pass "command line arguments" in the format /p:Param=Value.

"Value" may be an environment variable. In a Windows environment, you refer to it as% myvar%

So, if you configure the global GLOBAL_IIS_DIR=C:\path\to\IIS using EnvInject, you can reference it on the command line using /p:IIS_DIR=%GLOBAL_IIS_DIR%

+7
source share

You can do this with Jenkins built-in functionality:

enter image description here

Then you need to expand your variable. It depends on where you use it.

For example: %MSBuild% and %IIS_DIR% for the build step β€œ %IIS_DIR% Windows batch command”. Other build steps (and plugins) can use it in different ways.

+19
source share

All Articles