Jenkins Groovy how to call methods from @NonCPS method without ending pipeline Jenkins Groovy how to call methods from @NonCPS method without ending pipeline jenkins jenkins

Jenkins Groovy how to call methods from @NonCPS method without ending pipeline


I've resolved this issue essentially by doing the following:

import groovy.json.JsonSlurperdef myMethod(String json) {    def jsonSlurper = new JsonSlurper()    def jsonObject = jsonSlurper(json)    jsonSlurper = null    for(int i = 0; i < jsonObject.size(); i++) {        switch(jsonObject[i].name) {            case "foo":                doAThing(jsonObject[i])                break            case "bar":                doAnotherThing(jsonObject[i])                break        }    }}

Immediately destroy the JsonSlurper instance after it's used, remove @NonCPS annotation, switch to a c-style for loop instead of each.


Just to clarify, this is documented as an unsupported feature - https://github.com/jenkinsci/workflow-cps-plugin/#technical-design

You may not call regular (CPS-transformed) methods, or Pipeline steps, from a @NonCPS method, so they are best used for performing some calculations before passing a summary back to the main script.