title: Jenkins Pipeline emailext: How to access build object in pre-send script
body: <p>I'm using Jenkins ver. 2.150.1 and have some freestyle jobs and some pipeline jobs.
  In both job types I am using the emailext plugin, with template and pre-send scripts.</p>
  
  <p>It seems that the <em>build</em> variable, which is available in the freestyle projects, is <em>null</em> in the pipeline projects.</p>
  
  <p>The pre-send script is the following (just an example, my script is more complex):</p>
  
  <pre><code>msg.setSubject(msg.getSubject() + " [" + build.getUrl() + "]")
  </code></pre>
  
  <p>There is no problem with the msg variable.
  In the freestyle job, this script adds the build url to the mail subject.
  In the pipeline job, the following is given in the job console:</p>
  
  <pre><code>java.lang.NullPointerException: Cannot invoke method getUrl() on null object
  </code></pre>
  
  <p>The invocation of emailext in the pipeline job is:</p>
  
  <pre><code>emailext body: '${SCRIPT, template="groovy-html.custom.pipeline.sandbox.template"}', 
           presendScript: '${SCRIPT, template="presend.sandbox.groovy"}', 
           subject: '$DEFAULT_SUBJECT', 
           to: 'user@domain.com'
  </code></pre>
  
  <p>I would rather find a general solution to this problem (i.e. <strong>Access the <em>build</em> variable in a pipeline pre-send script</strong>), but would also appreciate any workarounds to my current needs:
  <strong>Access <em>job name</em>, <em>job number</em>, and <em>workspace folder</em> in a pipeline pre-send script.</strong></p>