Azure-pipelines-agent: env:BUILD_SOURCEBRANCHNAME variable is populated wrong when the branch name contains slash ('/'), e.g.: feature/my-new-feature

Created on 19 Jun 2018  路  9Comments  路  Source: microsoft/azure-pipelines-agent

Have you tried trouble shooting?

Yes

Agent Version and Platform

Version of your agent? 2.102.0/2.100.1/...
Hosted agent
OS of the machine running the agent? OSX/Windows/Linux/...
Windows

VSTS Type and Version

VisualStudio.com

If VisualStudio.com, what is your account name? http://account.visualstudio.com
slb1-swt

What's not working?

env:BUILD_SOURCEBRANCHNAME envionment variable is populated wrong when the branch name contains slash ('/'), ex: feature/my-new-feature

Repro steps:

  1. create a branch named "feature/my-feature"
  2. run a build from this branch with PowerShell script: "echo $env:BUILD_SOURCEBRANCHNAME

Expected result:
"feature/my-feature"

Actual result:
"my-feature"

Agent and Worker's Diagnostic Logs

N/A

Build

All 9 comments

Not part of the team, but if you view the build variables link you can see you don't want to use Build.SourceBranchName. You want to use Build.SourceBranch and remove the "ref/" part of the SourceBranch. This is so people can use branch folders and get the actual name.

@PaulGiletich the variable is set on the service side before it sends to the agent, i would suggest you post this issue to https://developercommunity.visualstudio.com/spaces/21/index.html
from what i know, the current behavior is "by design", since we can't change existing variable easily (people may rely on this weird variable), what we can do is add a new variable to have the right information, like BUILD_SOURCEBRANCHFULLNAME

Closing. There is a recommended alternative.

What is the recommended alternative?

I want to add branch name like (feature/123) to build name. Me adding a custom variable in tasks is too late.

@moswald

Build.SourceBranchName will be the last path segment of the ref.
Build.SourceBranch is the entire branch.

See https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=vsts&tabs=batch#build-variables for full details.

@moswald thanks for responding. Unfortunately I think its a bit confusing to add refs/heads/feature/abc to my build name since developers named the branch feature/abc at the start.

However, I can understand why the team might be reluctant to add an extra Build.SourceBranchFullName or something similar to have the name without those git added prefixes. I still can't find the right solution though.

@harouny When you say "build name", do you mean the build number? You can use the updatebuildnumber command in a powershell script, and using powershell you should be able to easily strip the leading refs/heads/.

@moswald Yes build number. Thanks for the workaround!

@moswald this is not even remotely acceptable. We need hacks and scripts to get the branch name. Just the basic branch name. This should not be a thing, this a straight up bug, watching you fumble and argue for why it is somehow ok is just depressing. What are you guys doing?? This is a commercial service we pay for! Its not ok for it have stupid massive problems like this 3 year old issue! The hell with this product.

Am reminded of that line from Glen Garry Glen Ross that starts "What you're for, is to help us."

Was this page helpful?
0 / 5 - 0 ratings