Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set assembly version to Jenkins build number?

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

like image 479
Nirman Avatar asked Nov 03 '15 10:11

Nirman


People also ask

What is Jenkins build number?

BUILD_NUMBER is the current build number. You can use it in the command you execute for the job, or just use it in the script your job executes. See the Jenkins documentation for the full list of available environment variables.

Where is the version information stored of an assembly?

The version number is stored in the assembly manifest along with other identity information, including the assembly name and public key, as well as information on relationships and identities of other assemblies connected with the application.


4 Answers

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.

like image 122
steveoshima Avatar answered Oct 17 '22 03:10

steveoshima


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.

like image 28
mips Avatar answered Oct 17 '22 04:10

mips


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

like image 5
Nirman Avatar answered Oct 17 '22 05:10

Nirman


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."
}
like image 1
GrahamS Avatar answered Oct 17 '22 04:10

GrahamS