Jenkinsfile分支设置时对模式的评估是什么? [英] What's the pattern evaluation on Jenkinsfile when-branch setting?
问题描述
我正在尝试在阶段内的when语句上检测分支模式.
I am trying to detect the branch pattern on a when statement inside a stage.
赞:
stage('deploy to staging') {
agent label:'some-node'
when { branch "feature/*" }
steps {
sh './deploy_pr.sh'
}
}
如果我想要更复杂的图案怎么办?
What if I want a more complicated pattern?
我正在尝试检测诸如feature/0.10.25之类的内容,并且以下模式不起作用:
I am trying to detect something like feature/0.10.25 and the following pattern doesn't work:
when { branch 'feature/[0-9]+.[0-9]+.[0-9]+' }
不起作用.根据 https://regexr.com/
推荐答案
好!因此,通过错误堆栈跟踪,我发现在分支分支选项中,Jenkins与Ant样式模式进行了比较: https://ant.apache.org/manual/dirtasks.html
Ok ! So, through the error stack trace I found out that on the when-branch option, Jenkins compares with Ant style patterns: https://ant.apache.org/manual/dirtasks.html
这意味着它不希望使用正则表达式,而是更简单的东西,例如:
That means it doesn't expect regexp, but simpler stuff like:
*/staging/*
我改用when-expression选项解决了这个问题,
I solved this by using the when-expression option instead, like this:
when {
expression { BRANCH_NAME ==~ /feature\/[0-9]+\.[0-9]+\.[0-9]+/ }
}
它使用groovy表达式,如下所述:
That uses groovy expressions as described here:
https://www.regular-expressions.info/groovy.html
尤其是寻找==〜运算符的解释,这很有帮助.
Especially, look for the explanation of the ==~ operator, that was helpful.
对于正则表达式本身,您可以在此处进行测试:
For the regular expression itself, you can test yours here:
这篇关于Jenkinsfile分支设置时对模式的评估是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!