Using Jenkins Version Environment Variable in the MSBuild step

17,927

Solution 1

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)

Solution 2

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>

Solution 3

The easiest way I´ve found to do this is using the Change Assembly Version. Using that plugin you only need to provide the version number (or the environment variable used) and it will replace it in all the AssemblyInfo.cs files in your workspace.

Share:
17,927
Ryan Burnham
Author by

Ryan Burnham

Automated Testing Specialist and Visual Studio ALM MVP for 2012

Updated on July 20, 2022

Comments

  • Ryan Burnham
    Ryan Burnham almost 2 years

    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

  • Ryan Burnham
    Ryan Burnham over 11 years
    Yeah i figured that out, Also it seems like MSBuild.exe dosn't support /p:version by default. you need to mess around with MSBuld Extensions. I ended up just writing a console app to update the AssemblyInfo.cs before building and call it passing in the environment variable. This was quicker than messing around with complicated msbuild extensions
  • Gonen
    Gonen over 11 years
    Well, I was hoping you DID find a nice way to pass the version to the build, as we also use a tailored script for that...