Created
June 18, 2025 11:42
-
-
Save slhck/64f78832f1b115396b201b8675d73ec7 to your computer and use it in GitHub Desktop.
Simple release script
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
#!/usr/bin/env bash | |
# | |
# Release the project and bump version number in the process. | |
# | |
# To set up the project, make sure you have a VERSION file: | |
# | |
# echo "1.0.0" > VERSION | |
set -e | |
cd "$(dirname "$0")" | |
FORCE=false | |
DRY_RUN=false | |
usage() { | |
echo "Usage: $0 [options] VERSION" | |
echo | |
echo "VERSION:" | |
echo " major: bump major version number" | |
echo " minor: bump minor version number" | |
echo " patch: bump patch version number" | |
echo | |
echo "Options:" | |
echo " -f, --force: force release" | |
echo " -n, --dry-run: dry run" | |
echo " -h, --help: show this help message" | |
exit 1 | |
} | |
# parse args | |
while [ "$#" -gt 0 ]; do | |
case "$1" in | |
-f | --force) | |
FORCE=true | |
shift | |
;; | |
-h | --help) | |
usage | |
;; | |
-n | --dry-run) | |
DRY_RUN=true | |
shift | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
# check if version is specified | |
if [ "$#" -ne 1 ]; then | |
usage | |
fi | |
if [ "$1" != "major" ] && [ "$1" != "minor" ] && [ "$1" != "patch" ]; then | |
usage | |
fi | |
# check if git is clean and force is not enabled | |
if ! git diff-index --quiet HEAD -- && [ "$FORCE" = false ]; then | |
echo "Error: git is not clean. Please commit all changes first." | |
exit 1 | |
fi | |
if ! command -v uv &> /dev/null; then | |
echo "Error: uv is not installed. Please install uv from https://docs.astral.sh/uv/" | |
exit 1 | |
fi | |
VERSION_FILE="VERSION" | |
if [ ! -f "$VERSION_FILE" ]; then | |
echo "Error: VERSION file not found. Please create a VERSION file." | |
exit 1 | |
fi | |
VERSION=$(cat "$VERSION_FILE") | |
new_version=$(uvx --from semver pysemver bump "$1" "$VERSION") | |
echo "Would bump version:" | |
echo "$VERSION -> $new_version" | |
if [ "$DRY_RUN" = true ]; then | |
exit 0 | |
fi | |
# prompt for confirmation | |
if [ "$FORCE" = false ]; then | |
read -p "Do you want to release? [yY] " -n 1 -r | |
echo | |
else | |
REPLY="y" | |
fi | |
echo | |
# if .gitchangelog.rc does not exist, create it! | |
if [ ! -f ".gitchangelog.rc" ]; then | |
tee <<EOF > .gitchangelog.rc | |
output_engine = mustache("markdown") | |
tag_filter_regexp = r'^v[0-9]+\.[0-9]+(\.[0-9]+)?$' | |
EOF | |
git add .gitchangelog.rc | |
fi | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
# replace version number | |
echo "$new_version" > "$VERSION_FILE" | |
# commit changes | |
git add "$VERSION_FILE" | |
git commit -m "bump version to $new_version" | |
git tag -a "v$new_version" -m "v$new_version" | |
# generate changelog | |
uvx --with pystache gitchangelog > CHANGELOG.md | |
git add CHANGELOG.md | |
git commit --no-verify --amend --no-edit | |
git tag -a -f -m "v$new_version" "v$new_version" | |
# push changes | |
git push origin master | |
git push origin "v$new_version" | |
else | |
echo "Aborted." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment