Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic tagging of releases

Tags:

git

bash

hudson

How do you tag your release versions in git?

Now I have each release identified by build number, but they increment even if there are no changes in the repo. My idea is to have it generated automatically on successful deployment on staging server. E.g.

  • run Hudson build
  • when successful, add new tag, i.e. 1.0-1
  • on next successful build add next tag, 1.0-2
  • version tag is displayed then in site footer

This would require:

  • Hudson to manage next version numbers
  • or script to store last tag in some file
  • or parse git tags to determine last

Any tips?

like image 897
takeshin Avatar asked Sep 21 '10 12:09

takeshin


People also ask

What is tagging in release?

Tags are a simple aspect of Git, they allow you to identify specific release versions of your code. You can think of a tag as a branch that doesn't change.

How do I automatically add tags?

In the left page menu, click Settings. Click Account settings. Click the Auto-tagging section. To enable auto-tagging, click to check the box next to “Tag the URL that people click through from my ad.”

How do I automatically tag in GitHub?

# The git tag is constructed by prepending 'v' to the string in RELEASE_ID. # underscores will automatically converted to dashes. This also affects tilde. # the most recent changes and automatically creates a new tag in the git repo.


1 Answers

I wrote this to help with updating tags incrementally e.g. 1.0.1 to 1.0.2 etc.

2020 update: I have posted an improved version beneath/here. (Also worth seeing @Geoffrey's answer below for some comments on branching).

#!/bin/bash  #get highest tag number VERSION=`git describe --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]} VNUM2=${VERSION_BITS[1]} VNUM3=${VERSION_BITS[2]} VNUM3=$((VNUM3+1))  #create new tag NEW_TAG="$VNUM1.$VNUM2.$VNUM3"  echo "Updating $VERSION to $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`  #only tag if no tag already if [ -z "$NEEDS_TAG" ]; then     git tag $NEW_TAG     echo "Tagged with $NEW_TAG"     git push --tags else     echo "Already a tag on this commit" fi 

I revisit this 6 years later needing to write a similar script for uploading to npm. Code below or here, comments and suggestions appreciated and welcome:

#!/bin/bash  # https://github.com/unegma/bash-functions/blob/main/update.sh  VERSION=""  #get parameters while getopts v: flag do   case "${flag}" in     v) VERSION=${OPTARG};;   esac done  #get highest tag number, and add 1.0.0 if doesn't exist CURRENT_VERSION=`git describe --abbrev=0 --tags 2>/dev/null`  if [[ $CURRENT_VERSION == '' ]] then   CURRENT_VERSION='1.0.0' fi echo "Current Version: $CURRENT_VERSION"   #replace . with space so can split into an array CURRENT_VERSION_PARTS=(${CURRENT_VERSION//./ })  #get number parts VNUM1=${CURRENT_VERSION_PARTS[0]} VNUM2=${CURRENT_VERSION_PARTS[1]} VNUM3=${CURRENT_VERSION_PARTS[2]}  if [[ $VERSION == 'major' ]] then   VNUM1=$((VNUM1+1)) elif [[ $VERSION == 'minor' ]] then   VNUM2=$((VNUM2+1)) elif [[ $VERSION == 'patch' ]] then   VNUM3=$((VNUM3+1)) else   echo "No version type (https://semver.org/) or incorrect type specified, try: -v [major, minor, patch]"   exit 1 fi   #create new tag NEW_TAG="$VNUM1.$VNUM2.$VNUM3" echo "($VERSION) updating $CURRENT_VERSION to $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`  #only tag if no tag already #to publish, need to be logged in to npm, and with clean working directory: `npm login; git stash` if [ -z "$NEEDS_TAG" ]; then   npm version $NEW_TAG   npm publish --access public   echo "Tagged with $NEW_TAG"   git push --tags   git push else   echo "Already a tag on this commit" fi  exit 0  

Subnote: this won't work with zsh as (for one), zsh doesn't use 0 indexed arrays.

like image 182
timhc22 Avatar answered Sep 18 '22 21:09

timhc22