Azure DevOps

Auto Versioning Nuget Packages in Azure DevOps Pipelines

I have been using the 'byPrereleaseNumber' versioning strategy in my Azure DevOps pipelines for a project's libraries and we are now moving a number of the libraries to 1.0

Investigating the options available I came accross the 'Counters' feature categorised under 'Expressions' within the Azure DevOps documentation site

This useful feature allows adding arbitrary counters to your YAML files, however includes some nice features, such as incrementing until another variable is changed then resetting. I want to manually bump Major and Minor numbers and have patch auto increment.

To enable this in my YAML pipeline I just need to define varaibles for my major and minor numbers, then implement a counter for the patch number starting at 0, that will reset when then minor number changes.

An example YAML now looks like


pool:
  vmImage: 'VS2017-Win2016'

variables:
  projectName: 'SampleLib'
  buildConfiguration: 'Release'
  major: 1
  minor: 0
  patch: $[counter(variables['minor'], 0)] #this will reset when we bump minor
  NugetVersion: $(major).$(minor).$(patch)

steps:

## removed for breivity

- task: DotNetCoreCLI@2
  displayName: 'dotnet pack'
  inputs:
    command: pack
    nobuild: true
    projects: '$(projectName)/$(projectName).csproj'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'NugetVersion'

## removed for breivity

When your pipeline runs it should now create package with the version 1.0.1 and the patch version will increment until you modify the minor number, to say 1. The next build will then generate a package with the version number 1.1.0