正则表达式用于匹配分支詹金斯声明性管道

时间:2018-09-26 08:34:32

标签: regex jenkins jenkins-pipeline declarative

我也要求允许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分支的正则表达式部分中。我想让詹金斯检查补丁分支是否以Patch_For_shortCommitIDSha开头,例如Patch_For_87eff88

但是我写错的正则表达式允许以Patch_For_开头的分支以外的分支

2 个答案:

答案 0 :(得分:1)

问题出在NOT运算符上。

'!=〜'不是Groovy的有效匹配运算符,必​​须将其替换。 IF NOT MATCH正则表达式的重写形式应如下所示:

isPatch = !(env.BRANCH_NAME =~ /Patch_For_*([a-z0-9]*)/)

因此,您看到,NOT运算符应该超出布尔匹配表达式,该表达式应该用括号括起来,而不能放在其前面。

答案 1 :(得分:0)

这对我有用。

isPatch = (env.BRANCH_NAME ==~ /Patch_For_*([a-z0-9]*)/)
相关问题