2017-02-14 87 views
2

我正在研究甚至沒有構建任何東西的管道腳本。它克隆一個repo,然後獲取有關repo的一些信息,並使用BitBucket REST API獲取有關存儲庫的其他信息。curl調用在Jenkinsfile中獲取SerializableException

以下是Jenkinsfile的摘錄:

stageName = 'GET-COMMITS-AND-USERS' 
    stage (stageName) { 
     withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: params.JP_MechIdCredentials, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { 
      def uniqueCommitterMap = {} 
      def format = 'yyyy-MM-dd' 
      def now = new Date() 
      def aWhileAgo = now - params.JP_DaysInPastToLookFor.toInteger() 
      def uniqueCommitterEmails = sh(returnStdout: true, script:"git log --date=short --pretty=format:'%ce' --after='${aWhileAgo.format(format)}' --before='${now.format(format)}' | sort -u") 
      now   = null 
      aWhileAgo = null 
      println "uniqueCommitterEmails[${uniqueCommitterEmails}]" 
      def uniqueCommitterEmailList = uniqueCommitterEmails.split(/[ \t\n]+/) 
      uniqueCommitterEmails = null 
      println "uniqueCommitterEmailList[${uniqueCommitterEmailList}] size[${uniqueCommitterEmailList.size()}]" 
      for (int ctr = 0; ctr < uniqueCommitterEmailList.size(); ++ ctr) { 
       println "entry[${uniqueCommitterEmailList[ctr]}]" 
       println "entry[${uniqueCommitterEmailList[ctr].split('@')}]" 
       uniqueCommitterMap[uniqueCommitterEmailList[ctr].split("@")[0]] = uniqueCommitterEmailList[ctr] 
      } 
      println "uniqueCommitterMap[${uniqueCommitterMap}]" 
      println "end of uCM." 
      uniqueCommitterEmailList = null 
      def cmd = "curl -u ${USERNAME}:${PASSWORD} https://.../rest/api/1.0/projects/${params.JP_ProjectName}/repos/${params.JP_RepositoryName}/permissions/users?limit=9999" 
      USERNAME = null 
      PASSWORD = null 
      println "cmd[${cmd}]" 
      def usersJson = sh(returnStdout: true, script:cmd.toString()) 
      println "Past curl call." // Don't get here 

以下是控制檯輸出的一個片段,當我運行此作業使用適當的參數:

[Pipeline] echo 
end of uCM. 
cmd[curl -u ****:**** https://.../rest/api/1.0/projects/.../repos/.../permissions/users?limit=9999] 
[Pipeline] echo 
[Pipeline] sh 
[workspace] Running shell script 
[Pipeline] } 
[Pipeline] // withCredentials 
[Pipeline] } 

[Pipeline] // stage 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] End of Pipeline 
[DOSSIER] Response Code: 201 
java.io.NotSerializableException: java.io.StringWriter 
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860) 
    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:1777) 
    at java.util.HashMap.writeObject(HashMap.java:1354) 
    at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 

正如你所看到的,它似乎執行了「sh」步驟來調用BitLucket REST API的「curl」,但它並沒有超越它。我無法弄清楚它抱怨的是什麼對象。

更新

我運行詹金斯2.19.2。

管道具有以下設置:

  • 「不允許併發建立」:上
  • 10個總定義的參數,其中一個證書參數,其在該塊中引用

回答

1

爲了回答你的問題,我從the official Dockerfile跑Jenkins v2.32.2並創建了以下測試流水線:

node() { 
    stage('serialize') { 
     def USERNAME = 'myusername' 
     def PASSWORD = 'mypassword' 
     def cmd = "echo curl -u ${USERNAME}:${PASSWORD} https://.../${params.TEST_PARAM1}/permissions/users?limit=9999" 
     USERNAME = null 
     PASSWORD = null 
     println "cmd[${cmd}]" 
     def usersJson = sh(returnStdout: true, script:cmd) 
     println "Past curl call." 
    } 
} 

我還在編譯作業中添加了文本參數,它與您的params.JP_ProjectName變量類似。

並與文本參數設置爲運行時,這是我的輸出「默認值修改」

Started by user admin 
[Pipeline] node 
Running on master in /var/jenkins_home/workspace/42217046 
[Pipeline] { 
[Pipeline] stage 
[Pipeline] { (serialize) 
[Pipeline] echo 
cmd[echo curl -u myusername:mypassword https://.../defaultValue modified/permissions/users?limit=9999] 
[Pipeline] sh 
[42217046] Running shell script 
+ echo curl -u myusername:mypassword https://.../defaultValue modified/permissions/users?limit=9999 
[Pipeline] echo 
Past curl call. 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] End of Pipeline 
Finished: SUCCESS 

正如你所看到的,管道完成successully。我可以看到管道沒有問題。

也許你可以用你的工作配置截圖和你的jenkins安裝的版本號更新你的問題。

1

我遇到了同樣的問題,但看起來這個問題根本不是由sh造成的。這可能是由於您在sh之上定義的變量導致的,該變量不是可序列化的。