简体   繁体   中英

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!

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

You can use groovy split function -

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

Output:

XXX-111

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM