variablesyamlexpressionazure-pipelinesbuild-numbers

Devops YAML - set build name using expression


In a yaml build, is it possible to set the build name using an expression; it would be advantageous if I could match the release pipeline with the actual build id.

Example:

trigger:  
- master

variables:  
    major: 2  
    minor: 3  
    offset: 2000  
    bid: $[$build.BuildID -as [int] + $offset]

name: "$(major).$(minor).$(bid)"


Solution

  • You can use the UpdateBuildNumber command to dynamically set the name as part of a bash or PowerShell script.

    For more details you can see this blog post, but the gist of it is something like this:

    name: 'Set dynamically below in a task'
    
    variables:  
        major: 2  
        minor: 3  
        offset: 2000
    
    steps:
    - task: PowerShell@2
      displayName: Set the name of the build (i.e. the Build.BuildNumber)
      inputs:
        targetType: 'inline'
        script: |
          [int] $buildIdWithOffset = ([int] $(Build.BuildId)) + ([int] $(offset))
          [string] $buildName = "$(major).$(minor).$buildIdWithOffset"
          Write-Host "Setting the name of the build to '$buildName'."
          Write-Host "##vso[build.updatebuildnumber]$buildName"