I my pipeline I use properties of ParentJob (the job that triggered current job) multiple times.
I wrote script to evaluate it, but it order to do this only once, I wanted to store it in property outside pipeline, unfortunately this brings NotSerializableException regardless that it actually works.
Maybe you have some ideas, my simplified pipeline:
1. pipeline TestScriptRunner, pipelinescript:
build 'TestScript'
2. pipeline TestScript, pipelinescript:
import jenkins.model.Jenkins
import java.text.*
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.job.WorkflowRun
WorkflowJob ParentJob
pipeline {
agent {
node {
label params.NODE
}
}
stages {
stage ('Initialise properties') {
steps {
echo "Evaluating parent job"
script {
def cause = currentBuild.rawBuild.getCause(hudson.model.Cause$UpstreamCause)
if (cause == null) {
bat """
@ECHO Error: Unknown parent build or current build was triggered by user directly.
@EXIT 1
"""
}
def parentJob = hudson.model.Hudson.instance.getItem(cause.upstreamProject)
//below works fine
echo "Located parent job:\t${parentJob.getFullDisplayName()}"
//below causes the NotSerializableException
ParentJob = parentJob
//below works fine, but there is exception waiting for being thrown at the end of the pipeline
echo "Property ParentJob:\t${ParentJob.getFullDisplayName()}"
}
}
}
}
}