Using Git to display your web app’s version

I’m mainly writing this down for my own benefit, but others might find this useful. In a previous post about setting up git to deploy to a remote web server, I wrote about a way to display the lastest abbreviated commit hash in your web app’s footer.

Today, I discovered that simply running:

git describe

will output the most recent tag and the current abbreviated commit hash (if there are commits after the tag), so you can use tags to show more human-readable versions of your app (v1.0, etc.) along with the current commit. Unfortunately, you apparently can’t run ‘git describe’ on a bare git repository like what I’m using for deployment on the remote server.

(local) Lamont Audio Archive

My solution for now (although I think there has to be a better way) is to create my own little shell script that will run a “git push”, overwrite a local version.txt file with the current tag/commit hash, then use SCP to upload the file to the environment I just pushed to.

The version.txt and push scripts (“./testpush”, “./prodpush”) are all left out of the repository and stored in the base folder of my app.

[Previous script removed}

Updated (3/7/2013):

A single, more efficient script simply called "push". Enter "./push [environment]” to do everything above to the designated environment:

#!/bin/bash

VERSION_FILE=version.txt
GIT_PATH=/usr/local/git/bin/git
REMOTE_PATH=[full remote path to application root]/$VERSION_FILE
LOCAL_PATH=[full local path to application root]/$VERSION_FILE
TEST_SERVER=[test ip/domain]
PROD_SERVER=[prod ip/domain]

if [ -z "$1" ]; then

  echo 'no server parameter given'
  exit

else

  if [ $1 == "test" ]; then
    SERVER=$TEST_SERVER
    elif [ $1 == "prod" ]; then
    SERVER=$PROD_SERVER
  else
    echo 'invalid server parameter'
    exit
  fi

  $GIT_PATH push $1
  echo "pushed to $1"
  $GIT_PATH describe > $LOCAL_PATH
  echo "updated version.txt"
  scp $LOCAL_PATH $SERVER:$REMOTE_PATH
  echo "copied version.txt to $1"
  exit

fi

https://gist.github.com/rufhausen/5114025

I’d like to somehow roll this functionality into a git hook instead of needing to create my own scripts, but I just haven’t figured that out yet.