Pipeline: Is SCM revision information stored with each build?

27 views
Skip to first unread message

Dallas Clement

unread,
Aug 16, 2017, 1:46:46 PM8/16/17
to Jenkins Users
I know that I can get the latest revision for the current build by executing a shell command such as:
      def currentCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()

But I would like to get the commit hash associated with a previous build.  The reason I want to do this is so that I can list all commits associated with a given build since the previous build.

Dallas Clement

unread,
Aug 16, 2017, 7:01:20 PM8/16/17
to Jenkins Users
Answering my own question again.  It looks like there is some API available for getting SCM revision info in a groovy sandbox environment. 

def getChanges() {
  def lines = []
  def changeSets = currentBuild.changeSets
  for (int i = 0; i < changeSets.size(); i++) {
    def entries = changeSets[i].items
    for (int j = 0; j < entries.length; j++) {
      def entry = entries[j]
      lines.add("${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}")
      def files = new ArrayList(entry.affectedFiles)
      for (int k = 0; k < files.size(); k++) {
        def file = files[k]
        lines.add("\t${file.editType.name} ${file.path}")
      }
    }
  }
  def string = ''
  for (i = 0; i < lines.size(); i++) {
    string += lines[i] + '\n'
  }
  return string
}

Trying to get this info from currentBuild.rawBuild seems nearly impossible due to sandbox / security restrictions.
Reply all
Reply to author
Forward
0 new messages