Gradle VersionName

Generate Version Name

Gradle code to generate version names:

//gradle def on :project

def gitSha() {
    return 'git rev-parse --short HEAD'.execute().text.trim()
}


def generateVersionCode() {
    def versionText = "git --git-dir=.git --work-tree=. rev-list --count HEAD".execute().text


    def versionCode = versionText.length() > 0 ? versionText.toInteger() : 99999


    println "Set version code to: ${versionCode}"
    return versionCode
}


def generateVersionName() {
    def versionName = 'git describe --tags'.execute().text.trim().replace('v', '')
    println "Set version name to: ${versionName}"
    return versionName
}


def generateLatestReleaseVersionName() {
    def lastVersionTagHash = "git rev-list --tags --max-count=1".execute().text
    def versionName = "git describe --tags ${lastVersionTagHash}".execute().text.trim().replace('v', '')
    println "Latest release version name: ${versionName}"
    return versionName
}


def generateDogfoodVersionName(suffix = "") {
    def versionName = generateLatestReleaseVersionName()
    if (versionName.length() <= 3) {
        versionName = versionName + '.0'
    }
    versionName = versionName + '.1' + suffix
    println "Set dogfood version name to: ${versionName}"
    return versionName
}

For Bitrise we need to run a script to fetch tags first before generateVersionName()

Last updated