Home > Back-end >  Set build number format in YAML pipelines
Set build number format in YAML pipelines

Time:09-14

I have a property group in my .csproj file with major, minor and patch version, which is to define the NuGet package version. It looks like this:

<PropertyGroup>
    <MajorVersionOfNuget>1</MajorVersionOfNuget>
    <MinorVersionOfNuget>1</MinorVersionOfNuget>
    <PatchVersionOfNuget>0</PatchVersionOfNuget>
</PropertyGroup>

In our old Azure Pipelines (classic) these properties are used to set a build number format, under options. Like such:

Screenshot from options in a classic Azure Pipeline

How do I do this, in the YAML version of Azure Pipelines?

CodePudding user response:

You can customize how your pipeline runs are numbered. The default value for run number is $(Date:yyyyMMdd).$(Rev:r).

In YAML, the build number is defined by the property called name and is at the root level of a pipeline. If not specified, your run is given a unique integer as its name.

name: $(TeamProject)_$(Build.DefinitionName)_$(SourceBranchName)_$(Date:yyyyMMdd)$(Rev:.r)

steps:
  - script: echo '$(Build.BuildNumber)' # outputs customized build number like project_def_master_20200828.1

(source)

CodePudding user response:

From your description, you need to use the value in .csproj file to set the build number.

In Azure DevOps Pipeline, the value in .csproj file can not be used as Pipeline variable to set the build number directly.

To meet your requirement, you need to use script to get the value in csproj and then you can use logging command: UpdateBuildNumber to update the build number.

Here is a PowerShell example:

steps:
- powershell: |
   $xml = [Xml] (Get-Content $(build.sourcesdirectory)\xx.csproj(Filepath))
   $MajorVersionOfNuget=  $xml.Project.PropertyGroup.MajorVersionOfNuget
   $MinorVersionOfNuget=  $xml.Project.PropertyGroup.MinorVersionOfNuget
   $PatchVersionOfNuget=  $xml.Project.PropertyGroup.PatchVersionOfNuget
   
   echo $MajorVersionOfNuget
   
   echo $MinorVersionOfNuget
   
   echo $PatchVersionOfNuget
   
   echo "##vso[build.updatebuildnumber]$MajorVersionOfNuget.$MinorVersionOfNuget.$PatchVersionOfNuget"
  displayName: 'PowerShell Script'
  • Related