Skip to content

Instantly share code, notes, and snippets.

@jeffnelson
Created October 25, 2018 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffnelson/c5e687ff046d6a1f1c31039db3bcc907 to your computer and use it in GitHub Desktop.
Save jeffnelson/c5e687ff046d6a1f1c31039db3bcc907 to your computer and use it in GitHub Desktop.
jenkins groovy script to get the build result of a given build's upstream cause
import jenkins.model.Jenkins
import hudson.model.AbstractItem
import hudson.model.Cause
// === this code enables you to run this whole thing ===
// === just in https://myjenkins.me/script console ===
def currentBuild = Jenkins.instance.getAllItems(AbstractItem.class)
.findAll{ it.fullName == "my/org/job-name" }
.collect{ it.builds.findAll{ it.number == 3 } } // desired build number of "my/org/job-name"
.flatten()[0]
// =====================================================
// the below code can be used in a groovy script in a jenkins job because 'currentBuild' is available in context
Cause.UpstreamCause cause = currentBuild.getCause(Cause.UpstreamCause.class)
def upstreamTriggerBuildResult = Jenkins.instance.getAllItems(AbstractItem.class)
.findAll{ it.fullName == cause.getUpstreamProject() }
.collect{ proj -> proj.getBuilds().findAll{ it.number == cause.getUpstreamBuild() }.collect{ it.getResult() } }
.flatten()[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment