title: Accessing the current Jenkins build in Groovy script
body: <p>I have created a Groovy script which is used in a <code>System Groovy Script</code> step in a Jenkins job which needs to access the current build of the current job.  </p>
  
  <p>The current build is required when using an Hudson.model <code>Cause.UpstreamCause</code> to link the current build of my current job to a dependent job that I am scheduling.  </p>
  
  <p>Since code is more concise:</p>
  
  <p><code>my-job-step.groovy</code>:</p>
  
  <pre><code>def scheduleDependentJob(jobName) {
    def fooParam = new StringParameterValue('foo', 'bar');
    def paramsAction = new ParametersAction(fooParam)
  
    println "Scheduling dependent job"
    def currentJob = ???
    def cause = new Cause.UpstreamCause(currentBuild)
    def causeAction = new hudson.model.CauseAction(cause)
    instance.queue.schedule(job, 0, causeAction, paramsAction)
  }
  </code></pre>
  
  <p>The <code>CauseAction</code> constructor (Seen on <a href="http://javadoc.jenkins-ci.org/hudson/model/Cause.UpstreamCause.html" rel="noreferrer">http://javadoc.jenkins-ci.org/hudson/model/Cause.UpstreamCause.html</a>) requires a <code>Run</code> object, which the current build object should be an instance of.  I just can't find a good way to get the current running job build inside of a Groovy script.</p>