简体   繁体   中英

Azure Pipeline - Increment build number and display in web app

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

在此处输入图像描述

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.

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.

I am using ADO pipeline variables and the counter function like this在此处输入图片说明

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.

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
      }

You should Use Azure DevOps Pipelines Release (aka Azure Pipelines release) instead of Azure DevOps Pipelines build (aka 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://docs.microsoft.com/en-us/azure/devops/pipelines/release/?view=vsts#numbering

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM