Itai Ganot Itai Ganot - 24 days ago 12
Groovy Question

How to define case switch to check if the case variable includes specific strings by using regex?

I'm writing a Jenkins pipeline as a code in order to build an Android application.

The job is configured as "GitHub Organization" which means that Jenkins is scanning the given repository, adds each branch as a job and starts a job run whenever a git commit takes place.

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
}


Some of the branches are named like so:

hotfix/3.0.1
release/3.2.1


I'd like to define a case option for these branches, something like:

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


And:

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


Edit #1:
Here's the code after adding your part:

// 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
}
}


But then, when I run the build, BRANCH_NAME is not set correctly and the next steps are unable to run because they lack the BUILDFLAV and BUILDTYPE variables.

Edit #2:
I've added the code you suggested:

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


But now I get this error:

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)


But i'm not so familiar with regex use in Jenkins (groovy), how can it be done?

Rao Rao
Answer

I believe that it can be done in the following simple for handling hotfix and releases:

Script:

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)

And you may notice the output:

branch is set: release/1.0.1
branch is set: release/2.2.3
Branch name was not set!
branch is set: hotfix/3.0.2

EDIT: Based on OP's comment, adding fixed pattern snippet:

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