在curl调用中获取Jenkinsfile中的SerializableException

时间:2017-02-14 02:51:17

标签: jenkins-pipeline

我正在开发一个甚至没有构建任何东西的管道脚本。它克隆一个repo,然后获取有关repo的一些信息,并使用BitBucket REST API获取有关存储库的其他信息。

以下是Jenkins文件的摘录:

    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”步骤为BitBucket REST API调用“curl”,但它没有超过它。我无法弄清楚它抱怨的对象是什么。

更新

我正在运行Jenkins 2.19.2。

管道具有以下设置:

  • “不允许并发构建”:on
  • 10个已定义的参数,一个是Credentials参数,在此块中引用

2 个答案:

答案 0 :(得分: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变量类似。

文本参数设置为“defaultValue modified”运行时,这是我的输出:

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

如您所见,管道已成功完成。我可以看到管道没有问题。

也许您可以使用工作配置的屏幕截图和jenkins安装的版本号更新您的问题。

答案 1 :(得分:1)

我遇到了同样的问题,但问题似乎并非由sh引起。它可能是由您在sh步骤之上定义的变量引起的,该变量不是可序列化的。