Created
November 18, 2019 17:29
-
-
Save Auronmatrix/557639791255ebf762c71a7cf4878048 to your computer and use it in GitHub Desktop.
Bash Python Release Bump and Tag
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 | |
echo "---------Existing Tags---------" | |
git show-ref --tags -d | |
echo "-------------------------------" | |
function increase_version() { | |
python - "$1" "$2" <<EOF | |
import sys | |
kind = int(sys.argv[1]) | |
current_version = sys.argv[2] | |
parts = current_version.split('.') | |
parts[kind] = str(int(parts[kind]) + 1) | |
new_version = ".".join(parts) | |
print(new_version) | |
EOF | |
} | |
function update_setup_py() { | |
sed -i "" "s/version=\"$1\"/version=\"$2\"/g" setup.py | |
} | |
sem_ver_type=$1 | |
current_version=$(python setup.py --version) | |
new_version=$(python setup.py --version) | |
should_bump=false | |
if [ -n "$sem_ver_type" ]; then | |
case "$sem_ver_type" in | |
major) | |
echo "attempting major bump" | |
new_version=$(increase_version 0 $current_version) | |
should_bump=true | |
;; | |
minor) | |
echo "attempting minor bump" | |
new_version=$(increase_version 1 $current_version) | |
should_bump=true | |
;; | |
patch) | |
echo "attempting patch bump" | |
new_version=$(increase_version 2 $current_version) | |
should_bump=true | |
;; | |
*) | |
echo $"Error, invalid semver type specified. Usage: $0 {major|minor|patch}" | |
exit 1 | |
esac | |
fi | |
if [ "$should_bump" = true ] ; then | |
echo "Old version ($current_version) -> New version ($new_version)" | |
if [[ -n $(git status -s) ]]; then | |
echo "Working tree dirty. Please commit or stash all work before creating a new release" | |
exit 1 | |
else | |
echo "Updating version in setup.py" | |
update_setup_py "$current_version" "$new_version" | |
git add . | |
git commit -m "[$new_version] Bump version from $current_version to $new_version for $sem_ver_type release" | |
fi | |
fi | |
echo "Creating tag with version $new_version" | |
git tag $new_version | |
could_create_tag=$? | |
if [ $could_create_tag -eq 0 ]; then | |
echo "Tag created. Pushing..." | |
git push --tags | |
else | |
echo "Version already exists. Please update the version in setup.py or use this script to bump the version e.g. ./release.sh {major|minor|patch}" | |
exit 1 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment