Search code examples
gitjenkinsversion-controlrevision

Jenkins: how to access the git revision after build has finished


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.

The general flow is:

  • Start a job with several parameters including git_identifier which is used in building the git url for src checkout
  • After some verifications a checkout is performed on git_identifier (i know it is the same as before but nevertheless)
  • In certain situations (verification fail, business rules, etc) checkout master instead of the passed git_identifier
  • Job finishes and i need to create some audit logs.
  • The job execution is monitored by external system

Questions:

  • What is exactly recorded in lastBuiltRevision?
  • 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?

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.

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.

Any hints are greatly appreciated.


Solution

  • What is exactly recorded in lastBuiltRevision?

    lastBuiltRevision is the commit checked out by Git plugin when the build starts, i.e. it is the SHA1 of the commit pointed by the treeish configured in Branches to build field.

    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?

    lastBuiltRevision might be the git_identifier but only, if you have configured $git_identifier into Branches to build field.

    Anyhow, if you check out another commit later on in a build step, Jenkins does not have any record on this: lastBuiltRevision will stay in the revision originally checked out by the Git plugin.

    If you want to record this information yourself, here are some options:

    • You could echo the checked out revision into a text file and archive this file as a artifact
    • Or you could abuse the build name or the build description and include your final checked out revision there
    • Or maybe you could write some clever system groovy scripts to manipulate the build meta-data