morristech
11/21/2017 - 10:50 AM

Gradle function to get the current Git branch

Gradle function to get the current Git branch

def getCurrentGitBranch() {
    def gitBranch = "Unknown branch"
    try {
        def workingDir = new File("${project.projectDir}")
        def result = 'git rev-parse --abbrev-ref HEAD'.execute(null, workingDir)
        result.waitFor()
        if (result.exitValue() == 0) {
            gitBranch = result.text.trim()
        }
    } catch (e) {
    }
    return gitBranch
}