-
Notifications
You must be signed in to change notification settings - Fork 6
/
release
executable file
·193 lines (155 loc) · 4.79 KB
/
release
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
189
190
191
192
193
#!/bin/bash
#
# Script to release app : `./release --help`
#
set -e
git_path=$(which git)
GREEN="\033[0;32m"
CYAN="\033[0;36m"
NOCOLOR="\033[0m"
target_branch="main"
usage() {
printf "Usage : ./release [OPTION]\n\n"
printf "This script does following steps :\n"
printf " * Update CHANGELOG.md\n"
printf " * Create commit release and tag\n"
printf " * Merge into release (release/x.y) branch\n\n"
printf "Options :\n"
printf "\t-b <branch> : Default branch to work on (default to 'main').\n"
printf "\t-h : Display this help.\n"
}
print_step() {
echo -e "${GREEN}$1${NOCOLOR}"
}
print_sub_step() {
echo -e "${CYAN}$1${NOCOLOR}"
}
while getopts ":hb:" opt; do
case ${opt} in
h)
usage
exit 1
;;
b) target_branch=$OPTARG ;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
cd "$(dirname "$0")"
tag=$(cat version.txt)
echo "You are going to release version $tag."
read -r -p "Are you sure? [y/N] " response
if [[ ! "$response" =~ ^([yY])$ ]]; then
echo "Exit..."
exit 0
fi
release_branch=$(echo "$tag" | sed -Ene's#(0|[1-9][0-9]*)\.(0|[1-9][0-9]*).*#release/\1.\2#p')
repo_url=$($git_path remote -v | grep -m1 '^origin' | sed -Ene's#.*(git@[^[:space:]]*).*#\1#p' | sed -Ene's#git@(.*):(.*).git#https://\1/\2#p')
ensure_on_target_branch() {
if [ "$($git_path symbolic-ref --short HEAD)" != "$target_branch" ]; then
echo "You are not on $target_branch branch"
exit 1
fi
}
# https://github.com/git/git/blob/master/git-sh-setup.sh
require_clean_work_tree() {
$git_path rev-parse --verify HEAD >/dev/null || exit 1
$git_path update-index -q --ignore-submodules --refresh
if ! $git_path diff-files --quiet --ignore-submodules; then
echo >&2 "You have unstaged changes."
fi
if ! $git_path diff-index --cached --quiet --ignore-submodules HEAD --; then
echo >&2 "Your index contains uncommitted changes."
fi
if [ -n "$($git_path status --porcelain)" ]; then
echo >&2 "You have untracked files."
fi
}
ensure_tag_dont_exist() {
local remote=$($git_path ls-remote --tags origin "$tag")
local local=$($git_path tag -l "$tag")
if [ -n "$remote" ] || [ -n "$local" ]; then
echo "Tag '$tag' exist on remote or on local"
exit 1
fi
}
# https://gist.github.com/rverst/1f0b97da3cbeb7d93f4986df6e8e5695
function check_version() {
if [[ $1 =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]]; then
echo "$1"
else
echo ""
fi
}
ensure_tag_format() {
if ! $git_path check-ref-format "tags/$tag"; then
echo "Tag '$tag' is not a valid name"
exit 1
fi
semver=$(check_version "$tag")
if [[ ! "$semver" ]]; then
echo "Tag '$tag' doesn't follow semantic version (https://semver.org/)"
exit 1
fi
}
check_prerequisites() {
ensure_tag_format
ensure_tag_dont_exist
ensure_on_target_branch
require_clean_work_tree
}
prepare() {
print_step "Preparing sources for $tag..."
if [[ -f "CHANGELOG.md" ]]; then
print_sub_step " * Update CHANGELOG.md"
./gradlew generateChangelog -PfinalRelease >/dev/null
fi
}
release_source() {
print_step "Pushing release $tag..."
$git_path add --all . >/dev/null
print_sub_step " * Create release commit"
$git_path commit --quiet --allow-empty --signoff --message="Release $tag"
print_sub_step " * Create tag"
$git_path tag --annotate "$tag" --message="Release $tag"
print_sub_step " * Push release commit and tag"
$git_path push --quiet origin "$target_branch"
$git_path push --quiet origin "$tag"
}
merge_in_release() {
if [[ "${release_branch}" == "${target_branch}" ]]; then
return
fi
print_step "Merging into release branch $release_branch..."
local existed_in_local=$($git_path branch --list "$release_branch")
if [[ -z ${existed_in_local} ]]; then
print_sub_step " * Creating release branch"
$git_path checkout --quiet -b "$release_branch"
else
print_sub_step " * Checkout release branch and reset from remote"
$git_path checkout --quiet "$release_branch"
$git_path reset --quiet --hard "origin/$release_branch"
fi
print_sub_step " * Merge release on release branch"
$git_path merge --quiet --no-edit "$target_branch"
print_sub_step " * Push release branch"
$git_path push --quiet origin "$release_branch"
$git_path checkout --quiet "$target_branch"
}
check_prerequisites
prepare
release_source
merge_in_release
echo "-------------------"
echo -e "Release '$tag' pushed on remote :"
echo -e " \033[4m$repo_url/-/tags/$tag\033[0m"
echo -e "-> Add any necessary release notes"
echo -e "-> Creating artifacts : follow CI pipeline"
echo "-------------------"