Jul 17, 2018

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.

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

Leave a Reply

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

Related Blog Posts
Up to Spec: JavaScript Numeric Separators
Let's take a look at the proposal to add Numeric Separators to the JavaScript specification.
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]