Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 293
TechDawg270 Avatar asked May 29 '15 19:05

TechDawg270


1 Answers

You can do this with built-in Jenkins' functionality:

enter image description here

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.

like image 189
Vitalii Elenhaupt Avatar answered Sep 17 '22 04:09

Vitalii Elenhaupt