Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set build name (Build.BuildNumber) in an Azure DevOps Build Pipeline template?

I need to create several build definitions, which will all perform the exact same steps. What changes are only triggers and a few parameters, basically. I therefore based them all on a template that does all the heavy lifting. That works fine.

However, I need to customise the value of $(Build.BuildNumber) for several reasons, some aesthetic, some practical. According to Configure run or build numbers, all I have to do is set the name property in a YAML definition.

All my definitions should have the same name format, so I wanted to define it in the template directly. But setting a top-level name property in the template results in error message :

/templates/default-build.yml (Line: 5, Col: 1): Unexpected value 'name'

Setting the property in the individual definitions works as expected, but won't "scale" as a solution.

The individual definitions look like this:

trigger:
- master

extends:
  template: ../templates/default-build.yml
  parameters:
    solution: '**/ASolution.sln'

The template looks like this:

parameters:
- name: solution
  type: string
  default: ''

name: $(Build.DefinitionName)_$(Date:yyyyMMdd)$(Rev:.r)  # Trying to set Build.BuildNumber

variables:
  solution: ${{ parameters.solution }}
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

stages:
- stage: build_solution
  jobs:
    - job:
      steps:
        - task: VSBuild@1
          displayName: 'Build solution $(solution)'
          inputs:
            solution: $(solution)
            platform: '$(BuildPlatform)'
            configuration: '$(BuildConfiguration)'

        - task: PublishBuildArtifacts@1
          displayName: 'Publish Artifact'
          inputs:
            PathtoPublish: '$(build.artifactstagingdirectory)'
            ArtifactName: '$(Build.BuildNumber)'

Am I missing something? Or is setting this particular property in a template just not (currently) supported?

like image 677
madd0 Avatar asked Oct 16 '22 07:10

madd0


2 Answers

I would think that using a template for this is not supported, because name is a top level parameter and everything inside template is not a top level thing.

https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema?view=azure-devops&tabs=schema#pipeline

like image 54
4c74356b41 Avatar answered Oct 19 '22 02:10

4c74356b41


You can do this in a Powershell task that calls the ##vso[build.updatebuildnumber] 'command'.

https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#updatebuildnumber-override-the-automatically-generated-build-number

The task can also be defined as a template like so:

steps:

- task: PowerShell@2
  displayName: 'Create version number'
  name: 'CreateVersionNumber'
  inputs:
    targetType: 'inline'
    script: |
        $newVersion = "MyVersion $(Build.BuildId)"

        Write-Host "New version is $($newVersion)"

        [string] $buildName = "$($newVersion)"
        Write-Host "Setting the name of the build to '$buildName'."
        Write-Host "##vso[build.updatebuildnumber]$buildName"
like image 44
Rye bread Avatar answered Oct 19 '22 02:10

Rye bread