Skip to content

Instantly share code, notes, and snippets.

@petitJAM
Last active November 27, 2017 17:15
Show Gist options
  • Save petitJAM/1ecf7485b81784cb3155574cf189f059 to your computer and use it in GitHub Desktop.
Save petitJAM/1ecf7485b81784cb3155574cf189f059 to your computer and use it in GitHub Desktop.
Gradle - Build Time / Git Hash

build.gradle

apply from: 'functions.gradle'

...

buildConfigField 'String', 'GIT_HASH', "\"${gitHash()}\""
buildConfigField 'String', 'BUILD_TIME', "\"${buildTime()}\""

functions.gradle

import java.text.SimpleDateFormat

ext.buildTime = { ->
    def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'")
    df.setTimeZone(TimeZone.getTimeZone("UTC"))
    return df.format(new Date())
}

ext.gitHash = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', '--short', 'HEAD'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment