There are plenty of discussion about what it means and what to do about it, however the main solution of using @NonCPS doesn't seem to work. Here's the relevant piece of the code:
@NonCPS def restCall(String method, String resource, String data = '') { def URL url = new URL("${Params.REST_BASE_URI}/${resource}") def HttpURLConnection connection = url.openConnection() withCredentials([usernamePassword(credentialsId: 'restful-api', passwordVariable: 'RA_PASS', usernameVariable: 'RA_USER')]) { String encoded = Base64.getEncoder().encodeToString(("${env.RA_USER}:${env.RA_PASS}").getBytes(StandardCharsets.UTF_8)) connection.setRequestProperty("Authorization", "Basic ${encoded}"); } connection.setRequestProperty("content-type", "application/json"); connection.setRequestMethod(method) connection.doOutput = true if (data != '') { def writer = new OutputStreamWriter(connection.outputStream) writer.write(data) writer.flush() writer.close() } connection.connect(); def statusCode = connection.responseCode if (statusCode != 200 && statusCode != 201) { throw new Exception(connection.getErrorSteam().text) } return connection.content.text } Note that it does have @NonCPS on the function. However executing this still produces the same error:
an exception which occurred: in field groovy.lang.Reference.value in object groovy.lang.Reference@1375b00 in field WorkflowScript$_bitbucketCall_closure1.connection in object WorkflowScript$_bitbucketCall_closure1@b3001c in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.closures in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@144b2a6 in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@144b2a6 Caused: java.io.NotSerializableException: sun.net.www.protocol.https.HttpsURLConnectionImpl at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860) at ... How can I solve it?
@NonCPSwould only apply to the method itself I would think. That would then not apply to the scope ofURL.openConnection(), which does not seem to be serializable. That being said, I hope someone knows how to solve this because global vars to create REST API calls are going to be rather helpful in jenkins-pipeline.HTTPUrlConnectionclass. In any event, an example calling code would bedef result = restCall('GET', 'info')