sg0133
sg0133

Reputation: 79

Not serializable error for jenkins declarative pipeline

I am trying to trigger my email promotion job from my pipeline which extracts the repo name from Jenkins messages. But not able to resolve the SerializableException error for this block. Any help is greatly appreciated.

post{
        success{
            script{
                @NonCPS
                //upstream_job_name = null
                def manager = manager.getLogMatcher('.*Obtained Jenkinsfile from git (.*)$')
                if(manager.matches()){
                    def gitMsg=manager.group(1)
                    gitrepo = "${gitMsg}"
                    echo gitrepo
                    def upstream_job_name = gitrepo.split("/")[4].replace(".git", "")
                    println upstream_job_name

                }
                build job: 'job-approval' , parameters: [[$class: 'StringParameterValue', name: 'upstream_job_name', value: upstream_job_name]]


            }
        }
    }

Below is the error messages i am receiving :

[Pipeline] // script Error when executing success post condition:

java.io.NotSerializableException: java.util.regex.Matcher at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926) 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)

Upvotes: 1

Views: 2427

Answers (2)

Supun Sandaruwan
Supun Sandaruwan

Reputation: 2418

I got the same NotSerializableException, Marshalling errors for another reason when executing shell commands. Please check if this is the reason in your code as well.

====== Code that gives the above errors ======

stage("Update the Route53 record") {
    steps {
        script {

            // 1st command
            def albDnsNameCMD = """ aws elbv2 ..."""
            def albDNSProcess = ["bash", "-c", albDnsNameCMD].execute()
            albDNSProcess.waitFor()
            def dnsNameExitCode = albDNSProcess.exitValue()
            def albDnsName = albDNSProcess.text.trim()

        if (dnsNameExitCode != 0) {
            error "Failed to retrieve '${albName}' ALB DNS Name"
        }
        println("Successfully fetched the '${albName}' ALB DNS Name: '${albDnsName}'")

            // 2nd command
            String updateCommand = """aws elbv2 ..."""
            def updateProcess = ["bash", "-c", updateCommand].execute()
            updateProcess.waitFor()
            def updateExitCode = updateProcess.exitValue()

            if (updateExitCode != 0) {
                error "Failed to update Route 53 record. Exit Code: $updateExitCode"
            }

            println("Route 53 record updated successfully.")
        }
    }
}

====== Code that was able to fix the above issues ======

stage("Update the Route53 record") {
    steps {
        script {

            // 1st command ###THIS PART HAS BEEN CHANGED
            def albDnsName = sh(script: """aws elbv2""",returnStdout: true).trim()

            if (!albDnsName) {
                error "Failed to retrieve '${albName}' ALB DNS Name."
            }
            println("Successfully fetched the '${albName}' ALB DNS Name: '${albDnsName}'")

            // 2nd command
            String updateCommand = """aws elbv2 ...."""
            def updateProcess = ["bash", "-c", updateCommand].execute()
            updateProcess.waitFor()
            def updateExitCode = updateProcess.exitValue()

            if (updateExitCode != 0) {
                error "Failed to update Route 53 record. Exit Code: $updateExitCode"
            }

            println("Route 53 record updated successfully.")
        }
    }
}

====== Summary ======

I faced this when executing the shell Command execution. Let's consider the below types.

  • Type A => def Process = ["bash", "-c", CMD].execute()
  • Type B => def dnsName = sh(script: """aws elbv2""",returnStdout: true).trim()

If we write Type A command execution as 1st And the 2nd command execution as either Type A or Type B, it will lead to the above issues. To avoid those issues I use Type B shell command execution.

If we write more command executions we must follow Type B Command execution method.

Upvotes: 0

yong
yong

Reputation: 13712

You need to release manager immediately after using. More detail can find in this post

script{

    //upstream_job_name = null
    def manager = manager.getLogMatcher('.*Obtained Jenkinsfile from git (.*)$')
    if(manager.matches()){
        def gitMsg=manager.group(1)
        gitrepo = "${gitMsg}"
        echo gitrepo
        def upstream_job_name = gitrepo.split("/")[4].replace(".git", "")
        println upstream_job_name      
    }
    manager = null

    build job: 'job-approval' , 
        parameters: [
            [$class: 'StringParameterValue', name: 'upstream_job_name', value: upstream_job_name]
        ]
}

Upvotes: 4

Related Questions