Last active
April 11, 2022 09:51
-
-
Save nir0s/ed262afd6a20d20af7be095e5004af9c to your computer and use it in GitHub Desktop.
Use semver to release to GitHub
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 | |
function log() { | |
local -r level="$1" | |
local -r message="$2" | |
echo -e "[$level] $message" | |
} | |
function _get_bump_part() { | |
local -r commit_subject="$1" | |
local bump_part=$(echo $commit_subject | sed -En 's/.*\[semver:(major|minor|patch)\].*/\1/p') | |
echo $bump_part | |
} | |
function _install_ghr() { | |
if ! command -v go &> /dev/null; then | |
log info "Installing ghr" | |
echo "Go must be available in the path." | |
exit 1 | |
fi | |
go install github.com/tcnksm/ghr@latest | |
} | |
function _install_semver_tool() { | |
if ! command -v semver &> /dev/null; then | |
log info "Installing semver tool" | |
sudo curl -sSL https://raw.githubusercontent.com/fsaintjacques/semver-tool/master/src/semver -o /usr/local/bin/semver && \ | |
sudo chmod +x /usr/local/bin/semver | |
fi | |
} | |
function set_new_version() { | |
_install_semver_tool &> /dev/null | |
# Get the current commit message's subject line. | |
local commit_subject=$(git log -1 --pretty=%s.) | |
log info "Checking if semver prefix exists" | |
if ! [[ "$commit_subject" == *"[semver"* ]]; then | |
return | |
fi | |
log info "Getting semver bump part" | |
local bump_part=$(_get_bump_part "$commit_subject") | |
if [ -z "$bump_part" ]; then | |
log error "Could not find find valid bump part. Should be one of [semver:(major/minor/patch)]" | |
exit 1 | |
fi | |
# We don't want to get the last tag, but rather the latest semver'd one. | |
log info "Bumping ${bump_part} version" | |
local latest_tag=$(git describe --abbrev=0 --tags --match "v[0-9]*") | |
log info "Previous tag is: $latest_tag" | |
local new_version=$(semver bump "$bump_part" "$latest_tag") | |
log info "Setting new version in semver.env in NEW_VERSION variable to $new_version" | |
echo NEW_VERSION=$new_version > semver.env | |
} | |
function _main() { | |
# Get the current commit message's subject line. | |
local commit_subject=$(git log -1 --pretty=%s.) | |
if ! [[ "$commit_subject" == *"[semver"* ]]; then | |
log info "Semver prefix not provided in commit subject." | |
exit 0 | |
fi | |
if [ -z "$GITHUB_TOKEN" ]; then | |
echo "You must set a GITHUB_TOKEN environment variable." | |
exit 1 | |
fi | |
_install_semver_tool | |
set_new_version | |
source semver.env | |
_install_ghr | |
local release_files_path=${BUILD_ARTIFACTS_PATH:-~/workspace/} | |
local latest_tag=$(git describe --abbrev=0 --tags --match "v[0-9]*") | |
local changelog=$(git log --pretty=format:'* %s (%h)' "$latest_tag"..HEAD) | |
local new_tag="v$NEW_VERSION" | |
log info "Release info: latest_tag=$latest_tag new_tag=$new_tag release_files_path=$release_files_path" | |
log info "Changelog:\n $changelog" | |
ghr -token $GITHUB_TOKEN -name latest -body "${changelog}" -owner strigo -delete latest $release_files_path | |
ghr -token $GITHUB_TOKEN -name $new_tag -body "${changelog}" -owner strigo -delete $new_tag $release_files_path | |
} | |
# Only run main if not sourced. | |
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
_main "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment