0

I am currently building a multibranch pipeline. I am trying to build a docker image and tagging it with the branch name which will always be "feature/XXX-111". However, when i get the branch name using the $branch_name env variable the docker build with tag -t command doesnt like the "/" in the "feature/XXX-111" branch name. So i was wondering if it was possible to only get the "XXX-111" part of the branch name and dropping the "feature/". Any help will be appreciated.

Thanks!

giturr14
  • 33
  • 9

2 Answers2

3

This works:

def branch_name = 'feature/XXX-111'
def regex_to_search = 'feature/([\\w-_]*)'
def matcher = branch_name =~ regex_to_search
if (matcher.find()) {  
    println matcher.group(1)
}

Output:

XXX-111
MaratC
  • 6,418
  • 2
  • 20
  • 27
  • thank you but the branch name will be changing constantly, so that is why i am using the $GIT_BRANCH variable to pull in the branch name. So i dont think i can set branch_name = $GIT_BRANCH ?right? – giturr14 Jul 06 '20 at 14:52
  • Start with `def branch_name = GIT_BRANCH` and move from there. – MaratC Jul 06 '20 at 16:21
1

You can use groovy split function -

def branchName = 'feature/XXX-111'
def newBranchName = branchName.split('/')[1]
println(newBranchName)
​

Output:

XXX-111
Pankaj Saini
  • 1,493
  • 8
  • 13
  • thank you but the branch name will be changing constantly, so that is why i am using the $GIT_BRANCH variable to pull in the branch name. So i dont think i can set branch_name = $GIT_BRANCH ?right? – giturr14 Jul 06 '20 at 14:53
  • You can set for sure – Pankaj Saini Jul 06 '20 at 15:27