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)"
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"
There's yet another way to set the build number without using the scripts. The advantage of this method is that the build name is set from the beginning, unlike the script that changes the original name during the build. And the sequence number generation is cleaner (see the $(Rev:r) piece that works only in the "name" property of the pipeline.
You must use the "format" expression instead of ($Variable) syntax, because the build name is created during the Compile stage when the $(Variable) syntax does not work yet. Otherwise the variables will not be evaluated and will go into the build name as $(Variable).
name: '$(buildNumber).$(Rev:r)'
variables:
majorVersion: '1'
minorVersion: '0'
${{ if eq(variables['Build.SourceBranch'], 'refs/heads/master') }}:
buildNumber: ${{ format('{0}.{1}', variables['majorVersion'], variables['minorVersion']) }}
${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
buildNumber: ${{ format('{0}.{1}.PR-{0}', variables['majorVersion'], variables['minorVersion'], variables['System.PullRequest.PullRequestId']) }}
More about expressions here: Azure Pipelines Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With