Skip to content

Commit

Permalink
Add a script to tag and push k0s releases
Browse files Browse the repository at this point in the history
The script encapsulates the instructions in RELEASE.md and helps to
create and push release tags in the way described.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Jun 27, 2023
1 parent c777ddc commit 9090c17
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Once the release notes are done we can publish the release.

If for some reason there is an error triggering the action, it is safe to delete the tag remotely with `git push --delete origin <tag>` and push it again.

The above steps are encapsulated in the [`hack/release.sh`](hack/release.sh)
shell script for convenience.

## Semver

We're following [semantic versioning](https://semver.org/) for version numbering. Currently we're working on 0.y.z series so the rules are interpreted in bit more relaxed way.
Expand Down
156 changes: 156 additions & 0 deletions hack/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#!/usr/bin/env sh

set -eu

print_usage() {
cat <<EOF
Usage: $0
Creates and pushes a k0s release tag by performing the following steps:
- Determine the Kubernetes version
- Ask about RC and hotfix numbers
- Construct the tag name from the above information
- Create the tag and push it to the remote repository
OPTIONS:
-h Show this message
ENVIRONMENT:
K0S_SIGNED_TAG Used to optionally disable tag signing if set to
'no'.
EOF
}

confirm_action() {
printf '%s (Y/n) ' "$1"

while :; do
read -r yn
if [ -n "$yn" ]; then
case "$yn" in
y | Y) return 0 ;;
n | N) return 1 ;;
*) printf "To confirm, enter 'y', 'Y' or nothing at all, to decline, enter 'n' or 'N': " ;;
esac
else
return 0
fi
done
}

git_get_upstream_remote_for_local_branch() {
baseBranchFullRef=$(git rev-parse --symbolic-full-name "$1")
upstreamRemoteRef=$(git for-each-ref --format='%(upstream)' "$baseBranchFullRef")

case "$upstreamRemoteRef" in
refs/remotes/*)
upstreamRemoteBranch=${upstreamRemoteRef##"refs/remotes/"}
echo "${upstreamRemoteBranch%/*}"
;;
esac
}

git_get_current_branch_name() {
git rev-parse --symbolic-full-name --abbrev-ref HEAD
}

determine_k8s_version() {
printf %s 'Kubernetes version: '
set -- ./vars.sh kubernetes_version
k8sVersion=$("$@" 2>/dev/null) || {
retVal=$?
echo Failed to determine Kuberntes version! 1>&2
"$@"
return $retVal
}

echo "$k8sVersion"
}

determine_git_base_branch() {
printf %s 'Base git branch: '
baseBranchName=$(git_get_current_branch_name)
echo "$baseBranchName"
}

determine_upstream_git_remote() {
printf %s 'Upstream git remote: '
upstreamRemote=$(git_get_upstream_remote_for_local_branch "$baseBranchName")
if [ -n "$upstreamRemote" ]; then
echo "$upstreamRemote"
else
echo N/A
fi
}

read_k0s_rc_version() {
if confirm_action 'Is the next version a release candidate?'; then
while :; do
printf %s 'Please enter the release candidate number (e.g. 0, 1, 2, ...): '
read -r k0sRc
! [ "$k0sRc" -ge 0 ] 2>/dev/null || return 0
done
else
k0sRc=''
fi
}

read_k0s_hotfix_version() {
while :; do
printf %s 'Please enter the k0s hotfix number (e.g. 0, 1, 2, ...): '
read -r k0sHotfix
! [ "$k0sHotfix" -ge 0 ] 2>/dev/null || return 0
done
}

construct_tag_name() {
k0sTag="v$k8sVersion"
[ -z "${k0sRc-}" ] || k0sTag="$k0sTag-rc.$k0sRc"
k0sTag="$k0sTag+k0s.$k0sHotfix"
}

create_tag() {
set -- git tag -a -m "release $k0sTag"
if [ "${K0S_SIGNED_TAG-}" != no ]; then
set -- "$@" --sign
confirm_action "About to create signed tag '$k0sTag' (disable with K0S_SIGNED_TAG=no). Okay?"
else
set -- "$@" --no-sign
confirm_action "About to create unsigned tag '$k0sTag'. Okay?"
fi

"$@" -- "$k0sTag"
}

push_tag_to_upstream_remote() {
git show "$k0sTag"
if [ -n "$upstreamRemote" ]; then
if confirm_action "Push the above tag '$k0sTag' to remote '$upstreamRemote'?"; then
git push "$upstreamRemote" "$k0sTag"
fi
fi
}

do_release() {
determine_k8s_version
determine_git_base_branch
determine_upstream_git_remote
read_k0s_rc_version
read_k0s_hotfix_version
construct_tag_name
create_tag
push_tag_to_upstream_remote
}

if [ $# = 1 ] && [ "$1" = h ]; then
print_usage
exit 1
fi

if [ $# != 0 ]; then
print_usage >&2
exit 1
fi

do_release

0 comments on commit 9090c17

Please sign in to comment.