Skip to content

Instantly share code, notes, and snippets.

@jinqian
Created February 14, 2017 21:47
Show Gist options
  • Save jinqian/86d6d714788f66e59dea33b255db77ff to your computer and use it in GitHub Desktop.
Save jinqian/86d6d714788f66e59dea33b255db77ff to your computer and use it in GitHub Desktop.
Gradle Script for auto version incrementing, git tagging, and deploying to Beta by Crashlytics
/*
* Copyright © 52inc 2015.
* All rights reserved.
*/
apply plugin: 'org.ajoberstar.grgit'
import groovy.json.*
def versionFile = file("../version.json")
def versionJSON = getJSON(versionFile)
def versionCode = versionJSON.buildNumber
def versionName = "${versionJSON.major}.${versionJSON.minor}.${versionJSON.revision}"
// Expose as extra properties at project level
ext.versionCode = versionCode
ext.versionName = versionName
/***********************************************************************************************
*
* Methods
*
*/
def getJSON(file) {
return new JsonSlurper().parseText(file.text)
}
/***********************************************************************************************
*
* Tasks
*
*/
task prepareAlphaRelease << {
// Ensure our working copy is clean first
if (!grgit.status().isClean()) {
throw new GradleException("You must NOT have any changes in your working copy!")
}
// Update version code
versionJSON.buildNumber += 1
versionCode = versionJSON.buildNumber
versionFile.write(new JsonBuilder(versionJSON).toPrettyString())
// Apply version code to all variants. This is necessary
// so when we build the APK, it gets the updated values
android.applicationVariants.all { variant ->
variant.mergedFlavor.versionCode = versionCode
variant.mergedFlavor.versionName = "${variant.name}-${versionName}"
}
// Add changes
def changes = grgit.status().unstaged.getAllChanges()
grgit.add(update: true, patterns: changes)
// Commit
grgit.commit(message: "Prepare for Alpha release")
// Push
grgit.push()
// Tag
def tagName = "alpha-v${versionName}.${versionCode}"
grgit.tag.add(name: tagName, message: "Alpha Release ${tagName}")
// Push
grgit.push(refsOrSpecs: [tagName])
}
task prepareBetaRelease << {
// Ensure our working copy is clean first
if (!grgit.status().isClean()) {
throw new GradleException("You must NOT have any changes in your working copy!")
}
// Update version code
versionJSON.buildNumber += 1
versionCode = versionJSON.buildNumber
versionFile.write(new JsonBuilder(versionJSON).toPrettyString())
// Apply version code to all variants. This is necessary
// so when we build the APK, it gets the updated values
android.applicationVariants.all { variant ->
variant.mergedFlavor.versionCode = versionCode
}
// Add changes
def changes = grgit.status().unstaged.getAllChanges()
grgit.add(update: true, patterns: changes)
// Commit
grgit.commit(message: "Prepare for Beta release")
// Push
grgit.push()
// Tag
def tagName = "beta-v${versionName}.${versionCode}"
grgit.tag.add(name: tagName, message: "Beta Release ${tagName}")
// Push
grgit.push(refsOrSpecs: [tagName])
}
/***********************************************************************************************
*
* Alpha Release Task
*
*/
task releaseAlpha(dependsOn: ['prepareAlphaRelease', 'crashlyticsUploadDistributionAlphaRelease'])
tasks.whenTaskAdded { task ->
if (task.name.equals("crashlyticsUploadDistributionAlphaRelease")) {
task.mustRunAfter prepareAlphaRelease
}
}
/***********************************************************************************************
*
* Beta Release Task
*
*/
task releaseBeta(dependsOn: ['prepareBetaRelease', 'crashlyticsUploadDistributionBetaRelease'])
tasks.whenTaskAdded { task ->
if (task.name.equals("crashlyticsUploadDistributionBetaRelease")) {
task.mustRunAfter prepareBetaRelease
}
}
{
"major": 1,
"minor": 0,
"revision": 0,
"buildNumber": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment