1

Our yml-definition files for an Azure-pipeline start with

name: $(Build.DefinitionName)_$(SourceBranchName)_$(rev:rrrrr)

With that, we get very long build names that have negative consequences for display in build result pages. Therefore we would like to shorten the $(SourceBranchName) to the e.g. first 20 characters.

Is there a way of doing that?

riQQ
  • 9,878
  • 7
  • 49
  • 66
frochi42
  • 71
  • 1
  • 3

1 Answers1

0

we would like to shorten the $(SourceBranchName) to the e.g. first 20 characters.

You could try to use command line\powershell script to split long string into short.

steps:
- script: |
   echo $(Build.SourceBranchName)
   
   set  TestVar=$(Build.SourceBranchName)
   
   set MyCustomVar= %TestVar:~0,20%
   
   echo %MyCustomVar%
   
   echo ##vso[task.setvariable variable=CustomVar]%MyCustomVar%
   
  displayName: 'Get the first 20 character versions of Build.SourceBranchName'

Then we could get the short string of the SourceBranchName.

Generally, SourceBranchName is different from SourceVersion, and it is generally not a very long string. If your SourceBranchName is indeed a very long string, then the above method will help you.

You could check my previous thread for some more details.

Note: If you want to shorten your build names, we need update the build number for current build through Logging Command (e.g. Write-Host "##vso[build.updatebuildnumber]buildnumber"):

Write-Host "##vso[build.updatebuildnumber]$(Build.DefinitionName).$(CustomVar).$(rev:rrrrr)"

But the $(rev:rrrrr) could not be available in the task, so we have to include $(rev:rrrrr) in default build number format (Options), for example: $(date:yyyyMMdd)-$(rev:rrrrr). And Get current build number from pre-defined variable (Build.BuildNumber/BUILD_BUILDNUMBER), then we parse the value of $(rev:rrrrr).

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • I have a nicely encapsulated task called Transform Variable with a substring option: https://marketplace.visualstudio.com/items?itemName=jessehouwing.jessehouwing-vsts-variable-tasks. There's also a Regex Search & Replace option. – jessehouwing Nov 18 '20 at 09:55