Last Updated: September 04, 2018
·
1.77K
· newtriks

Versioning a Rails app with Git and Heroku

Using a combination of resources I use this shell script to compile an incremented tag release of a Rails app. The script creates or bumps the existing version, creates the git tag, update's the heroku env config variables and also adds the version to $taggedreleaseversion that's pulled in by the rails app and added into the app footer:

<%= $tagged_release_version.split(/-/)[0] %>

There was one particular blog post I gleamed a lot of this logic but I can't find the damned link. As soon as I re-discover I will update and offer props here.

version.sh

    #!/bin/bash
REVISION=$(heroku config:get REVISION)
echo " info: heroku REVISION: $REVISION"
function version_release
{
    echo -e " info: writing: '\$tagged_release_version = \"$1\"' to version_helper.rb"
    echo -e "\$tagged_release_version = \"$1\"" > config/initializers/version_helper.rb 
    git add config/initializers/version_helper.rb
    git commit -am "Updating version_helper with latest tagged version $1"
    echo " info: deployed version $REVISION is not yet tagged; tagging now as $1"
    git tag -a $1 -m "Deployed $(LANG=en_GB.UTF-8; date '+%d %B %Y')."
    echo " info: updating heroku REVISION to: $(git describe)"
    heroku config:add REVISION=$(git describe)
}
if [ ! $(git describe 2> /dev/null) ]; then
    if [ "$#" -ne 3 ]; then
      echo " error: no previous git tags and no semantic version parameters provided e.g. 0 0 1" >&2
      return 1
    fi
    echo " warning: no previous git tags, creating first version using params: $1.$2.$3"
    version_release "$1.$2.$3"
elif [ ! $(git describe --exact-match REVISION 2> /dev/null) ]; then
    version_release $(git describe $(git rev-list --tags --max-count=1) | awk -F . '{ printf "%d.%d.%d", $1, $2, $3 + 1}')
else
    echo " info: local git tag and remote REVISION match, exit versioning"
fi