3

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)"

4 Answers 4

13

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"
1
  • Thanks @MouthOfMadness, I'm glad it worked. Would you mind marking this as the answer please :) It seems you upvoted it (or somebody did), but did not mark it as the answer. Thanks!
    – deadlydog
    May 13, 2020 at 17:14
4

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

1
  • Thanks for the link to the MSDN reference. I finally found the crucial info that ${{ ... }} is evaluated at compile time, so can only use static vars, but $[ ... ] is evaluated at runtime, so can use all the normal $( ... ) vars. Sprinkling this on your sample above suddenly started working. Jan 5, 2022 at 18:46
0

The example won't work for syntax error.

You can check the MSDN doc Expressions and there's no mathematical operations like summation provided.

A better work around would be quiting the offset:

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  major: 2  
  minor: 3  
  #offset: 2000  

name: $(major).$(minor).$(build.BuildID)

This will return you the name as 2.3.101 for example.

6
  • Unfortunately, I require the offset to publish to the Apple and Google store. It's difficult to trace the build through the system when you've got a different buildId vs a publish ID. Jan 20, 2020 at 17:21
  • Is it possible that you can use counter expression. Here is a similar thread where you can check for more information. Jan 21, 2020 at 9:41
  • No. I attempted a similar strategy but the task to update a custom variable happens to late in the process (in steps rather than declaration) to Name the build. Jan 22, 2020 at 19:12
  • Yes. The build name is always generated before any of its tasks start running. As i mentioned, this seems not possible at current time. Jan 24, 2020 at 9:35
  • Either an declaration expression or a pre-task would have been ideal. Jan 27, 2020 at 19:14
0

If you want the use the generated expression to automatically increment the counter function (which can only be used as part of a variable declaration) for each unique value, you can use the following:

jobs:
- job: VERSION_BUILD
  displayName: Version Build
  variables:
    - name: pipeline_start_date
      value: $[format('{0:yyyy}-{0:MM}-{0:dd}', pipeline.startTime)]
  steps:
  - checkout: none
  - task: PowerShell@2
    name: generate_minor_version
    displayName: Generate Minor Version
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Pipeline start date is '$(pipeline_start_date)'."
        [int] $minorVersion = [math]::floor(((New-TimeSpan -Start '2022-02-01' -End '$(pipeline_start_date)').Days) / 14) + 1
        Write-Host "Minor version is '$minorVersion'."
        Write-Host "##vso[task.setvariable variable=minor_version;isOutput=true;]$minorVersion"
- job: VERSION_BUILD_2
  displayName: Version Build 2
  dependsOn:
  - VERSION_BUILD
  variables:
    - name: minor_version
      value: $[dependencies.VERSION_BUILD.outputs['generate_minor_version.minor_version']]
    - name: major_minor_version
      value: $[format('{0}.{1}', variables['major_version'], variables['minor_version'])]
    - name: build_version
      value: $[counter(variables['major_minor_version'], 0)]
  steps:
  - checkout: none
  - task: PowerShell@2
    displayName: Update Build Number
    inputs:
      targetType: 'inline'
      script: |
        Write-Host "Major-minor version is '$(major_minor_version)'."
        Write-Host "Build version is '$(build_version)'."
        [string] $buildNumber = "$(major_version).$(minor_version).$(build_version)"
        Write-Host "Setting the name of the build to '$buildNumber'."
        Write-Host "##vso[build.updatebuildnumber]$buildNumber"

This particular script calculates the $minor_version as the number of 2 week intervals (iterations) from a given point in time. So in theory, on every other Tuesday, the pipeline will increment the minor version of the build number by 1 and reset the build version of the build number to 0, which will then automatically increment for each subsequent build in that two-week period.

1
  • That's pretty cool; my issue was centered around a developer deploying a manual value of the version to the play store that exceed the value of the build id. Example build id = 100, developer deployed = 6000; that's where I need to calculate an offset that took the build id and created a version that was: version id = offset + build id > 6000 Feb 21, 2022 at 21:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.