Jenkins Pipeline 抛出 java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowRun 即使使用@NonCPS [英] Jenkins Pipeline throwing java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowRun even with @NonCPS

查看:64
本文介绍了Jenkins Pipeline 抛出 java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowRun 即使使用@NonCPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码相当简单,我只想要一个发布到 slack 的 rev-list.但是导致我出现问题的部分是当我实际上试图从 git 获取 rev-list 时.

问题代码

@NonCPSdef getRevisionList(currentCommit, lastSuccessfulCommit) {def 提交 = sh(脚本:git rev-list $currentCommit $lastSuccessfulCommit",返回标准输出:真).split('
')回声$提交"}

完整代码:

管道 {环境 {失败消息 = ""}代理人 {节点{标签gsacsp-build02.reisys.com"}}阶段{阶段(解析提交"){脚步 {脚本 {def currentBuild = currentBuild.rawBuilddef currentCommit = commitHashForBuild(currentBuild)def lastSuccessfulCommit = getLastSuccessfulCommit()getRevisionList(currentCommit, lastSuccessfulCommit)}}}}邮政 {总是 {withCredentials([字符串(credentialsId:'BOT_SLACK_HOOK',变量:'BOT_SLACK_HOOK')]){脚本 {failureMessage = readFilejenkinshelpers/slackfailuremessage.json"sh "curl -X POST -H '内容类型:应用程序/json' --data '$failureMessage' $BOT_SLACK_HOOK"}}}}}//Groovy 辅助方法@NonCPSdef getRevisionList(currentCommit, lastSuccessfulCommit) {def 提交 = sh(脚本:git rev-list $currentCommit $lastSuccessfulCommit",返回标准输出:真).split('
')回声$提交"}@NonCPSdef getLastSuccessfulCommit() {def lastSuccessfulHash = nulldef lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()if ( lastSuccessfulBuild ) {lastSuccessfulHash = commitHashForBuild(lastSuccessfulBuild)}返回最后一个成功的哈希}@NonCPSdef commitHashForBuild(build) {def scmAction = build?.actions.find { action ->动作实例 jenkins.scm.api.SCMRevisionAction }返回 scmAction?.revision?.hash}

詹金斯给我的错误是这样的:

发生的异常:在字段 com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals在对象 com.cloudbees.groovy.cps.impl.BlockScopeEnv@76bde0fe在字段 com.cloudbees.groovy.cps.impl.CallEnv.caller在对象 com.cloudbees.groovy.cps.impl.FunctionCallEnv@662f031a在字段 com.cloudbees.groovy.cps.Continuable.e在对象 org.jenkinsci.plugins.workflow.cps.SandboxContinuable@1156ea7f在字段 org.jenkinsci.plugins.workflow.cps.CpsThread.program在对象 org.jenkinsci.plugins.workflow.cps.CpsThread@5bada334在字段 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads在对象 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@42bbb563在对象 org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@42bbb563

原因:java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowRun

我正在关注以下内容,但我收到了错误消息.我已经尝试以原始方式运行它,但也没有任何运气.

Jenkinsfile - 获取构建之间的所有更改

解决方案

你看到的异常是由下面这行引起的:

def currentBuild = currentBuild.rawBuild

currentBuild.rawBuild 返回一个不可序列化的对象,因此必须在 @NonCPS 方法内调用以避免出现此异常.尝试简化您的辅助方法,以便它们访问 @NonCPS 内的 currentBuild 变量:

@NonCPSdef getRevisionList(currentCommit, lastSuccessfulCommit) {def 提交 = sh(脚本:git rev-list $currentCommit $lastSuccessfulCommit",返回标准输出:真).split('
')回声$提交"}@NonCPSdef getLastSuccessfulCommit() {def lastSuccessfulHash = nulldef lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()if ( lastSuccessfulBuild ) {lastSuccessfulHash = commitHashForBuild(lastSuccessfulBuild)}返回最后一个成功的哈希}@NonCPSdef commitHashForBuild() {def scmAction = currentBuild?.rawBuild?.actions?.find { action ->动作实例 jenkins.scm.api.SCMRevisionAction }返回 scmAction?.revision?.hash}

<块引用>

currentBuild.rawBuild - 带有 其他 API,仅适用于沙盒外的受信任库或管理员批准的脚本;该值不会是 Serializable 因此您只能在标记为 @NonCPS

的方法中访问它<小时>

来源:https://qa.nuxeo.org/jenkins/pipeline-syntax/globals#currentBuild

The code is fairly simple I just want a rev-list to post to slack. But the part that is causing me the issue is when I am actually trying to get the rev-list from git.

Problem Code

@NonCPS
def getRevisionList(currentCommit, lastSuccessfulCommit) {
     def commits = sh(
        script: "git rev-list $currentCommit $lastSuccessfulCommit",
        returnStdout: true
    ).split('
')
    echo "$commits"
}

Complete Code:

pipeline {
    environment {
      failureMessage = ""
    }

  agent {
        node {
            label 'gsacsp-build02.reisys.com'
        }
    }
    stages {
        stage('Parse Commits') {
            steps {
                script {
                    def currentBuild = currentBuild.rawBuild
                    def currentCommit = commitHashForBuild(currentBuild)
                    def lastSuccessfulCommit =  getLastSuccessfulCommit()

                    getRevisionList(currentCommit, lastSuccessfulCommit)
                }
            }
        }
    }
    post {
        always {
            withCredentials([string(credentialsId: 'BOT_SLACK_HOOK', variable: 'BOT_SLACK_HOOK')]) {
               script {
                    failureMessage = readFile "jenkinshelpers/slackfailuremessage.json"
                    sh "curl -X POST -H 'Content-type: application/json' --data '$failureMessage' $BOT_SLACK_HOOK"
                }
            }
        }
    }
}

//Groovy Helper Methods

@NonCPS
def getRevisionList(currentCommit, lastSuccessfulCommit) {
     def commits = sh(
        script: "git rev-list $currentCommit $lastSuccessfulCommit",
        returnStdout: true
    ).split('
')
    echo "$commits"
}

@NonCPS
def getLastSuccessfulCommit() {
  def lastSuccessfulHash = null
  def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
  if ( lastSuccessfulBuild ) {
    lastSuccessfulHash = commitHashForBuild( lastSuccessfulBuild )
  }
  return lastSuccessfulHash
}

@NonCPS
def commitHashForBuild(build) {
    def scmAction = build?.actions.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
    return scmAction?.revision?.hash
}

The Error Jenkins is giving me is this:

an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@76bde0fe
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@662f031a
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@1156ea7f
in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
in object org.jenkinsci.plugins.workflow.cps.CpsThread@5bada334
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@42bbb563
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@42bbb563

Caused: java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowRun

More of less I am following the below, however I am getting the error. I have tried running it the original way but am not having any luck with that either.

Jenkinsfile - get all changes between builds

解决方案

The exception you see is caused by the following line:

def currentBuild = currentBuild.rawBuild

currentBuild.rawBuild returns a non-serializable object, thus has to be called inside a @NonCPS method to avoid getting this exception. Try simplifying your helper methods so they access currentBuild variable inside @NonCPS:

@NonCPS
def getRevisionList(currentCommit, lastSuccessfulCommit) {
     def commits = sh(
        script: "git rev-list $currentCommit $lastSuccessfulCommit",
        returnStdout: true
    ).split('
')
    echo "$commits"
}

@NonCPS
def getLastSuccessfulCommit() {
  def lastSuccessfulHash = null
  def lastSuccessfulBuild = currentBuild.rawBuild.getPreviousSuccessfulBuild()
  if ( lastSuccessfulBuild ) {
    lastSuccessfulHash = commitHashForBuild( lastSuccessfulBuild )
  }
  return lastSuccessfulHash
}

@NonCPS
def commitHashForBuild() {
    def scmAction = currentBuild?.rawBuild?.actions?.find { action -> action instanceof jenkins.scm.api.SCMRevisionAction }
    return scmAction?.revision?.hash
}

currentBuild.rawBuild - a hudson.model.Run with further APIs, only for trusted libraries or administrator-approved scripts outside the sandbox; the value will not be Serializable so you may only access it inside a method marked @NonCPS


Source: https://qa.nuxeo.org/jenkins/pipeline-syntax/globals#currentBuild

这篇关于Jenkins Pipeline 抛出 java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowRun 即使使用@NonCPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆