java.io.NotSerializableException: hudson.model.FreeStyleProject

I've made some tests, cause I've the same issue in my pipeline jobs (for Matrix).

The solution is to put def before job or the name of Object (here Job) before your declaration.

After the NotSerializableException disappear.

If this does not solve your problem, you can also add @NonCPS flag before your declaration. See the official documentation: serializing-local-variables.


Jenkins stages needs to be serialized so that they can survive job & jenkins restarts. Now if you have some variable which have NotSerializable still exists after the stage [Pipeline] // stage then we get this error.

So we should set the variable to null before it touches the stage boundary

further read https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#serializing-local-variables


Add the following:

job = null

after

newJob = job.getLastBuild()