Cannot invoke method getLastSuccessfulBuild() on null object

6.9k views Asked by At

Unable to retrieve Jenkins job last successful job variables or Parameters via Groovy script so that I can pass these to next job as a variable.

import jenkins.model.Jenkins
def job = Jenkins.instance.getJob("Testing-4")
def run = instance.getItems()
println run.getEnvironment()["PipelineId"]

Error logs

    ERROR: Build step failed with exception
java.lang.NullPointerException: Cannot invoke method getLastBuild() on null object
    at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at Script1.run(Script1.groovy:3)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:580)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:618)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:589)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:168)
    at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
    at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
    at hudson.model.Build$BuildExecution.build(Build.java:205)
    at hudson.model.Build$BuildExecution.doRun(Build.java:162)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
    at hudson.model.Run.execute(Run.java:1738)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:410)
2

There are 2 answers

4
slim On

From JavaDoc for Jenkins.getItem():

Returns: an item whose Item.getName() is name and whose Item.getParent() is this, or null if there is no such item, or there is but the current user lacks both Item.DISCOVER and Item.READ.

For you, getItem("Testing-4") is returning null, so either there is no item with the name "Testing-4", or you are not logged in as a user with the appropriate permissions.

You can get a list of items with getItems(), which would be useful in an exploratory script to find out what the items available to you look like.

1
asur On
import hudson.model.*;

def lastSuccessfulPipelineID = build.getProject().getLastSuccessfulBuild().properties.get("envVars")['PipelineNumber']
def pa = new ParametersAction([
  new StringParameterValue("lastSuccessfulPipelineID", lastSuccessfulPipelineID)
])
println(lastSuccessfulPipelineID)

build.addAction(pa)