DEV Community

Cover image for Using counters in Azure DevOps pipeline to increment assembly version numbers
Venkatesan Rethinam
Venkatesan Rethinam

Posted on

Using counters in Azure DevOps pipeline to increment assembly version numbers

There are many pipeline task extensions in the Azure Marketplace which can be installed directly to your DevOps instance. But for funny 😁 reasons, I want to do this myself where I ended up using the counter function and found it satisfied.

I hope you also will find this helpful 🎉🎉🎉.

Counter function maintains a seed value that should be variable in your pipeline and will increment it on each pipeline run.

So, here is my complete pipeline yml file before going further,

name: '$(Build.DefinitionName)_$(SourceBranchName)_$(Year:yyyy).$(Month).$(DayOfMonth).$(revision)'

trigger:
    branches:
        include:
            - master
            - develop    

pool:
  vmImage: 'windows-latest'

variables:
  solution: '*/.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'  
  buildNumber: 'Will be set dynamically'
  revision: $[counter(format('{0:dd}', pipeline.startTime), 1)]

steps:
- task: PowerShell@2
  displayName: 'Preparing Build Number'
  inputs:
    targetType: 'inline'
    script: |
      $currentDate = $(Get-Date)
      $year = $currentDate.Year
      $month = $currentDate.Month
      $day = $currentDate.Day
      Write-Host $currentDate
      Write-Host $day
      Write-Host $env:revision
      Write-Host "##vso[task.setvariable variable=buildNumber]$year.$month.$day.$env:revision"

- task: UseDotNet@2
  displayName: 'Use .NET 6'
  inputs:
    packageType: 'sdk'
    version: '6.0.x'
    includePreviewVersions: true

- task: NuGetToolInstaller@1
  displayName: 'Update Nuget'
  inputs:
    checkLatest: true

- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: config
    nugetConfigPath: nuget.config

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    projects: 'ProjectName.csproj'
    arguments: '--configuration $(buildConfiguration) /p:AssemblyVersion=$(buildNumber)'

- task: DotNetCoreCLI@2
  displayName: Publish
  inputs:
    command: publish
    publishWebProjects: false
    projects: 'ProjectName.csproj'
    arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory) /p:Version=$(buildNumber)'
    zipAfterPublish: True

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
  condition: succeededOrFailed()
Enter fullscreen mode Exit fullscreen mode

We will focus only on the variables section and the PowerShell task in this blog.

In the variables section, I have added a variable revision that is set from the counter function.

revision: $[counter(format('{0:dd}', pipeline.startTime), 1)]

This counter function is using startTime of the pipeline in the format of day number as its seed. The counter starts from 1.

On each pipeline run, the counter will check for changes in the day number in the pipeline.startTime, and if it's new, the counter will reset to the default value which is 1.

As of now, we have achieved a revision number that gets incremented on each pipeline run on the same day and also resets to 1 on the next day.

Now, in the PowerShell task, we are going to use this revision to form a build number for our assembly.

Write-Host "##vso[task.setvariable variable=buildNumber]$year.$month.$day.$env:revision"

We are trying to achieve a build number in this format - yyyy.mm.dd.revision => 2021.12.26.13

In the above Write-Host command, we are not printing any values to the output console, instead, we are using the logging command #vso to set the variable buildNumber with our required value.

Finally, in the build and publish tasks, I passed the buildNumber to the /p:AssemblyVersion and /p:Version properties respectively.

You can write your own logic to set the buildNumber using the counter function and achieve build numbers in any format you want.

Follow me on the below places for more of my blog posts,

dev.to

hashnode

Medium

Top comments (0)