Skip to content

Instantly share code, notes, and snippets.

@AvnerCohen
Created May 20, 2021 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AvnerCohen/68ced8fb74125ba0243f61a068aad767 to your computer and use it in GitHub Desktop.
Save AvnerCohen/68ced8fb74125ba0243f61a068aad767 to your computer and use it in GitHub Desktop.
Automatic Git Tagging for Shared Libraries
#!/bin/bash
# When executing in circleci, make sure a user is defined in git config
CURRENT_NAME=$(git config user.name)
if [[ -z $CUR_NAME ]]; then
git config user.name "CI USER"
git config user.email ci-user@companyname.biz
echo "Git names init done."
fi
# Extract status - latest tag and prefix template
LATEST_TAG=$(git describe --tags --abbrev=0)
echo "Identified latest tag as - $LATEST_TAG"
TAG_MAJOR_PART=$(echo "${LATEST_TAG}" | cut -d"." -f1)
TAG_MINOR_PART=$(echo "${LATEST_TAG}" | cut -d"." -f2)
TAG_PATCH_PART=$(echo "${LATEST_TAG}" | cut -d"." -f3)
TMPLT_MAJOR_PART=$(cat version.prefix | cut -d"." -f1)
TMPLT_MAJOR_PART=$(cat version.prefix | cut -d"." -f2)
# If a major or minor version changed, reset patch counter
if [[ $TAG_MAJOR_PART -ne $TMPLT_MAJOR_PART || $TAG_MINOR_PART -ne $TMPLT_MAJOR_PART ]]; then
NEW_PATCH="0"
else
NEW_PATCH=$(expr $TAG_PATCH_PART + 1)
fi
# Tag and push
NEW_VER=$(cat version.prefix | sed "s/\#/${NEW_PATCH}/")
echo "Pushing new tag - $NEW_VER"
git tag -a $NEW_VER -m "Auto-tag created: $(date +'%Y-%m-%d_%H-%M-%S') - Prev tag ${LATEST_TAG}"
git push origin $NEW_VER
## Notify user about the newly created tag - via slack api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment