How to set assembly version to Jenkins build number?

13,615

Solution 1

Cool, I found the answer myself.

basically, I had to give "1.0.0.$BUILD_NUMBER" in the "Assembly Version" field of the "Change Assembly Version" plugin

Solution 2

The previous answer about how to use "Change Assembly Version" plugin for Jenkins doesn't work. In my AssemblyInfo.cs files I usually set them up with auto incrementing version to help local dev work.

Example

AssemblyInfo.cs contains:

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]

After the Jenkins build if the version is 10 then AssemblyInfo.cs will contain:

[assembly: AssemblyVersion("1.0.10")]
[assembly: AssemblyFileVersion("1.0.10")]

The plugin is used like so to achieve the above:

Assembly Version: $BUILD_NUMBER
FileName: 
RegexPattern: Assembly(\w*)Version\("(\d+).(\d+).(\*)"\)
ReplacementPattern: Assembly$1Version("$2.$3.%s")

One other error I got whilst using the plugin was the file permission didn't allow write access. In order to fix this find the AssemblyInfo.cs and disable "Read Only".

Hope to helps anyone.

Solution 3

For others looking to update just 1 number of the version number but keep the rest of the existing version numbers you can set up the "Change Assembly Version" plug-in as follows:

Assembly Version: $BUILD_NUMBER
FileName: <project folder>/Properties/AssemblyInfo.cs
RegexPattern: Assembly(\w*)Version\("(\d+).(\d+).(\d+).(\d+)"\)
ReplacementPattern: Assembly$1Version("$2.$3.%s")

This will keep the existing, first 2 numbers already contained in the Assembly???Version settings and set the 3rd version number to the current Jenkins build number.

Example

AssemblyInfo.cs contains:

[assembly: AssemblyVersion("1.40.0.0")]
[assembly: AssemblyFileVersion("1.40.0.0")]

If the Jenkins build number is 103, then after the above settings are used by the Change Assembly Version plugin the AssemblyInfo.cs will contain:

[assembly: AssemblyVersion("1.40.103.0")]
[assembly: AssemblyFileVersion("1.40.103.0")]

Note

If you are using subversion (and likely other source control systems) and are using the "Check-out Strategy" of "Use SVN update as much as possible" you will have to change it to "Use SVN update as much as possible with svn revert before update" to ensure that the modified AssemblyInfo.cs file is reset for the next build.

Solution 4

I had to do this recently without the "Change Assembly Version" plug-in. I just used a PowerShell script instead. I'll post it here as it may offer a bit more flexibility for those that want it:

if (Test-Path env:BUILD_NUMBER) {
    Write-Host "Updating AssemblyVersion to $env:BUILD_NUMBER"

    # Get the AssemblyInfo.cs
    $assemblyInfo = Get-Content -Path .\MyShinyApplication\Properties\AssemblyInfo.cs

    # Replace last digit of AssemblyVersion
    $assemblyInfo = $assemblyInfo -replace 
        "^\[assembly: AssemblyVersion\(`"([0-9]+)\.([0-9]+)\.([0-9]+)\.[0-9]+`"\)]", 
        ('[assembly: AssemblyVersion("$1.$2.$3.' + $env:BUILD_NUMBER + '")]')
    Write-Host  ($assemblyInfo -match '^\[assembly: AssemblyVersion')
        
    # Replace last digit of AssemblyFileVersion
    $assemblyInfo = $assemblyInfo -replace 
        "^\[assembly: AssemblyFileVersion\(`"([0-9]+)\.([0-9]+)\.([0-9]+)\.[0-9]+`"\)]", 
        ('[assembly: AssemblyFileVersion("$1.$2.$3.' + $env:BUILD_NUMBER + '")]')
    Write-Host  ($assemblyInfo -match '^\[assembly: AssemblyFileVersion')
        
    $assemblyInfo | Set-Content -Path .\MyShinyApplication\Properties\AssemblyInfo.cs -Encoding UTF8
} else {
    Write-Warning "BUILD_NUMBER is not set."
}
Share:
13,615
Nirman
Author by

Nirman

Updated on July 28, 2022

Comments

  • Nirman
    Nirman over 1 year

    I am using "Change Assembly Version" plug-in in Jenkins to update all AssemblyInfo.cs files of my ASP.NET MVC project to apply version number during build process. If I set the "Assembly Version" value to a hard-coded one, this works very well.

    But my requirement is different - I would want to use a build number in the version number. For example, "1.1.0.25", where 25 is the build number and auto-generated by Jenkins. In short, the versions should be like "1.1.0.<>"

    I could do this in TFS build process using TFS environment variables, I am new in Jenkins, and not sure how can we achieve this in Jenkins. Following is a screenshot of "Change Assembly Version" plug-in from Jenkins for your quick reference:

    enter image description here

    Thanks in advance