Using Git within Gradle

I had a task to have a Gradle build look at the Git branch name and set a different variable based on what branch was. I could have made a system call to git but I have learned not to depend on system calls. A little searching and I found grgit and it was pretty easy:

buildscript {
    repositories {
      jcenter()
  }
  dependencies {
    classpath "org.ajoberstar:grgit:2.2.1"
  }
}
 
apply plugin: 'java'
apply plugin: "org.ajoberstar.grgit"
 
//....
 
def getWorkingBranch(){
    ext.repo = grgit.open()
 
    return ext.repo.branch.current.name
 
}

Then, naturally, I just call getWorkingBranch in my tasks when I need the branch name.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*