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
Seamlessly Integrating Micro Apps with iFrame
A recent client wanted to upgrade a small portion of their legacy application with a more modern UI and extra functionality, like fuzzy text search. There are a few approaches to incremental upgrades of legacy […]
Consul on Docker EE
[markdown]”Consul is a service networking solution to connect and secure services across any runtime platform and public or private cloud” – [https://consul.io](https://consul.io) This post details a way to run a Consul cluster on Docker EE […]
Passing the AWS Machine Learning Speciality Exam
The Amazon Machine Learning Specialty Exam is a 3-hour, 65 question test. It is designed to test your skills in AWS specific Data Engineering and Machine Learning Practices along with Machine Learning in general. I […]
Consistent Python environments with Poetry and pre-commit hooks
Clean and Consistent Environments Regardless of the programming language you are working in, it can sometimes be a struggle to maintain a clean codebase and a consistent development environment for all members of your team, […]