shell - 执行shell时Jenkins管道 "java.io.NotSerializableException: hudson.model.User"

标签 shell jenkins groovy jenkins-pipeline

这是我的管道代码。注意函数 promoteBuild()是假的,它需要执行shell块。在这里,我只是让它回显一个常量字符串。

   pipeline {
    agent none

    stages {
        stage("PromoteBuild") {
            agent {
                label "test_agent"
            }
            steps {

                script {

                    mopName = "mop name"
                    try {

                        // Timeout in case to avoid running this forever
                        timeout(time: 30, unit: 'SECONDS') {
                            promoteMap = input id: 'promote', message: 'Choose bundled buildNumbers', ok: 'Promote?',
                                    parameters: [

                                            string(
                                                    defaultValue: mopName,
                                                    description: '',
                                                    name: 'MopName'),

                                    ],
                                    submitter: "xxxx", submitterParameter: 'approver'
                        }
                      promoteBuild()

                    } catch (err) {
                        def user = err.getCauses()[0].getUser()
                        if ('SYSTEM' == user.toString()) { // SYSTEM means timeout.
                            //No response means the build is launched by timer, promote the first bundled-number
                            echo "Promote only. No following deployment"
                            promoteBuild()
                            currentBuild.result = 'SUCCESS'
                        } else {
                            //if user aborted this operation, do nothing and mark the build as aborted
                            currentBuild.result = 'ABORTED'

                            echo "This build was aborted by [${user}]"
                        }

                    }

                }
            }
        }
    }

def promoteBuild() {
sh """
    echo "hello"
  """
}

函数promoteBuildtry 成功运行阻止(当我响应输入请求时)但在 catch 中失败阻止(超时时)。

下面是控制台输出:
Input requested
Cancelling nested steps due to timeout
[Pipeline] }
[Pipeline] // timeout
[Pipeline] echo
Promote only. No following deployment
[Pipeline] sh
[testTmp] Running shell script
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
***java.io.NotSerializableException: hudson.model.User***
    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.GeneratedMethodAccessor35.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.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.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.TreeMap.writeObject(TreeMap.java:2434)
    at sun.reflect.GeneratedMethodAccessor421.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)

我很确定它与“用户”无关,因为如果我删除 shell 调用,构建就会成功。任何提示?

最佳答案

https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#user-content-serializing-local-variables

As such: any variable values used in your program should be numbers, strings, or other serializable types



你的问题是在这一行:def user = err.getCauses()[0].getUser()
返回的对象类型为 hudson.model.User , 即 not serializable即不实现 Serializable界面。

由于您只对用户名感兴趣,因此您可以这样做:
def user = err.getCauses()[0].getUser().toString()
if ('SYSTEM' == user) {

这会起作用,因为 String 是可序列化的。

您可能还想首先找出发生异常的真正原因 - 尝试将其记录在 catch 块中。

关于shell - 执行shell时Jenkins管道 "java.io.NotSerializableException: hudson.model.User",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50050076/

相关文章:

php - 如何获取linux命令执行结果或消息

bash - 在不同文件的不同位置删除包含给定字符串的行

java - 在 Jenkins 分析时 Sonar 工作失败

Linux权限问题

groovy - 通过 Groovy 脚本使用自定义名称复制文件或文件夹

grails - Grails Web Flow的第一状态

linux - Ansible 执行脚本不起作用

bash - 将文件从存储库移动到另一个文件时,权限被拒绝

jenkins - Jenkins 中 archiveArtifacts 命令的文档

Gradle:在某个任务中顺序执行任务