jenkins-pipeline - 在 curl 调用时在 Jenkinsfile 中获取 SerializedException

标签 jenkins-pipeline

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

以下是 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”步骤来为 BitBucket REST API 调用“curl”,但它并没有超越这一点。我不知道它在提示什么对象。

更新:

我正在运行 Jenkins 2.19.2。

管 Prop 有以下设置:

  • “不允许并发构建”:开启
  • 总共 10 个已定义参数,其中一个是 Credentials 参数,在此 block 中引用了

最佳答案

为了回答你的问题,我从 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修改”时的输出:

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 安装的版本号来更新您的问题。

关于jenkins-pipeline - 在 curl 调用时在 Jenkinsfile 中获取 SerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42217046/

相关文章:

jenkins - 是否可以在 Jenkins 参数名称中包含空格?

jenkins - Jenkins 管道中基于时间的步骤的默认单位

jenkins - 如何从管道作业访问 git 分支名称?

Jenkins 管道 s3 文件下载

maven - 如何在 Jenkins 管道中使用嵌套的 docker.image ('test_image' ).inside()

jenkins - 如何在 Jenkins 管道中并行运行 for 循环的每次迭代?

Jenkins 亚马逊 ECR : no basic auth credentials

jenkins - 如何使用 Pipeline Job 的 CopyArtifact 插件传递从中复制工件的内部版本号?

git - 如何知道哪个 git 分支触发了 jenkins 作业

jenkins - Jenkinsfile 参数化构建中的环境和参数之间有什么关系?