Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 292
Y.Y. Avatar asked Oct 02 '19 08:10

Y.Y.


People also ask

What is build number in Azure Devops?

BuildId) is an internal immutable ID that is also referred to as the Run ID. It is unique across the organization. 2 (The third run will be 3, and so on.) Use $(Rev:r) to ensure that every completed build has a unique name.

What is the difference between assembly version and file version?

AssemblyVersion: Specifies the version of the assembly being attributed. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource.

Where do I put assembly version?

You can set the assembly version using the AssemblyVersionAttribute. Assembly attributes are usually applied in the AssemblyInfo.


1 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

}
like image 169
Shayki Abramczyk Avatar answered Nov 15 '22 08:11

Shayki Abramczyk