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

时间:2018-04-26 18:50:28

标签: 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)

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

2 个答案:

答案 0 :(得分:2)

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

  

因此:程序中使用的任何变量值都应该是数字,字符串或其他可序列化类型

你的问题出在这一行: def user = err.getCauses()[0].getUser()

返回的对象属于hudson.model.User类型,即not serializable,即未实现Serializable接口。

由于您只对用户名感兴趣,所以您可以这样做:

def user = err.getCauses()[0].getUser().toString()
if ('SYSTEM' == user) {

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

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

答案 1 :(得分:0)

@Vasan 的回答是正确的解决方案。我只想补充为什么当您不添加promoteBuild() 行或删除shell 调用时行为会有所不同。 如doc中所述,

<块引用>

由于管道必须在 Jenkins 重启后继续存在,因此运行程序的状态会定期保存到磁盘,以便稍后恢复(保存发生在每个步骤之后或步骤中间,例如 sh)。

您可以看到调用 sh 的步骤将触发要保存的管道状态,从而触发序列化操作,然后导致抛出 java.io.NotSerializableException。