Last active
November 4, 2020 01:25
-
-
Save NickLarsenNZ/ee907e05429a02fb6f2bcc73e7c34733 to your computer and use it in GitHub Desktop.
Automatically bump the semantic version tag if there are changes since the last (with discrimination between major, minor, patch bumps from comments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gist.github.com/NickLarsenNZ/ee907e05429a02fb6f2bcc73e7c34733 | |
set -euo pipefail | |
COMMIT_MESSAGES="" | |
LAST_TAG=$(git describe --abbrev=0 --tags 2>/dev/null) | |
if [ -z "$LAST_TAG" ]; then | |
>&2 echo "No tags previously defined" | |
LAST_TAG="0.0.1" | |
# Get commit logs for all time | |
COMMIT_MESSAGES=$(git log --pretty=format:'%s' --abbrev-commit) | |
else | |
# Ensure semver | |
echo -n "Validating last version: " | |
grep --color -E "[0-9]+\.[0-9]+\.[0-9]+[-+]?.*" <(echo $LAST_TAG) | |
# Get commit logs between now and the last semver | |
COMMIT_MESSAGES=$(git log --pretty=format:'%s' --abbrev-commit HEAD...${LAST_TAG}) | |
# If the commit log is empty, then we don't need to bump | |
if [ -z "$COMMIT_MESSAGES" ]; then | |
>&2 echo "No change since the last tag" | |
echo $LAST_TAG | |
exit 0 | |
fi | |
fi | |
# Check if the commit logs contain major/minor changes | |
echo "${COMMIT_MESSAGES}" | grep -i '\[major\]' &> /dev/null | |
IS_MAJOR=$? | |
echo "${COMMIT_MESSAGES}" | grep -i '\[minor\]' &> /dev/null | |
IS_MINOR=$? | |
MAJOR=$(echo $LAST_TAG | cut -d. -f1 | grep -oE "^[0-9]+\$") | |
MINOR=$(echo $LAST_TAG | cut -d. -f2 | grep -oE "^[0-9]+\$") | |
PATCH=$(echo $LAST_TAG | cut -d. -f3 | grep -oE "^[0-9]+") | |
if [ $IS_MAJOR == 0 ]; then | |
NEW_TAG=$(($MAJOR+1)).0.0 | |
elif [ $IS_MINOR == 0 ]; then | |
NEW_TAG=$MAJOR.$(($MINOR+1)).0 | |
else | |
NEW_TAG=$MAJOR.$MINOR.$(($PATCH+1)) | |
fi | |
git tag $NEW_TAG | |
git push --tags | |
echo "##vso[task.setvariable variable=NEW_TAG;isOutput=true]$NEW_TAG" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
COMMIT_MESSAGES="" | |
#PREVIOUS_SEMVER | |
#SEMVER | |
BUMP="patch" | |
PREVIOUS_SEMVER=$(git describe --abbrev=0 --tags 2>/dev/null) | |
if [ -z "$PREVIOUS_SEMVER" ]; then | |
>&2 echo "No tags previously defined" | |
PREVIOUS_SEMVER="0.0.1" | |
# Get commit logs for all time | |
COMMIT_MESSAGES=$(git log --pretty=format:'%s' --abbrev-commit) | |
else | |
# Get commit logs between now and the last semver | |
COMMIT_MESSAGES=$(git log --pretty=format:'%s' --abbrev-commit HEAD...${PREVIOUS_SEMVER}) | |
# If the commit log is empty, then we don't need to bump | |
if [ -z "$COMMIT_MESSAGES" ]; then | |
>&2 echo "No change since the last tag" | |
echo $PREVIOUS_SEMVER | |
exit 0 | |
fi | |
fi | |
# Check if the commit logs contain major/minor changes | |
echo "${COMMIT_MESSAGE}" | grep -i '\[major\]' &> /dev/null | |
IS_MAJOR=$? | |
echo "${COMMIT_MESSAGE}" | grep -i '\[minor\]' &> /dev/null | |
IS_MINOR=$? | |
if [ $IS_MAJOR == 0 ]; then | |
BUMP = 'major' | |
elif [ $IS_MINOR == 0 ]; then | |
BUMP = 'minor' | |
fi | |
# Do the needful | |
SEMVER=$(>&2 git semver ${BUMP}) || exit 1 | |
# Push the tags upstream | |
>&2 git push --tags || exit 2 | |
# Print the new semver to stdout | |
echo -n $SEMVER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: