如何使用正则表达式定义大小写切换以检查大小写变量是否包含特定字符串?

时间:2016-11-08 16:29:50

标签: jenkins groovy jenkins-pipeline

我正在编写Jenkins管道作为代码来构建Android应用程序。

作业配置为" GitHub Organization"这意味着Jenkins正在扫描给定的存储库,将每个分支添加为作业,并在发生git提交时启动作业运行。

switch(BRANCH_NAME) {
    case "develop":
        BUILDFLAV = "Staging"
        BUILDTYPE = "Debug"
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        break
    case "master":
        BUILDFLAV = "Production"
        BUILDTYPE = "Release"
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        break
    case "release_candidate":
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        break
    case "hotfix":
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        break
    case "jenkins-pipeline":
        BUILDFLAV = "Staging"
        BUILDTYPE = "Debug"
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        break
    case "feature/upload_to_hockeyapp":
        BUILDFLAV = "Staging"
        BUILDTYPE = "Debug"
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        break
    case "feature/notifications":
        BUILDFLAV = "Staging"
        BUILDTYPE = "Debug"
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        break
    default:
        println "Branch name was not set!"
        break
}

有些分支的名称如此:

hotfix/3.0.1
release/3.2.1

我想为这些分支定义一个案例选项,例如:

case "^hotfix.*"
   code....
   break

case "^release\/.*"
    code...
    break

编辑#1: 这是添加您的部分后的代码:

// Branch selection
def showRelease(BRANCH_NAME, regex) {
    def pattern = "(release|hotfix)\\/(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)"
    switch(BRANCH_NAME) {
        case "develop":
            BUILDFLAV = "Staging"
            BUILDTYPE = "Debug"
            ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            break
        case "master":
            BUILDFLAV = "Production"
            BUILDTYPE = "Release"
            ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            break
        case "release_candidate":
            ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            break
        case "hotfix":
            ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            break
        case "jenkins-pipeline":
            BUILDFLAV = "Staging"
            BUILDTYPE = "Debug"
            ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            break
        case "feature/notifications":
            BUILDFLAV = "Staging"
            BUILDTYPE = "Debug"
            ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            break
        case ~/$regex/:
            BUILDFLAV = "Staging"
            BUILDTYPE = "Debug"
            ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            println BRANCH_NAME

            break
        default:
            println "Branch name was not set!"
            break
    }
}

但是,当我运行构建时,BRANCH_NAME设置不正确,后续步骤无法运行,因为它们缺少BUILDFLAV和BUILDTYPE变量。

编辑#2: 我已经添加了您建议的代码:

case ~/(release|hotfix)\\/(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)/:
        BUILDFLAV = "Staging"
        BUILDTYPE = "Debug"
        ARTIFACT_NAME = "App01-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
        println "Branch Name: ${BRANCH_NAME}"
        break

但现在我收到了这个错误:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.ScriptBytecodeAdapter bitwiseNegate java.lang.Object
    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:190)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onStaticCall(SandboxInterceptor.java:142)
    at org.kohsuke.groovy.sandbox.impl.Checker$2.call(Checker.java:180)

但是我对詹金斯的正则表达不太熟悉(时髦),怎么办呢?

2 个答案:

答案 0 :(得分:2)

我相信可以通过以下简单方式来处理修补程序发布

<强>脚本:

def showRelease(val, regex) {
    switch (val) {
        case ~/$regex/:
            println "branch is set: ${val}"
            break       

        default:
            println "Branch name was not set!"
            break
    }
}

def pattern = "(release|hotfix)\\/(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)"
showRelease('release/1.0.1', pattern)
showRelease('release/2.2.3', pattern)
showRelease('release.3.x', pattern)
showRelease('hotfix/3.0.2', pattern)

您可能会注意到输出:

  

分支设置:release / 1.0.1
  分支设置:release / 2.2.3
  未设置分支名称!
  分支设置:hotfix / 3.0.2

编辑:根据OP的评论,添加固定模式片段:

switch(BRANCH_NAME) {
        case ~/(release|hotfix)\/(\d+\.)?(\d+\.)?(\*|\d+)/:
            BUILDFLAV = "Staging"
            BUILDTYPE = "Debug"
            ARTIFACT_NAME = "AutoCAD360-${BUILDFLAV.toLowerCase()}-${BUILDTYPE.toLowerCase()}.apk"
            break
....
}

答案 1 :(得分:-1)

我不明白为什么下面的例子不匹配:

val = 'release/v1.1'
def pattern = "(release)\\/(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)"


switch (val) {
    case ~/$pattern/:
        println "branch is set: ${val}"
        break       
     default:
         println "Branch name was not set!"
         break
}

它输出:

Branch name was not set!