1

In my Jenkins pipeline, I'm trying to get this output into an array:

gitBranches  = sh (
script: 'git branch -a',
returnStdout: true).trim()

Is that possible?

1 Answer 1

1

Can be done in groovy way:

gitBranchStr  = sh (
script: 'git branch -a',
returnStdout: true)
gitBranchList = gitBranchStr.substring(gitBranchStr.lastIndexOf("*") + 1).split("\n")*.trim()
Sign up to request clarification or add additional context in comments.

1 Comment

I think it's better to first get rid of the leading * and then trim - or else you end up with a whitespace before the current branch. And for golfing you could use the spread operator instead of the collect.

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.