Azure Pipeline - Increment build number and display in web app
Asked Answered
S

4

8

I have the following simple build pipeline working in Azure DevOps with releases deployed to a staging slot.

enter image description here

I would like to have a build revision/version string and that is auto-incremented. I then want to display this in my web app so I can tell which version of the software is in production.

Currently I display the version string from the .csproj file. Something like this in a

<Version>1.1.4.7</Version>

And then displayed on a web page using the following code:

Version: @typeof(Startup).Assembly.GetName().Version.ToString()

If I can update the existing version string that would be great but I'm open to changing to whatever's easiest to integrate in the CI process.

Splenectomy answered 12/12, 2018 at 3:1 Comment(0)
P
7

Versioning has been simplified in the .Net Core world.

Edit your csproj and modify it as follows:

<PropertyGroup>
  <Version Condition=" '$(BUILD_BUILDNUMBER)' == '' ">1.0.0.0</Version>
  <Version Condition=" '$(BUILD_BUILDNUMBER)' != '' ">$(BUILD_BUILDNUMBER)</Version>
</PropertyGroup>

If your file doesn’t have a version node, add the above.

The above setup will mean debugging locally will give you a version of 1.0.0.0, and in the event, you build in a non-Azure DevOps environment you will also end up with a 1.0.0.0 version. $(BUILD_BUILDNUMBER) is an environment variable set by Team Build and which will be updated at build time by VSTS or TFS.

The .Net version needs to be in the format [major].[minor].[build].[revision] with each segment being a number between 0 and 65000. You can configure the build number format in the Options tab, see here more info about the formatting. See here for helpful steps on configuring the build.

Precis answered 12/12, 2018 at 7:59 Comment(3)
I get the following error from the Build pipeline when I added those lines: Error CS7034: The specified version string does not conform to the required format - major[.minor[.build[.revision]]]Splenectomy
The default of $(date:yyyyMMdd)$(rev:.r)Splenectomy
This blog helped close the loop. The build number needs to be in that 0.0.0.0 format. blog.siliconvalve.com/2018/02/07/… Once I changed that the assembly version is updated and it displays on the web. ThanksSplenectomy
F
2

I am using ADO pipeline variables and the counter function like this enter image description here

The patch variable is $[counter(format('{0}.{1}',variables['Major'], variables['Minor']),0)]

Then I put all of these values directly into a variable as a powershell task.

  - task: InlinePowershell@1
    displayName: Create Version Number
    inputs:
      Script: |
        param($major, $minor, $patch)
        $bv = "$major.$minor.$patch"
        Write-Host "##vso[task.setvariable variable=buildVersion]$bv"
        Write-Host "Version of net App : $bv"
    ScriptArguments: '-major $(Major) -minor $(Minor) -patch $(Patch)'

Now when I do the .net publish I just pass the version

  - task: DotNetCoreCLI@2
    displayName: Publish Application
    inputs:
      command: 'publish'
      publishWebProjects: false
      projects: '**/MyAPIProject.csproj'
      arguments: '-r $(buildPlatform) -o $(Build.BinariesDirectory) -c $(buildConfiguration) --self-contained true /p:Version=$(buildVersion)'
      zipAfterPublish: false
      modifyOutputPath: false

What is really nice about this, is the patch gets seeded at zero, then every follow up build will auto-increment.

Fe answered 26/10, 2021 at 19:42 Comment(1)
Hi, I have probable misconfigured it, I have this error: param : The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At D:\a_temp\7ae6f5b6-3760-43e0-89b2-b1e6de300eb0.ps1:4 char:1 + param($major, $minor, $patch) Any idea, how to fix it?Gallaher
G
0

You should Use Azure DevOps Pipelines Release (a.k.a. Azure Pipelines release) instead of Azure DevOps Pipelines build (a.k.a. Azure Pipelines build). Azure Pipelines release by default will auto-increment your releases.

Azure Pipelines build has no automatic version numbering by default. Because increasing version after it should be done at release stage, because build should only concerns the continuous integrations, not to be used as versioning a build to be released.

This is the official docs of Azure Pipelines release on auto-increment your release: https://learn.microsoft.com/en-us/azure/devops/pipelines/release/?view=vsts#numbering

Gigahertz answered 12/12, 2018 at 4:43 Comment(2)
Ok but how do I access that release name in my web app so I can display it on a page?Splenectomy
The quickest way to do this is by using the badge of your generated release version, or you can simply check your executable's or DLL version of your app.Gigahertz
C
0

In a .NET Core (.NET 6.0 in particular) app I have set the following code in .csproj:

<PropertyGroup>
  <AssemblyVersion>1.0.0.0</AssemblyVersion>
</PropertyGroup>

Then in Azure DevOps I have added the following powershell script task to set version on my build.

Note that I am using the below convention: Major -> Phase/Product cycle. I am using a variable/parameter to pass this value in the pipeline. Minor -> Sprint number. I am using a variable/parameter to pass this value in the pipeline. Revision -> Max limit for this number is 65000 and therefore cannot use Build.BuildNumber. I am using (Date:YY)(DayOfYear). Build -> Taking the second part of the Build.BuildNumber (increment of the day)

- task: PowerShell@2
  displayName: 'Edit AssemblyInfo'
  inputs:
    targetType: 'inline'
    script: |
  
      $Major = "$(Major)"
      Write-Host "Major: $Major"
  
      $Minor = "$(Minor)"
      Write-Host "Minor: $Minor"
  
      $DayOfYear = (Get-Date).DayOfYear      
      $Year = (Get-Date –format yy)
      $Revision = '' + $Year + $DayOfYear
      Write-Host "Revision: $Revision"
  
      $BuildNumber = "$(Build.BuildNumber)"
      Write-Host "BuildNumber: $BuildNumber"
  
      $Build = $BuildNumber.Split('.')[1]
      Write-Host "Build: $Build"
  
      $pattern = '<AssemblyVersion>(.*)</AssemblyVersion>'
  
      Write-Host "Will search in paths.."
  
      $csProjFiles = Get-ChildItem .\*.csproj -Recurse
  
      Write-Host "CsProjFiles found are: "
      Write-Host "$csProjFiles"
  
      foreach ($file in $csProjFiles)
      {
        (Get-Content $file.PSPath) | ForEach-Object{
            if($_ -match $pattern){
                # We have found the matching line
                # Edit the version number and put back.
                Write-Host "Matched $file"
                $fileVersion = [version]$matches[1]
                $newVersion = "{0}.{1}.{2}.{3}" -f $Major, $Minor, $Revision, $Build 
                '<AssemblyVersion>{0}</AssemblyVersion>' -f $newVersion
            } else {
                # Output line as is
                $_
            }
        } | Set-Content $file.PSPath
      }
Cristionna answered 15/1, 2023 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.