smi*_*ace 2 regex declarative jenkins jenkins-pipeline
我有一个要求也允许使用主分支进行补丁分支(我们使用 git)。
stages {
stage('PR Build') {
when {
beforeAgent true
expression {
isMaster = env.BRANCH_NAME == masterBranchName
isPatch = env.BRANCH_NAME !=~ /Patch_For_*([a-z0-9]*)/
echo "isMaster : ${isMaster} , isPatch : ${isPatch}"
return !isMaster && !isPatch
}
}
steps {
script{
buildType = 'PR'
}
// Do PR build here...
}
}
stage('Build master / patch branch') {
when {
beforeAgent true
expression {
isMaster = env.BRANCH_NAME == masterBranchName
isPatch = env.BRANCH_NAME !=~ /Patch_For_*([a-z0-9]*)/
echo "isMaster : ${isMaster} , isPatch : ${isPatch}"
return isMaster || isPatch
}
}
steps {
script {
buildType = 'deployment'
)
}
// Do master or patch branch build and deployment
}
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是在补丁分支的正则表达式部分。我想让詹金斯检查补丁分支是否以Patch_For_shortCommitIDSha例如开始Patch_For_87eff88
但是我写错的正则表达式允许以以下开头的分支以外的分支 Patch_For_
问题在于 NOT 运算符。
'!=~' 不是 Groovy 的有效匹配运算符,必须替换。IF NOT MATCH 正则表达式的重写形式应如下所示:
isPatch = !(env.BRANCH_NAME =~ /Patch_For_*([a-z0-9]*)/)
所以你看,不是运算符!应该离开布尔匹配表达式,它应该括在括号中,而不是放在它之前。
这对我有用。
isPatch = (env.BRANCH_NAME ==~ /Patch_For_*([a-z0-9]*)/)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10724 次 |
| 最近记录: |