用于匹配分支jenkins声明性管道的正则表达式

问题描述 投票:1回答:2

我还要求使用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_之外的分支以外的分支

regex jenkins jenkins-pipeline declarative
2个回答
1
投票

问题在于NOT运算符。

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

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

所以你看,不是操作员!应该超出布尔匹配表达式,它应该括在括号中,而不是放在它之前。


0
投票

这对我有用。

isPatch = (env.BRANCH_NAME ==~ /Patch_For_*([a-z0-9]*)/)
© www.soinside.com 2019 - 2024. All rights reserved.