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