Automating Git Tag Creation with Bash Script

Phatcharaphan Ananpreechakun
odds.team
Published in
2 min readApr 11, 2024

Git tags serve as markers for specific points in a project’s history, commonly used to denote release versions. Manually creating tags can be a waste of time and prone to errors.

Automating the creation of Git tags can streamline the release process and ensure consistency in versioning across your project.

Recently, I found myself frequently creating the git tags manually. Now, I’ve taken the time to refactor my code and automate the process using a Bash script. This automation will save me valuable time during development and streamline the deployment of versioned releases.

In my case, I’ve structured the Git tags based on the branch names, which are abundant in the project. Every branch name dictates the initial version number, comprising major, and minor numbers. The patch number increases automatically when running the Bash script.

For example, if the branch name is release-2.2 , the initial version number in the build.gradle file might appear as follows:

.....
defaultConfig {
....
versionCode 1
versionName "8.2.0"
....
}
......

Let’s create a Bash script to automate this scenario:

#!/bin/bash

# Function to extract version name from build.gradle
extract_version_name() {
FILE='build.gradle'
version_name=$(grep versionName "$FILE" | sed -E 's/.*versionName "([0-9]+\.[0-9]+).*/\1.0/')
echo "$version_name"
}

This function extracts the version name from the build.gradle file. It searches for the line containing versionName and extracts the major and minor version numbers.

# Function to get the latest tag based on major and minor version numbers
get_latest_tag() {
MAJOR_VERSION=$(echo "$1" | cut -d'.' -f1)
MINOR_VERSION=$(echo "$1" | cut -d'.' -f2)
git tag --list "$MAJOR_VERSION.$MINOR_VERSION.*" | tail -n 1
}

Here, define a function that retrieves the latest tag from the Git repository based on the major and minor version numbers extracted earlier.

# Function to increment the version number
increment_version() {
local current_version="$1"
local patch=$(cut -d'.' -f3 <<< "$current_version")
((patch++))
echo "${current_version%.*}.$patch"
}

This function increments the patch number of the given version.

# Main script
latest_tag=$(get_latest_tag "$(extract_version_name)")
echo "Latest tag: $latest_tag"

if [[ "$latest_tag" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# Increment the version
new_version=$(increment_version "$latest_tag")
git tag "$new_version"
git push --tags
else
# If no tags exist, start with version in build.gradle file
new_version=$(extract_version_name)
git tag "$new_version"
git push --tags
fi

echo "Git tag version: $new_version"

In the main script, retrieve the latest tag and check if it follows the semantic versioning pattern x.y.z If a tag exists, increment the version; otherwise, start with the version in build.gradle file.

Finally, we tag the new version and put the tags into the Git repository.

Feel free to customize the script according to your project’s specific requirements and integrate it into your development workflow for seamless version control.

Hopefully, this content will prove helpful and inspire every reader. Enjoy, and see you on the next topic. I apologize for any mistakes.

--

--