giturr14
giturr14

Reputation: 43

Possible to extract part of $BRANCH_NAME string in Jenkins Pipeline?

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!

Upvotes: 0

Views: 1501

Answers (2)

Pankaj Saini
Pankaj Saini

Reputation: 1648

You can use groovy split function -

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

Output:

XXX-111

Upvotes: 1

MaratC
MaratC

Reputation: 6889

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

Upvotes: 3

Related Questions