Skip to content

Commit 59995a8

Browse files
committed
Add release script
1 parent 6cd156f commit 59995a8

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

release.sh

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

0 commit comments

Comments
 (0)