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