0

I am trying to write a jenkins file with a switch case, where I am trying to use string operations in case. In a regular groovy it is working fine but in JenkinsFile it is throwing errors. What am I missing?

Code

        switch (env.UPSTREAM)
        {
            case { it.contains("SDK") }:
                //do something
                break
            case { it.contains("sample") }:
                //do something
                break
        }

Error

hudson.remoting.ProxyException: CpsCallableInvocation{methodName=call, call=com.cloudbees.groovy.cps.impl.CpsClosureDef@47e13b68, receiver=org.jenkinsci.plugins.workflow.cps.CpsClosure2@3d8cf0c6, arguments=[asjkndakjsd_sample]}
Sarat
  • 53
  • 6

1 Answers1

0

Jenkins uses Groovy CPS to run pipeline scripts, you can read about it here. It's kind of confusing but basically you can't use some of the more complicated groovy functions/commands.

A way to get around this is to use the @NonCPS annotation. You have to create a function outside of your pipeline with that @NonCPS annotation. But in your case you could probably just use simple if statements instead of the switch. For example just create a new variable def upStream = env.UPSTREAM then check each case:

if (upStream.contains("SDK"))
{
    // do something
}
else if (upStream.contains("sample"))
{
    // do something
}
frankM_DN
  • 365
  • 7