我还要求使用master分支允许补丁分支(我们使用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
}
}
这里的问题出现在Patch分支的正则表达式部分。我希望jenkins检查补丁分支是否以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]*)/)