Search code examples
azure-devopsazure-pipelinescicd

Azure DevOps Pipeline - Add latest git tag to Build.BuildNumber


Is it possible to include the latest git tag in the Build.BuildNumber variable? You can read the latest git tag with git describe --abbrev=0 and assign it to a new variable. But can I use it in the build numbe's definiton then?


Solution

  • Your command to get latest tag failed for me. I believe it is because in cases when you do not have a tag assigned on the latest commit you get an error.

    You can use instead to get the latest tag.

    $tags =  git tag | sort -V
    

    Given that you have the latest tag, you can then update build number with the following power shell.

    - task: PowerShell@2
      displayName: update build number
      inputs:
        targetType: 'inline'
        script: |
          $tags =  git tag | sort -V
          Write-Host latest tag is:$tags[0]
          Write-Host "Updating buildnumber..."
          $buildnumber = -join($tags[0],"_","$(Build.BuildNumber)")
          Write-Host "##vso[build.updatebuildnumber]$buildnumber"
    

    Result:

    enter image description here