Last active
October 22, 2023 03:14
-
-
Save speaud/128949035e397828557cf8a71a1dfdcd to your computer and use it in GitHub Desktop.
Simple bump version script for python projects which use setup.py and git
This file contains hidden or 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
#!/bin/bash | |
: ' | |
This script should be configured to run during the pre-commit process of release branches however it could be manually executed before a release | |
This script will | |
1) Validate the version argument passed | |
2) Update the version value in setup.py | |
3) Commit and push the file change | |
4) Update and push the new git tag | |
Version format <major>.<minor>.<patch>-<interation>-g<short commit hash> | |
Usage example `bin/set_version.bash <major>.<minor>.<patch>` | |
' | |
echo " _ _ " | |
echo " | | (_) " | |
echo " ___ ___| |_ __ _____ _ __ ___ _ ___ _ __ " | |
echo " / __|/ _ \ __| \ \ / / _ \ '__/ __| |/ _ \| '_ \ " | |
echo " \__ \ __/ |_ \ V / __/ | \__ \ | (_) | | | | " | |
echo " |___/\___|\__| \_/ \___|_| |___/_|\___/|_| |_| " | |
echo " Version format <major>.<minor>.<patch>" | |
echo "" | |
# Prevent this script from running on non feature branches | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
[[ $current_branch =~ "develop" ]] || [[ $current_branch =~ "main" ]] && echo "FATAL: You can only run this script on a feature branch" | |
throw_arg_error() { | |
echo "FATAL: You did not provide a valid next version argument ($1)" | |
echo "REASON: $2" | |
echo "Example: bin/${0##*/} <major>.<minor>.<patch>" | |
exit 1 | |
} | |
# Check if the version argument was passed | |
[[ $# -ne 1 ]] && throw_arg_error "empty" "No argument found" | |
# Check if the version argument pattern is valid | |
[[ ! $1 =~ ^[0-9]*\.[0-9]*\.[0-9]*$ ]] && throw_arg_error $1 "Invalid pattern" | |
# Check if the version argument has already been used | |
[[ $(git tag -l | grep $1 | wc -l) -ne 0 ]] && throw_arg_error $1 "Value already used. See git tags: $(git tag -l)" | |
# Get the current version value from the setup method in setup.py | |
current_setup_py_verison=$(echo $(cat setup.py | grep -e "version=") | sed -E 's/version=|\"|\,//g') | |
# @returns <major>.<minor>.<patch> | |
echo "Current version from setup.py: $current_setup_py_verison" | |
# Get the current git tag | |
current_git_tag=$(git describe --tags) | |
# @returns <major>.<minor>.<patch> or <major>.<minor>.<patch>-<interation>-g<short commit hash> | |
echo "Current git tag: $current_git_tag" | |
# Parse the current git tag to get the current version value | |
current_git_tag_version=$(sed -E -e 's/\-([[:alnum:]]|\-)*//' <<< $current_git_tag) | |
# @returns <major>.<minor>.<patch> | |
echo "Parsed version from git tag: $current_git_tag_version" | |
# Check if the current version value from the setup method in setup.py matches the | |
# current version parsed from the current git tag | |
if [[ $current_setup_py_verison != $current_git_tag_version ]]; then | |
echo "FATAL: The version value from the setup method in setup.py ($current_setup_py_verison) does not match the parsed version from the current git tag ($current_git_tag_version)" | |
echo "You need to fix the mismatch before running this script" | |
git describe --tags | |
exit 1 | |
fi | |
# Make the user confirm the next version they entered | |
echo "Confirm the next version value you entered (y/n): $1" | |
read agreed | |
if [ $agreed != "y" ]; then | |
echo "FATAL: You did not confirm the next version. Exiting. Re-run and confirm to complete the process." | |
exit 1 | |
fi | |
# Update the version value in setup.py | |
sed -i "" -e "s/version\=\"[0-9]*.[0-9]*.[0-9]*\"\,/version\=\"$1\"\,/g" setup.py | |
git add setup.py | |
git commit -m "Auto-commit: version updated" | |
git push | |
# Check if the version value in setup.py was updated | |
if [[ $(cat setup.py | grep $1 | wc -l) -ne 1 ]]; then | |
echo "FATAL: Something went wrong while updating the version" | |
exit 1 | |
else | |
echo "Version successfully updated in setup.py" | |
fi | |
# Get this last commit hash from when this script updated setup.py | |
previous_commit_hash=$(git log -n 1 --pretty=format:"%h") | |
# Update the git tag with the new version value | |
git tag $1 $previous_commit_hash | |
git push origin $1 | |
# Confirm git tag was updated | |
if [[ $(git describe --tags) == $1 ]]; then | |
echo "Tag updated successfully. You can now push any remaining code changes, if any, then open a PR to merge into the develop branch." | |
else | |
echo "FATAL: Failed to update tag successfully" | |
exit 1 | |
fi | |
echo "Version set successfully" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sloppy shorthand command