|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +DEFAULT_REPO=netbox-community/netbox-docker |
| 4 | +REPO="${REPO-${DEFAULT_REPO}}" |
| 5 | + |
| 6 | +echomoji() { |
| 7 | + EMOJI=${1} |
| 8 | + TEXT=${2} |
| 9 | + shift 2 |
| 10 | + if [ -z "$DISABLE_EMOJI" ]; then |
| 11 | + echo "${EMOJI}" "${@}" |
| 12 | + else |
| 13 | + echo "${TEXT}" "${@}" |
| 14 | + fi |
| 15 | +} |
| 16 | + |
| 17 | +echo_nok() { |
| 18 | + echomoji "❌" "!" "${@}" |
| 19 | +} |
| 20 | +echo_ok() { |
| 21 | + echomoji "✅" "-" "${@}" |
| 22 | +} |
| 23 | +echo_hint() { |
| 24 | + echomoji "👉" ">" "${@}" |
| 25 | +} |
| 26 | + |
| 27 | +# check errors shall exit with code 1 |
| 28 | + |
| 29 | +check_clean_repo() { |
| 30 | + changes=$(git status --porcelain 2>/dev/null) |
| 31 | + if [ ${?} ] && [ -n "$changes" ]; then |
| 32 | + echo_nok "There are git changes pending:" |
| 33 | + echo "$changes" |
| 34 | + echo_hint "Please clean the repository before continueing: git stash --include-untracked" |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | + echo_ok "Repository has no pending changes." |
| 38 | +} |
| 39 | + |
| 40 | +check_branch() { |
| 41 | + expected_branch="${1}" |
| 42 | + actual_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) |
| 43 | + if [ ${?} ] && [ "${actual_branch}" != "${expected_branch}" ]; then |
| 44 | + echo_nok "Current branch should be '${expected_branch}', but is '${actual_branch}'." |
| 45 | + echo_hint "Please change to the '${expected_branch}' branch: git checkout ${expected_branch}" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + echo_ok "The current branch is '${actual_branch}'." |
| 49 | +} |
| 50 | + |
| 51 | +check_upstream() { |
| 52 | + expected_upstream_branch="origin/${1}" |
| 53 | + actual_upstream_branch=$(git rev-parse --abbrev-ref '@{upstream}' 2>/dev/null) |
| 54 | + if [ ${?} ] && [ "${actual_upstream_branch}" != "${expected_upstream_branch}" ]; then |
| 55 | + echo_nok "Current upstream branch should be '${expected_upstream_branch}', but is '${actual_upstream_branch}'." |
| 56 | + echo_hint "Please set '${expected_upstream_branch}' as the upstream branch: git branch --set-upstream-to=${expected_upstream_branch}" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + echo_ok "The current upstream branch is '${actual_upstream_branch}'." |
| 60 | +} |
| 61 | + |
| 62 | +check_origin() { |
| 63 | + expected_origin="git@github.com:${REPO}.git" |
| 64 | + actual_origin=$(git remote get-url origin 2>/dev/null) |
| 65 | + if [ ${?} ] && [ "${actual_origin}" != "${expected_origin}" ]; then |
| 66 | + echo_nok "The url of origin is '${actual_origin}', but '${expected_origin}' is expected." |
| 67 | + echo_hint "Please set '${expected_origin}' as the url for origin: git origin set-url '${expected_origin}'" |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + echo_ok "The current origin url is '${actual_origin}'." |
| 71 | +} |
| 72 | + |
| 73 | +check_latest() { |
| 74 | + git fetch --tags origin |
| 75 | + |
| 76 | + local_head_commit=$(git rev-parse HEAD 2>/dev/null) |
| 77 | + remote_head_commit=$(git rev-parse FETCH_HEAD 2>/dev/null) |
| 78 | + if [ "${local_head_commit}" != "${remote_head_commit}" ]; then |
| 79 | + echo_nok "HEAD is at '${local_head_commit}', but FETCH_HEAD is at '${remote_head_commit}'." |
| 80 | + echo_hint "Please ensure that you have pushed and pulled all the latest chanegs: git pull --prune --rebase origin; git push origin" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + echo_ok "HEAD and FETCH_HEAD both point to '${local_head_commit}'." |
| 84 | +} |
| 85 | + |
| 86 | +check_tag() { |
| 87 | + local tag |
| 88 | + |
| 89 | + tag=$(<VERSION) |
| 90 | + if git rev-parse "${tag}" 2>/dev/null >/dev/null; then |
| 91 | + echo_nok "The tag '${tag}' already points to '$(git rev-parse "${tag}" 2>/dev/null)'." |
| 92 | + echo_hint "Please ensure that the 'VERSION' file has been updated before trying to release: echo X.Y.Z > VERSION" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + echo_ok "The tag '${tag}' does not exist yet." |
| 96 | +} |
| 97 | + |
| 98 | +check_develop() { |
| 99 | + echomoji 📋 "?" "Checking 'develop' branch" |
| 100 | + |
| 101 | + check_branch develop |
| 102 | + check_upstream develop |
| 103 | + check_clean_repo |
| 104 | + check_latest |
| 105 | +} |
| 106 | + |
| 107 | +check_release() { |
| 108 | + echomoji 📋 "?" "Checking 'release' branch" |
| 109 | + |
| 110 | + check_upstream release |
| 111 | + check_clean_repo |
| 112 | + check_latest |
| 113 | +} |
| 114 | + |
| 115 | +# git errors shall exit with code 2 |
| 116 | + |
| 117 | +git_switch() { |
| 118 | + echomoji 🔀 "≈" "Switching to '${1}' branch…" |
| 119 | + if ! git checkout "${1}" >/dev/null; then |
| 120 | + echo_nok "It was not possible to switch to the branch '${1}'." |
| 121 | + exit 2 |
| 122 | + fi |
| 123 | + echo_ok "The branch is now '${1}'." |
| 124 | +} |
| 125 | + |
| 126 | +git_tag() { |
| 127 | + echomoji 🏷 "X" "Tagging version '${1}'…" |
| 128 | + if ! git tag "${1}"; then |
| 129 | + echo_nok "The tag '${1}' was not created because of an error." |
| 130 | + exit 2 |
| 131 | + fi |
| 132 | + echo_ok "The tag '$(<VERSION)' was created." |
| 133 | +} |
| 134 | + |
| 135 | +git_push() { |
| 136 | + echomoji ⏩ "»" "Pushing the tag '${2}' to '${1}'…" |
| 137 | + if ! git push "${1}" "${2}"; then |
| 138 | + echo_nok "The tag '${2}' could not be pushed to '${1}'." |
| 139 | + exit 2 |
| 140 | + fi |
| 141 | + echo_ok "The tag '${2}' was pushed." |
| 142 | +} |
| 143 | + |
| 144 | +git_merge() { |
| 145 | + echomoji ⏩ "»" "Merging '${1}'…" |
| 146 | + if ! git merge --no-ff "${1}"; then |
| 147 | + echo_nok "The branch '${1}' could not be merged." |
| 148 | + exit 2 |
| 149 | + fi |
| 150 | + echo_ok "The branch '${2}' was merged." |
| 151 | +} |
| 152 | + |
| 153 | +git_merge() { |
| 154 | + echomoji ⏩ "»" "Rebasing onto '${1}'…" |
| 155 | + if ! git rebase "${1}"; then |
| 156 | + echo_nok "Could not rebase onto '${1}'." |
| 157 | + exit 2 |
| 158 | + fi |
| 159 | + echo_ok "Rebased onto '${2}'." |
| 160 | +} |
| 161 | + |
| 162 | +### |
| 163 | +# MAIN |
| 164 | +### |
| 165 | + |
| 166 | +echomoji 📋 "▶︎" "Checking pre-requisites for releasing '$(<VERSION)'" |
| 167 | + |
| 168 | +check_origin |
| 169 | + |
| 170 | +check_develop |
| 171 | +check_tag |
| 172 | + |
| 173 | +git_switch release |
| 174 | +check_release |
| 175 | + |
| 176 | +echomoji 📋 "▶︎" "Releasing '$(<VERSION)'" |
| 177 | + |
| 178 | +git_merge develop |
| 179 | +check_tag |
| 180 | +git_tag "$(<VERSION)" |
| 181 | + |
| 182 | +git_push "origin" release |
| 183 | +git_push "origin" "$(<VERSION)" |
| 184 | + |
| 185 | +git_switch develop |
| 186 | +git_rebase release |
| 187 | + |
| 188 | +echomoji ✅ "◼︎" "The release of '$(<VERSION)' is complete." |
0 commit comments