Last active
October 30, 2018 12:10
-
-
Save sonOfRa/2f2940b3095733b39ef669dab9c78c8b to your computer and use it in GitHub Desktop.
Automating releases with gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
apply plugin: "com.github.ben-manes.versions" | |
apply plugin: "se.patrikerdes.use-latest-versions" | |
apply plugin: 'net.researchgate.release' | |
buildscript { | |
repositories { | |
maven { | |
url "https://plugins.gradle.org/m2/" | |
} | |
google() | |
jcenter() | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:3.2.1' | |
classpath "com.github.ben-manes:gradle-versions-plugin:0.20.0" | |
classpath "gradle.plugin.se.patrikerdes:gradle-use-latest-versions-plugin:0.2.3" | |
classpath 'net.researchgate:gradle-release:2.6.0' | |
// NOTE: Do not place your application dependencies here; they belong | |
// in the individual module build.gradle files | |
} | |
} | |
allprojects { | |
repositories { | |
google() | |
jcenter() | |
} | |
} | |
dependencyUpdates { | |
resolutionStrategy { | |
componentSelection { rules -> | |
rules.all { ComponentSelection selection -> | |
if (selection.candidate.group != "com.example") { | |
selection.reject('Only upgrade our own packages!') | |
} | |
} | |
} | |
} | |
checkForGradleUpdate = false | |
revision = "release" | |
} | |
// Only needed for multi-layer projects like all android projects | |
// For single layer things, you can just put these in buildTasks below | |
task build{} | |
build.dependsOn('app:assembleRelease') | |
task test{} | |
test.dependsOn('app:testReleaseUnitTest') | |
release { | |
buildTasks = ['build', 'test'] | |
// Single quotes! The substitution is not done by gradle, it's done by the plugin | |
// If we use double quotes, gradle will substitute, which will cause a tag like | |
// v1.2.3-SNAPSHOT | |
tagTemplate = 'v${version}' | |
preTagCommitMessage = '[Release Automation] - pre tag commit: ' | |
tagCommitMessage = '[Release Automation] - creating tag: ' | |
newVersionCommitMessage = '[Release Automation] - new version commit: ' | |
git { | |
requireBranch = '' | |
// The default for this is 'origin', so you can leave it out if you want to | |
// push to origin always. You can also set it to a fixed remote. | |
// This way, we can push to whatever the currently configured upstream remote | |
// for our release branch is. See https://github.com/researchgate/gradle-release/issues/271 | |
// for more information | |
pushToRemote = "${project.findProperty('remote')}" | |
} | |
} | |
afterReleaseBuild.dependsOn('app:uploadArchives') | |
task clean(type: Delete) { | |
delete rootProject.buildDir | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if ! git symbolic-ref -q HEAD > /dev/null ; then | |
echo "Detached HEAD. Can only release on a branch (git checkout -b develop-v1.2.3.4)" | |
exit 1 | |
fi | |
BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
read -p $'Running release on branch \e[31m'$BRANCH$'\e[0m. Is this correct? [y/N] ' -n 1 -r | |
echo | |
while [[ ! $REPLY =~ ^[YyNn]$|^$ ]]; do | |
read -p "Answer not understood, please answer with y or n" -n 1 -r | |
echo | |
done | |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
echo "Not releasing branch $BRANCH." | |
exit 0 | |
fi | |
if ! REMOTE=$(git config branch.$BRANCH.remote) ; then | |
echo "Branch $BRANCH does not track a remote! For gradle release-plugin to work, you need to track a remote." | |
echo "List remotes with 'git remote -v', then run 'git branch -u \$REMOTE/$BRANCH' to track the correct remote" | |
echo "If $BRANCH does not exist on the remote, run 'git push -u \$REMOTE $BRANCH' instead." | |
exit 1 | |
fi | |
echo "Fetching remote changes. Remember to enable VPN, or this will fail." | |
git fetch $REMOTE | |
# Rev list shows the differences between local commits and remote commits | |
DIFFERENCES=`git rev-list --count --left-right $BRANCH...$REMOTE/$BRANCH` | |
DIFFERENCES_NO_ZERO="$(echo -e ${DIFFERENCES} | sed -e 's/0//g')" | |
DIFFERENCES_NO_SPACE="$(echo -e ${DIFFERENCES_NO_ZERO} | sed -e 's/[[:space:]]//g')" | |
# Before releasing, we have to either push or pull remote changes | |
if [[ ! -z "$DIFFERENCES_NO_SPACE" ]]; then | |
echo "There are local or remote changes. See git status for more information" | |
exit 1 | |
fi | |
# We do not want untracked files | |
git ls-files --other --directory --exclude-standard | sed q1 --quiet || { echo "There are untracked files. Delete them or add them to git" && exit 1; } | |
# We do not want untracked changes | |
git diff --quiet || { echo "There are unstaged changes. Please remove or commit them" && exit 1; } | |
# We do not want tracked changes | |
git diff --cached --quiet || { echo "There are staged changes. Please commit them" && exit 1; } | |
# Clean the build before releasing | |
./gradlew clean || { echo "gradle clean failed" && exit 1; } | |
# Update versions | |
./gradlew useLatestVersions || { echo "Failed to run update versions task" && exit 1; } | |
# Check if version updates were applied | |
./gradlew useLatestVersionsCheck || { echo "Something went wrong when applying new dependency versions" && exit 1; } | |
if ! git diff --quiet ; then | |
git add app/build.gradle || { echo "Failed to stage dependency updates" && exit 1; } | |
git commit -m "[Release Automation] Dependency Updates" || { echo "Failed to commit dependency updates" && exit 1; } | |
git push || { echo "Failed to push dependency updates" && exit 1; } | |
fi | |
./gradlew --console=plain release -Premote=$REMOTE || { echo "gradle release failed" && exit 1; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version=1.1.8-SNAPSHOT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment