Skip to content

Instantly share code, notes, and snippets.

@CharlesHolbrow
Created March 2, 2018 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlesHolbrow/c3a8d5fe8cf1ee92caa6deb2c9b32413 to your computer and use it in GitHub Desktop.
Save CharlesHolbrow/c3a8d5fe8cf1ee92caa6deb2c9b32413 to your computer and use it in GitHub Desktop.
minimal build version tracking
#!/bin/bash
# This is a simple script that increments the integer in a the 'build-version'
# file, and makes a new commit. The new commit will be tagged with the branch
# and version
#
# You may specify a path. If no path is specified, the working directory will be
# used.
#
# Eventually we might want to replace this with a proper CI pipline, but this
# is a reasonable placeholder for now.
# check if the user supplied a path to cd into
if [ $# -eq 0 ]
then
echo "No argument supplied. using current dir: `pwd`"
else
cd $1 || exit 1
fi
# Check if git working dir is clean
#
# We do not want to proceed if there are changes, because we will add and commit
# the new build-version file.
if [[ `git status --porcelain` ]] # double braces ok in bash, may not work in sh
then
echo "Working dir is not clean. Aborting."
exit 1
fi
# ensure that the build-version file exists
if [ ! -e build-version ]
then
echo "Creating build-version file"
echo "0" > build-version
git add build-version
fi
# Get the current version from the file
V=$(head -1 build-version)
# Validate build-version
if [ "$V" ! -eq "$V" ] 2>/dev/null
then
echo "Previous version: $V"
echo "Previous version must be an integer"
exit 1
fi
# Increment build-version
V_NEXT=$(($V + 1))
echo "Updated ver: $V_NEXT"
echo $V_NEXT > build-version
# Build that updated tag name.
BRANCH=$(git symbolic-ref --short -q HEAD)
TAG="$BRANCH-v$V_NEXT"
echo "Updated tag: $TAG"
echo
# commit the updated build-version
echo "Committing and tagging..."
git add build-version
git commit -m "Update build version to $V_NEXT" || exit 1
git tag $TAG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment