Automatic Tagging of Releases

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

In the previous post, I pushed the docker image with a hardcoded "latest" tag. I will follow the standard semantic versioning flow to release new changes:

Given a version number Major.Minor.Patch, increment the:

  1. Major version when you make incompatible API changes,
  2. Minor version when you add functionality in a backwards compatible manner, and
  3. Patch version when you make backwards compatible bug fixes.

Define helper script for tagging

  1. Create a build directory in the root application folder
  2. Create a file in the build folder, below are the contents of the shell script:


# get parameters
while getopts v: flag
  case "${flag}" in
    v) VERSION=${OPTARG};;
Read the input parameter
# get highest tag number, and add v0.1.0 if doesn't exist
git fetch --prune --unshallow 2>/dev/null
CURRENT_VERSION=`git describe --abbrev=0 --tags 2>/dev/null`
Fetch latest git tag and start at v0.1.0
# get number parts

if [[ $VERSION == 'major' ]]
elif [[ $VERSION == 'minor' ]]
elif [[ $VERSION == 'patch' ]]
  echo "No version type ( or incorrect type specified, try: -v [major, minor, patch]"
  exit 1
Based on input parameter, increment the version (major, minor, or patch)
# create new tag
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
if [ -z "$NEEDS_TAG" ]; then
  echo "Tagged with $NEW_TAG"
  git tag $NEW_TAG
  git push --tags
  git push
  echo "Already a tag on this commit"
Create and push tag to GitHub
echo ::set-output name=git-tag::$NEW_TAG

exit 0
Define git-tag output variable

Add new step for tagging

  1. In the main.yml file, I will add another GitHub action. Every time there's a new commit to the main branch we will increment a specific version (major, minor, or patch):
- name: Automatic Tagging of Releases
        id: increment-git-tag
        run: |
          bash ./build/ -v patch
Increment patch version

2. I will then reference the output variable tag for our docker image, we get this from the previous step with the help of the shell script we defined.

IMAGE_TAG: ${{ steps.increment-git-tag.outputs.git-tag }}

See it in action

Commit latest changes to the main branch to see the workflow run the configuration.

Incremented patch version
First tag of our code
Image tagged accordingly


In general you can increment your version (major, minor, or patch), just replace the specific version on the step that you pass the input:

- name: Automatic Tagging of Releases
        id: increment-git-tag
        run: |
          bash ./build/ -v patch

Each time you commit your code will be versioned accordingly, thanks for reading along. In the meantime checkout the repo create-a-ci-pipeline.