1

I'm having trouble in an azure devops pipeline.

$(Build.SourceBranch) outputs something like refs/heads/xyz

I'd like to use something like JavaScript's .split('/') to output an array at the slashes.

Meaning $b = $(Build.SourceBranch).Split('/') would output

$b[0] = "refs"
$b[1] = "heads"
$b[2] = "xyz"

I'm not finding much on how I can do this easily.

2
  • If you need only the branchname you could use $(Build.SourceBranchName) Commented Apr 29, 2022 at 8:25
  • Have you tried using the YAML function from this link (developercommunity.visualstudio.com/t/…) ? split(variables.str, ',') but changing the symbol to / as you want it? Commented Jan 9, 2023 at 17:07

1 Answer 1

4

In Azure Pipeline, you can use PowerShell script to split string.

From your script, you need to add double quotes around the variable:$(Build.SourceBranch) to make it a string first and then split it.

Here is an example:

- powershell: |
   
   $b = "$(Build.SourceBranch)".Split("/")
   
   $b[0]
   $b[1]
   $b[2]
  displayName: 'PowerShell Script'

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

You should be able to export values using ##vso[task.setvariable variable=B_0]%$b[0]% - you might need to play around with it, but should get you most of the way there. You can then reference the variable later user $(B_0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.