Skip to content

Instantly share code, notes, and snippets.

@mbucc
Last active June 28, 2018 11:41
Show Gist options
  • Save mbucc/d0fa7c7970b560cc214846158f0cd85a to your computer and use it in GitHub Desktop.
Save mbucc/d0fa7c7970b560cc214846158f0cd85a to your computer and use it in GitHub Desktop.
gradle build script: increment build number (1.0.0-build23), commit and tag repo
#! /bin/sh -e
#
# Build Gradle distribution tarball. Crank version and tag repo if code has changed since last build.
#
# Abort if uncommitted changes.
# Maintain build number (in build.log file) and increment if code has changed since last build.
# When build number changes, update version in build.gradle and tag repo.
# Version number pattern in build.gradle: version = "1.0.0-build31"
#
# Usage: ./build.sh
#
#
# Define which file stores the build number log.
# Log contains three tab-delimited columns: (build_num, build_node_sha1, build_date)
#
BUILDLOG=build.log
#
# Prefix used on commmit messages; e.g, "buildbot: build new release 1.0.0-build31"
#
BOTNAME=buildbot
#
# If 'Y' crank build number, tag, etc.
#
NEW_TAG_NEEDED='N'
#
# Abort if uncommitted changes. Annoying as hell but the guarantee is nice.
#
# Ignore .iml files.
# IntelliJ updates those with the new build num. some time after build.gradle changes.
#
N=$(git status -s|grep -v '\.iml'|wc -l)
[ $N -ne 0 ] && echo "$(basename $0): uncommitted changes or untracked files" && exit 1
#
# We need a new tag if SHA1 in build log doesn't match HEAD~1.
#
#
# (D) <---- buildbot: add 1.0.0-build37 to build log.
# | * Tag as build/1.0.0-build37
# | * Note: The SHA1 (C) is written to build.log
# |
# (C) <---- buildbot: release 1.0.0-build37
# | * build.gradle with new version
# |
# (B) <---- commit a new feature
# |
# |
# (A) <---- buildbot: add 1.0.0-build36 to build log.
# * Tag as build/1.0.0-build36
#
#
# All this so running ./build.sh after a clone will not create a new tag.
#
if [ ! -f $BUILDLOG ]
then
NEW_TAG_NEEDED='Y'
BUILDNUM=0
else
LASTLINE=$(tail -1 $BUILDLOG)
BUILDNUM=$(echo "$LASTLINE" | cut -f 1)
SHA1=$(echo "$LASTLINE" | cut -f 2)
BUILDDATE=$(echo "$LASTLINE" | cut -f 3)
X=$(git rev-parse HEAD~1)
[ "x$X" != "x$SHA1" ] && NEW_TAG_NEEDED='Y'
fi
if [ "x$NEW_TAG_NEEDED" = "xY" ]
then
# Crank build number in build.gradle and commit.
X=$(grep ^version build.gradle)
OLD=$(echo $X | cut -d = -f 2 |tr -d "'"|tr -d '"'|sed 's/-build.*//'|tr -d ' ')
BUILDNUM=$((BUILDNUM + 1))
NEW="$OLD-build$BUILDNUM"
cat build.gradle | sed "s/^version.*/version = \"$NEW\"/" > t
mv t build.gradle
git commit -a -m "$BOTNAME: release $NEW"
# Write line to build log and commit.
printf "%s\t%s\t%s\n" $BUILDNUM $(git rev-parse HEAD) $(date +"%FT%T-%Z") >> build.log
git add build.log
git commit -a -m "$BOTNAME: update build.log with $NEW"
# Tag builds/X.Y.Z-buildN
git tag -a -m "$BOTNAME" builds/$NEW
fi
#
# Build Gradle distribution tarball.
#
./gradlew clean
./gradlew distTar
echo "built tarball $(find ./build/distributions -type f)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment