Jenkinsfile when-branch 设置的模式评估是什么? [英] 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/
Doesn't work . And it's a correct regexp, according to https://regexr.com/
推荐答案
好的!因此,通过错误堆栈跟踪,我发现在 when-branch 选项上,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 when-branch 设置的模式评估是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!