Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline throwing java.io.NotSerializableException: org.jenkinsci.plugins.workflow.job.WorkflowRun even with @NonCPS

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('\n')
    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('\n')
    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

like image 883
Ekalbs Avatar asked Aug 01 '18 15:08

Ekalbs


1 Answers

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('\n')
    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

like image 90
Szymon Stepniak Avatar answered Nov 15 '22 10:11

Szymon Stepniak