简体   繁体   English

可以在 Jenkins 管道中提取 $BRANCH_NAME 字符串的一部分吗?

[英]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".我正在尝试构建一个 docker 图像并使用始终为“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.但是,当我使用 $branch_name env 变量获取分支名称时,使用 tag -t 命令构建的 docker 不像“feature/XXX-111”分支名称中的“/”。 So i was wondering if it was possible to only get the "XXX-111" part of the branch name and dropping the "feature/".所以我想知道是否可以只获取分支名称的“XXX-111”部分并删除“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: Output:

XXX-111

You can use groovy split function -您可以使用 groovy 拆分 function -

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

Output: Output:

XXX-111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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