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