Skip to content

Instantly share code, notes, and snippets.

@JunaidQadirB
Created January 4, 2021 18:08
Show Gist options
  • Save JunaidQadirB/efbd40f9c746aa8a3db9a45b67710cc4 to your computer and use it in GitHub Desktop.
Save JunaidQadirB/efbd40f9c746aa8a3db9a45b67710cc4 to your computer and use it in GitHub Desktop.
Create and auto-increment Release versions on GitHub
#!/bin/bash
Green='\033[1;32m'
Red='\033[0;31m'
NC='\033[0m' # No Color
Yellow='\033[0;33m'
should_bump=true
bump_major()
{
ver=$version
major=`echo $ver | cut -d. -f1`
minor=`echo $ver | cut -d. -f2`
revision=`echo $ver | cut -d. -f3`
major=`expr $major + 1`
version="$major.$minor.$revision"
}
bump_minor()
{
ver=$version
major=`echo $ver | cut -d. -f1`
minor=`echo $ver | cut -d. -f2`
revision=`echo $ver | cut -d. -f3`
minor=`expr $minor + 1`
version="$major.$minor.$revision"
}
bump_revision()
{
ver=$version
echo $version
major=`echo $ver | cut -d. -f1`
minor=`echo $ver | cut -d. -f2`
revision=`echo $ver | cut -d. -f3`
revision=`expr $revision + 1`
version="$major.$minor.$revision"
}
update_tags_list()
{
echo "Updatng tags list..."
{
git tag -l | xargs git tag -d
git fetch --tags --all
} &> /dev/null
}
create_release()
{
branch=$(git rev-parse --abbrev-ref HEAD)
repo_full_name=$(git config --get remote.origin.url | sed 's/.*:\/\/github.com\///;s/.git$//')
token=$(git config --global github.token)
echo "Create release $version for repo: $repo_full_name branch: $branch"
response=$(curl --silent l --data "$(generate_post_data)" "https://api.github.com/repos/$repo_full_name/releases?access_token=$token")
if [[ $response =~ created_at ]];
then
echo -e "${Green}Release created successfully!${NC}"
else
echo -e "${Red}Error: Failed to create release. Try again.${NC}"
fi
update_tags_list
}
handle_initial_version_number()
{
case $version in
'fatal: No names found, cannot describe anything.' | '' )
version="v0.0.1"
echo "There are no releases, setting inital release version."
;;
esac
}
generate_post_data()
{
cat <<EOF
{
"tag_name": "$version",
"target_commitish": "$branch",
"name": "$version",
"body": "$message",
"draft": false,
"prerelease": false
}
EOF
}
auto_bump_version()
{
version="$(git tag --sort=v:refname | tail -1)"
}
show_usage()
{
echo -e "${Green}Usage: git release -v[1.0.1 | v1.0.1] -m\"Message\"${NC}"
echo "Examples:"
echo "git release -vv1.0.1 -m\"Fixed a typo\""
echo "git release -m\"Fixed a typo\""
}
if [[ $# -eq 0 ]] ; then
show_usage
exit 0
fi
current_release="$(git tag --sort=v:refname | tail -1)"
if [[ -z "${current_release// }" ]];
then
version="v0.0.1"
should_bump=false
else
version=$current_release
fi
while getopts ":v:m:" opt; do
case $opt in
v)
version="$OPTARG"
should_bump=false
;;
m) message="$OPTARG"
;;
\?) echo -e "${Green}Usage: git release -v1.0.1${NC}";exit >&2
;;
esac
done
if [[ $version != v* ]] ;
then
version="v$version"
fi
if [[ $version =~ ^v[0-9]+(\.[0-9]+){2,3}$ ]];
then
update_tags_list
if [[ "$should_bump" = true ]];
then
bump_revision
echo -e "${Yellow}Next relase version will be $version${NC}"
fi
create_release
else
echo -e "${Red}Error: Please make sure version follows semver${NC}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment