Jenkins脚本化管道分支条件与通配符

Jenkins脚本化管道分支条件与通配符,jenkins,groovy,Jenkins,Groovy,如何在脚本化管道中获得类似于声明性管道的内容“当分支等于其中任何一个…”,包括可能的通配符 例如,在声明性管道中,我有: when { anyOf{ branch "master"; branch "feature/*"; branch "fix/*" } } 如何在脚本化管道中实现这一点? 我试过这样的方法: def branches = ["master", "feature/*", "fix

如何在脚本化管道中获得类似于声明性管道的内容“当分支等于其中任何一个…”,包括可能的通配符

例如,在声明性管道中,我有:

when { 
      anyOf{
            branch "master";
            branch "feature/*";
            branch "fix/*"
      }
}
如何在脚本化管道中实现这一点? 我试过这样的方法:

def branches = ["master", "feature/*", "fix/*"]
if (branches.any{branch -> branch = env.BRANCH_NAME){ 
   do something here 
}

但不幸的是,它不适用于通配符。

这可以使用正则表达式匹配来完成:

def branches = ["master", "feature/.*", "fix/.*"]
if( branches.any{ branch -> env.BRANCH_NAME ==~ branch }) {
    do something here
}
这里我们使用
==~
操作符将左侧的字符串与右侧的正则表达式匹配

请注意,我们不使用
=~
,因为我们不希望子字符串匹配(例如,我们不希望“mymaster”与“master”匹配)