The following will give you the latest git tag name (if the HEAD is tagged) or latest commit ID otherwise. The getAppVersion
function returns the end value.
I use it to name packages of my builds and other versioning purposes in my Android projects.
def getAppVersion = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--short', 'HEAD'
standardOutput = stdout
}
def commitId = stdout.toString().replace("\n", "").replace("\r", "").trim()
stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'tag', '--points-at', commitId
standardOutput = stdout
}
def tagName = stdout.toString().replace("\n", "").replace("\r", "").trim()
def versionName = 'git-' + commitId
if (tagName != null && "" != tagName) {
versionName = tagName
}
return versionName
}
A KTS version