Git Auto Increment tag




If you want GIT tag autoIncrement with one version number after each build you can use below shell script(./gittag.sh) as the hook.

Scrip will find last committed TAG which starts with ENV_TAG_PREFIX (QA_V) and creates next release TAG then push tag to GIT.


VERSION_MODE  [major|minor|revision|buildNumber - These are the possible value. each time one version is a tag with GIT. By default, buildNumber is pass which increment last digit in release tag like(x.x.x.1 to x.x.x.2). So if you want major release then you can pass major as option which tag GIT with major release  like (1.x.x.x to 2.x.x.x)]

e.g. ./gittag.sh major
      ./gittag.sh minor




#!/bin/bash  
 VERSION_MODE=$1  
 ENV_TAG_PREFIX=QA_V
 # "major": 1,  
 # "minor": 0,  
 # "revision": 0,  
 # "buildNumber": 1  
 if [ "$VERSION_MODE" = "" ] ; then  
      VERSION_MODE="buildNumber"  
 fi  
 cd /path/to/your/git/checkout  
 #get highest tag number  
 VERSION=`git describe --match "$ENV_TAG_PREFIX[0-9]*" --abbrev=0 --tags`  
 #replace . with space so can split into an array  
 VERSION_BITS=(${VERSION//./ })  
 #get number parts and increase last one by 1  
 VNUM1=${VERSION_BITS[0]}  
 if [ "$VNUM1" = "" ] ; then  
      VNUM1=0;  
 fi  
 VNUM2=${VERSION_BITS[1]}  
 if [ "$VNUM2" = "" ] ; then  
      VNUM2=0;  
 fi  
 VNUM3=${VERSION_BITS[2]}  
 if [ "$VNUM3" = "" ] ; then  
      VNUM3=0;  
 fi  
 VNUM4=${VERSION_BITS[3]}  
 if [ "$VNUM4" = "" ] ; then  
      VNUM4=0;  
 fi  
 case $VERSION_MODE in  
      "major")  
           VNUM1=$((VNUM1+1))  
           ;;  
      "minor")  
           VNUM2=$((VNUM2+1))  
           ;;  
      "revision")  
           VNUM3=$((VNUM3+1))  
           ;;  
     "buildNumber" )  
           VNUM4=$((VNUM4+1))  
           ;;  
 esac  
 #create new tag  
 NEW_TAG="$ENV_TAG_PREFIX$VNUM1.$VNUM2.$VNUM3.$VNUM4"  
 echo "Last tag version $VERSION New tag will be $NEW_TAG"  
 #get current hash and see if it already has a tag  
 GIT_COMMIT=`git rev-parse HEAD`  
 NEEDS_TAG=`git describe --contains $GIT_COMMIT 2>/dev/null`  
 echo "###############################################################"  
 #only tag if no tag already (would be better if the git describe command above could have a silent option)  
 if [ -z "$NEEDS_TAG" ]; then  
   echo "Tagged with $NEW_TAG (Ignoring fatal:cannot describe - this means commit is untagged) "  
   git tag -a $NEW_TAG -m "$VERSION_MODE"  
   git push --tags  
 else  
   echo "Current commit already has a tag $VERSION"  
 fi  
 echo "###############################################################"  


Please note: I have used it for my own purpose, but you can extend it.


Comments

Popular posts from this blog

Bypass SSL certificate check for Apache Axis2 client

How to Enable or Disable the "Show More Options" Menu on Windows 11?