2017-03-01 80 views
0

我在全局管線庫的vars目錄中定義了一個全局函數。我總是得到java.io.NotSerializableExceptionJsonSlurper造成。我用JsonSlurperClassic替換了它,現在我得到了一個類似的由HttpURLConnection引起的錯誤。我無法找到任何文檔,我如何處理全局函數中的序列化問題。 @NonCPS不起作用。我在vars目錄中創建一個文件名sonar.groovy,並添加以下代碼:Jenkins 2全局函數中的管道序列化問題

import groovy.json.JsonSlurperClassic 

def call(Map params) { 

    // sonar maven scanner 
    def sonarCmd = 'org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar' 

    // set default value if a param is null 
    params.sonarId = params.sonarId ?: 'Sonar' 
    params.dir = params.dir ?: '.' 
    params.failOnError = params.failOnError ?: true 

    withSonarQubeEnv('Sonar-Dev') { 
     maven(dir: params.dir, cmd: sonarCmd) 
    } 

    // parse analysis status and throw an error if the analysis does not pass the quality gate 
    if (params.failOnError) { 
     def jsonParser = { url -> new JsonSlurperClassic().parse(url.openStream()) } 

     def timeoutInMinutes = 10 

     def props = readProperties file: "${params.dir}/target/sonar/report-task.txt" 

     def ceTaskUrl = new URL(props.ceTaskUrl) 
     def ceTask 

     timeout(time: timeoutInMinutes, unit: 'MINUTES') { 
      waitUntil { 
       ceTask = jsonParser(ceTaskUrl) 
       echo ceTask.toString() 
       return "SUCCESS" == ceTask.task.status 
      } 
     } 

     def analysisId = ceTask.task.analysisId 

     def statusUrl = new URL("${props.serverUrl}/api/qualitygates/project_status?analysisId=${analysisId}") 

     def analysisStatusJson = jsonParser(statusUrl) 

     def status = analysisStatusJson.projectStatus.status 

     if (status == "ERROR") { 
      error "Sonar Analysis: Quality Gate failure" 
     } 
    } 
} 

我總是得到這樣的錯誤:

[Pipeline] } 
[Pipeline] // wrap 
[Pipeline] readProperties 
[Pipeline] timeout 
Timeout set to expire in 10 min 
[Pipeline] { 
[Pipeline] waitUntil 
[Pipeline] { 
[Pipeline] } 
[Pipeline] // waitUntil 
[Pipeline] } 
[Pipeline] // timeout 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] End of Pipeline 
java.io.NotSerializableException: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:778) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65) 
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56) 
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50) 
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179) 
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344) 
    at java.util.HashMap.internalWriteEntries(HashMap.java:1785) 
    at java.util.HashMap.writeObject(HashMap.java:1362) 
    at sun.reflect.GeneratedMethodAccessor263.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:271) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988) 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854) 
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58) 
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111) 
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:132) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:452) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:427) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:415) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:360) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:240) 
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:228) 
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112) 
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: an exception which occurred: 
    in field arguments 
    in field abnormal 
    in field outcome 
    in field body 
    in field step 
    in field thread 
    in field body 
    in field step 
    in field thread 
    in field this$0 
    in field returnAddress 
    in field parent 
    in field caller 
    in field parent 
    in field parent 
    in field caller 
    in field parent 
    in field parent 
    in field parent 
    in field capture 
    in field def 
    in field closures 
    in object [email protected] 
Finished: FAILURE 

有沒有辦法在一個全局函數來處理序列化問題?

回答

0

我通過更換openStream()解決的問題newReader()

def jsonParser = { url -> new JsonSlurperClassic().parse(url.newReader()) } 
0

好,你有它解決了!一些額外的評論和解決方法。在Jenkins管道中定義的所有變量都必須是可序列化的,原因在於Jenkins可能會在任何時候暫停正在進行的管道執行並序列化狀態。所以,這就是你得到錯誤的原因。

解決此問題的一般方法是將不可序列化的代碼包裝在註釋爲@NonCPS的函數中。這告訴Jenkins該函數包含不可序列化的部分,並且必須執行而不會暫停。

更多信息可以在這裏找到https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#serializing-local-variables

+1

是的,但是我怎麼可以用'@ NonCPS'在我的[全局庫]定義的全局函數(https://wiki.jenkins-ci.org/display/JENKINS /管道+共享+ Groovy的+庫+插件) – Henok