How can I update AssemblyVersion number in AssemblyInfo.cs with build number of Azure DevOps via PowerShell?

Viewed 12166

I want to update my version number in AssemblyInfo.cs with the build number of Azure DevOps (VSTS).

Does anyone know how I can do that through PowerShell?

2 Answers

The best way to do it is with Assembly Info Extension, and use the variable $(Build.BuildNumber) in the version field.

But if you want to use your own PowerShell script you can do it with this script:

$buildNumber = "$env:Build_BuildNumber"
$pattern = '\[assembly: AssemblyVersion\("(.*)"\)\]'
$AssemblyFiles = Get-ChildItem . AssemblyInfo.cs -rec

foreach ($file in $AssemblyFiles)
{

(Get-Content $file.PSPath) | ForEach-Object{
    if($_ -match $pattern){
        '[assembly: AssemblyVersion("{0}")]' -f $buildNumber
    } else {
        # Output line as is
        $_
    }
} | Set-Content $file.PSPath

}

I recommend installing the GitVersionTask nuget package - this will generate a version number based on the number of commits since your previous build automatically, and update AssemblyVersion and the BuildNumber for you.

https://github.com/GitTools/GitVersion

Related