title: Jenkins: how to access the git revision after build has finished
body: <p>I have a freestyle (shell) jenkins job that in certain situations will checkout a different git tree than the one with which it was started. The reason behind this is procedural requirements.</p>
  
  <p>The general flow is:</p>
  
  <ul>
  <li>Start a job with several parameters including git_identifier which is used in building the git url for src checkout</li>
  <li>After some verifications a checkout is performed on git_identifier (i know it is the same as before but nevertheless)</li>
  <li>In certain situations (verification fail, business rules, etc) checkout master instead of the passed git_identifier</li>
  <li>Job finishes and i need to create some audit logs.</li>
  <li>The job execution is monitored by external system</li>
  </ul>
  
  <p>Questions:</p>
  
  <ul>
  <li>What is exactly recorded in <code>lastBuiltRevision</code>?</li>
  <li>If this is the sha1 of the git_identifier passed as part of the build parameters how can i get the sha1 of the state of the checkout (detached head) after the build has completed?</li>
  </ul>
  
  <p>Please assume that several (not small, not large) number of job results are kept by jenkins but the job we are interested in might be overwritten. Workspace content is not kept and we prefer it to stay this way.</p>
  
  <p>Notification of the external monitoring system from jenkins is not feasible. They are accessible but some additional data in the external system cannot be bound to the jenkins data. I need to get few additional bits of information to record in the audit log.</p>
  
  <p>Any hints are greatly appreciated.</p>