NotSerializableException using Publish Over SSH in Jenkinsfile

独自空忆成欢 提交于 2019-12-23 04:36:21

问题


I'm trying to use the Publish over SSH plugin inside a Jenkinsfile. However, I'm getting the exception java.io.NotSerializableException in the createClient method. This is my code:

def publish_ssh         = Jenkins.getInstance().getDescriptor("jenkins.plugins.publish_over_ssh.BapSshPublisherPlugin")
def hostConfiguration   = publish_ssh.getConfiguration("${env.DSV_DEPLOY_SERVER}");

if( hostConfiguration == null )
{
    currentBuild.rawBuild.result = Result.ABORTED
    throw new hudson.AbortException("Configuration for ${env.DSV_DEPLOY_SERVER} not found.")
}

def buildInfo = hostConfiguration.createDummyBuildInfo();

def sshClient = hostConfiguration.createClient( buildInfo, new BapSshTransfer(
        env.SOURCE_FILE,
        null,
        env.DSV_DEPLOY_REMOTE_DIR,
        env.REMOVE_PREFIX,
        false,
        false,
        env.DSV_DEPLOY_COMMAND,
        env.DSV_DEPLOY_TIMEOUT as Integer,
        false,
        false,
        false,
        null
    ));

How can I get rid of the exception?


回答1:


It is because some variables are not serializable.

From doc

Since pipelines must survive Jenkins restarts, the state of the running program is periodically saved to disk so it can be resumed later (saves occur after every step or in the middle of steps such as sh).

You may use @NonCPS annotation to do the creation, use the

@NonCPS
def createSSHClient() {
// your code here.
}


来源:https://stackoverflow.com/questions/45824120/notserializableexception-using-publish-over-ssh-in-jenkinsfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!