Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jenkins Version Environment Variable in the MSBuild step

I'm currently using the Jenkins "Version Number Plug-In" to set an Environment Variable for build version. This works fine within jenkins but i need a way to pass this to MSBuild so the Version Number of the exe's and dll's are updated. I tried the config bellow but it does not update the builds version

Version Number Config

Build Config

like image 944
Ryan Burnham Avatar asked Sep 10 '12 04:09

Ryan Burnham


People also ask

How do I set environment variables in MSBuild?

Click on System and Security and then on System. In the left pane, click on Advanced system settings. At the very bottom of the pop up, click on Environment Variables. Edit the Path variable and append the folder's path that contains the MSBuild.exe to it (e.g., ;C:\Windows\Microsoft.NET\Framework64\v4.

How do you inject environment variables to the build process?

EnvInject We can install and use the EnvInject plugin to inject environment variables during the build startup. In the build configuration window, we select the “Inject environment variables” option in the “Add build step” combo box. We can then add the required environment variables in the properties content text box.


2 Answers

This is a MSBuild target that replaces the revision number in the file GlobalAssemblyInfo.cs with the SVN_REVISION variable from Jenkins.

We have a multi project solution where each project references the same GlobalAssemblyInfo for common information (like version). Modify this so it fits your setup.

By having the Exists condition the target executes on developer machines where the MSBuildCommunityTasks is not installed. In those cases the version number in GlobalAssemblyInfo.cs is left untouched.

<Target Name="InjectSVNRevision" Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')">
<!-- When this build is done by the Jenkins CI server; the svn revision number is set in the environment variable SVN_REVISION
     This little snippet replaces the revision number in GlobalAssemblyInfo.cs with this svn revision number
  -->
<Message Text="Injecting SVN revision number in GlobalAssemblyInfo.cs" />
<FileUpdate Files="GlobalAssemblyInfo.cs"
    Multiline="true"
    Singleline="false"
    Regex="(AssemblyVersion|AssemblyFileVersionAttribute|AssemblyFileVersion)\(&quot;([0-9]+\.[0-9]+\.[0-9]+)(\.[0-9]+)?&quot;\)"
    ReplacementText="$1(&quot;$2.$(SVN_REVISION)&quot;)" 
    Condition="$(SVN_REVISION) != '' "/>
</Target>
like image 25
estromsnes Avatar answered Oct 09 '22 07:10

estromsnes


Environment Variable Name should be written without the percent-marks ('%'), like this:

VERSION

apart from that, I think you are good to go.

May also consider changing
${BUILDS_ALL_TIME}
to
${BUILDS_ALL_TIME, XX}

(this will enforce a two-digit build-number with leading zeros)

like image 131
Gonen Avatar answered Oct 09 '22 06:10

Gonen