Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovy

Jenkins pipeline groovy java.lang.IllegalStateException: unexpected break statement


problem description:

While writing a loop with groovy in Jenkins pipeline(below is a smallest case can reproduce the problem):

def workList = [1, 2, 3, 4]
workList.each{work->
    if (work == 2) {
        break
    }
}
sh 'hello world'

I got an error like below:

09:56:34  java.lang.IllegalStateException: unexpected break statement
09:56:34    at com.cloudbees.groovy.cps.impl.CallEnv.getBreakAddress(CallEnv.java:102)
09:56:34    at com.cloudbees.groovy.cps.impl.ProxyEnv.getBreakAddress(ProxyEnv.java:52)
09:56:34    at com.cloudbees.groovy.cps.impl.ProxyEnv.getBreakAddress(ProxyEnv.java:52)
09:56:34    at com.cloudbees.groovy.cps.impl.ProxyEnv.getBreakAddress(ProxyEnv.java:52)
09:56:34    at com.cloudbees.groovy.cps.impl.BreakBlock.eval(BreakBlock.java:21)
09:56:34    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
09:56:34    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
09:56:34    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
09:56:34    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
09:56:34    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
09:56:34    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
09:56:34    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
09:56:34    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:403)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:97)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:315)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:279)
09:56:34    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
09:56:34    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
09:56:34    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
09:56:34    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
09:56:34    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
09:56:34    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
09:56:34    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
09:56:34    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
09:56:34    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
09:56:34    at java.lang.Thread.run(Thread.java:748)
09:56:34  Finished: FAILURE

Things i tried

  1. I read the related stuff about the loop syntax in groovy, break can break the loop. But why here has errors.

  2. By search the error pattern java.lang.IllegalStateException: unexpected break statement also has no luck

last

Thanks in advance!


Solution

  • You can't break from an each type loop without throwing an error. If you want to break/abort the loop you can use a clasic-type loop. Here is an example. You can read about more options from here.

    def workList = [1, 2, 3, 4]
    for (def work : workList) { 
       if (work == 2) {
            break
        }
    }