|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +set -eu |
| 4 | + |
| 5 | +print_usage() { |
| 6 | + cat <<EOF |
| 7 | +Usage: $0 |
| 8 | +
|
| 9 | +Creates and pushes a k0s release tag by performing the following steps: |
| 10 | +
|
| 11 | + - Determine the Kubernetes version |
| 12 | + - Ask about RC and hotfix numbers |
| 13 | + - Construct the tag name from the above information |
| 14 | + - Create the tag and push it to the remote repository |
| 15 | +
|
| 16 | +OPTIONS: |
| 17 | + -h Show this message |
| 18 | +
|
| 19 | +ENVIRONMENT: |
| 20 | + K0S_SIGNED_TAG Used to optionally disable tag signing if set to |
| 21 | + 'no'. |
| 22 | +EOF |
| 23 | +} |
| 24 | + |
| 25 | +confirm_action() { |
| 26 | + printf '%s (Y/n) ' "$1" |
| 27 | + |
| 28 | + while :; do |
| 29 | + read -r yn |
| 30 | + if [ -n "$yn" ]; then |
| 31 | + case "$yn" in |
| 32 | + y | Y) return 0 ;; |
| 33 | + n | N) return 1 ;; |
| 34 | + *) printf "To confirm, enter 'y', 'Y' or nothing at all, to decline, enter 'n' or 'N': " ;; |
| 35 | + esac |
| 36 | + else |
| 37 | + return 0 |
| 38 | + fi |
| 39 | + done |
| 40 | +} |
| 41 | + |
| 42 | +git_get_upstream_remote_for_local_branch() { |
| 43 | + baseBranchFullRef=$(git rev-parse --symbolic-full-name "$1") |
| 44 | + upstreamRemoteRef=$(git for-each-ref --format='%(upstream)' "$baseBranchFullRef") |
| 45 | + |
| 46 | + case "$upstreamRemoteRef" in |
| 47 | + refs/remotes/*) |
| 48 | + upstreamRemoteBranch=${upstreamRemoteRef##"refs/remotes/"} |
| 49 | + echo "${upstreamRemoteBranch%/*}" |
| 50 | + ;; |
| 51 | + esac |
| 52 | +} |
| 53 | + |
| 54 | +git_get_current_branch_name() { |
| 55 | + git rev-parse --symbolic-full-name --abbrev-ref HEAD |
| 56 | +} |
| 57 | + |
| 58 | +determine_k8s_version() { |
| 59 | + printf %s 'Kubernetes version: ' |
| 60 | + set -- ./vars.sh kubernetes_version |
| 61 | + k8sVersion=$("$@" 2>/dev/null) || { |
| 62 | + retVal=$? |
| 63 | + echo Failed to determine Kuberntes version! 1>&2 |
| 64 | + "$@" |
| 65 | + return $retVal |
| 66 | + } |
| 67 | + |
| 68 | + echo "$k8sVersion" |
| 69 | +} |
| 70 | + |
| 71 | +determine_git_base_branch() { |
| 72 | + printf %s 'Base git branch: ' |
| 73 | + baseBranchName=$(git_get_current_branch_name) |
| 74 | + echo "$baseBranchName" |
| 75 | +} |
| 76 | + |
| 77 | +determine_upstream_git_remote() { |
| 78 | + printf %s 'Upstream git remote: ' |
| 79 | + upstreamRemote=$(git_get_upstream_remote_for_local_branch "$baseBranchName") |
| 80 | + if [ -n "$upstreamRemote" ]; then |
| 81 | + echo "$upstreamRemote" |
| 82 | + else |
| 83 | + echo N/A |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | +read_k0s_rc_version() { |
| 88 | + if confirm_action 'Is the next version a release candidate?'; then |
| 89 | + while :; do |
| 90 | + printf %s 'Please enter the release candidate number (e.g. 0, 1, 2, ...): ' |
| 91 | + read -r k0sRc |
| 92 | + ! [ "$k0sRc" -ge 0 ] 2>/dev/null || return 0 |
| 93 | + done |
| 94 | + else |
| 95 | + k0sRc='' |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +read_k0s_hotfix_version() { |
| 100 | + while :; do |
| 101 | + printf %s 'Please enter the k0s hotfix number (e.g. 0, 1, 2, ...): ' |
| 102 | + read -r k0sHotfix |
| 103 | + ! [ "$k0sHotfix" -ge 0 ] 2>/dev/null || return 0 |
| 104 | + done |
| 105 | +} |
| 106 | + |
| 107 | +construct_tag_name() { |
| 108 | + k0sTag="v$k8sVersion" |
| 109 | + [ -z "${k0sRc-}" ] || k0sTag="$k0sTag-rc.$k0sRc" |
| 110 | + k0sTag="$k0sTag+k0s.$k0sHotfix" |
| 111 | +} |
| 112 | + |
| 113 | +create_tag() { |
| 114 | + set -- git tag -a -m "release $k0sTag" |
| 115 | + if [ "${K0S_SIGNED_TAG-}" != no ]; then |
| 116 | + set -- "$@" --sign |
| 117 | + confirm_action "About to create signed tag '$k0sTag' (disable with K0S_SIGNED_TAG=no). Okay?" |
| 118 | + else |
| 119 | + set -- "$@" --no-sign |
| 120 | + confirm_action "About to create unsigned tag '$k0sTag'. Okay?" |
| 121 | + fi |
| 122 | + |
| 123 | + "$@" -- "$k0sTag" |
| 124 | +} |
| 125 | + |
| 126 | +push_tag_to_upstream_remote() { |
| 127 | + git show "$k0sTag" |
| 128 | + if [ -n "$upstreamRemote" ]; then |
| 129 | + if confirm_action "Push the above tag '$k0sTag' to remote '$upstreamRemote'?"; then |
| 130 | + git push "$upstreamRemote" "$k0sTag" |
| 131 | + fi |
| 132 | + fi |
| 133 | +} |
| 134 | + |
| 135 | +do_release() { |
| 136 | + determine_k8s_version |
| 137 | + determine_git_base_branch |
| 138 | + determine_upstream_git_remote |
| 139 | + read_k0s_rc_version |
| 140 | + read_k0s_hotfix_version |
| 141 | + construct_tag_name |
| 142 | + create_tag |
| 143 | + push_tag_to_upstream_remote |
| 144 | +} |
| 145 | + |
| 146 | +if [ $# = 1 ] && [ "$1" = h ]; then |
| 147 | + print_usage |
| 148 | + exit 1 |
| 149 | +fi |
| 150 | + |
| 151 | +if [ $# != 0 ]; then |
| 152 | + print_usage >&2 |
| 153 | + exit 1 |
| 154 | +fi |
| 155 | + |
| 156 | +do_release |
0 commit comments