This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathprepare_release.sh
executable file
Β·275 lines (226 loc) Β· 7.05 KB
/
prepare_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
set -eo pipefail
mute=">/dev/null 2>&1"
is_subsequent_release=0
base_branch="main"
build_number=0
# Get the directory where the script is stored
script_dir=$(dirname "$(readlink -f "$0")")
base_dir="${script_dir}/.."
#
# Output passed arguments to stderr and exit.
#
die() {
echo ""
cat >&2 <<< "$*"
exit 1
}
assert_ios_directory() {
if ! realpath "." | grep -q "/iOS"; then
die "π₯ Error: Run the script from inside the iOS project"
fi
}
assert_fastlane_installed() {
if ! command -v bundle &> /dev/null; then
die "π₯ Error: Bundle is not installed. See: https://app.asana.com/0/1202500774821704/1203766784006610/f"
fi
if ! bundle show fastlane &> /dev/null; then
die "π₯ Error: Fastlane is not installed. See: https://app.asana.com/0/1202500774821704/1203766784006610/f"
fi
}
assert_gh_installed_and_authenticated() {
if ! command -v gh &> /dev/null; then
die "π₯ Error: GitHub CLI is not installed. See: https://app.asana.com/0/1202500774821704/1203791243007683/f"
fi
if ! gh auth status 2>&1 | grep -q "β Logged in to github.com"; then
echo "π₯ Error: GitHub CLI is not authenticated. See: https://app.asana.com/0/1202500774821704/1203791243007683/f"
fi
}
print_usage_and_exit() {
local reason=$1
cat <<- EOF
Usage:
$ $(basename "$0") <version | hotfix-branch> [-v]
Current version: $(cut -d' ' -f3 < "${base_dir}/Configuration/Version.xcconfig")
Options:
-v Enable verbose mode
Arguments:
<version | hotfix-branch> Specify either a version number or a hotfix branch name.
EOF
die "${reason}"
}
stash() {
printf '%s' "Stashing your changes ... "
eval git stash "$mute"
echo "β
"
}
read_command_line_arguments() {
local input="$1"
local version_regexp="^[0-9]+(\.[0-9]+)*$"
if [ -z "$input" ]; then
print_usage_and_exit "π₯ Error: Missing argument"
fi
shift 1
while [[ "$#" -gt 0 ]]; do
case "$1" in
-v)
mute=
;;
-s|--subsequent)
is_subsequent_release=1
;;
*)
print_usage_and_exit "π₯ Error: Unknown option '$1'"
;;
esac
shift
done
if [[ $input =~ $version_regexp ]]; then
process_release "$input"
else
process_hotfix "$input"
fi
}
process_release() { # expected input e.g. "1.72.0"
version="$1"
release_branch="release/${version}"
echo "Processing version number: $version"
if [[ "$is_subsequent_release" -eq 1 ]]; then
# check if the release branch exists (it must exist for a subsequent release)
if ! release_branch_exists; then
die "π₯ Error: Release branch does not exist for a subsequent release!"
fi
base_branch="$release_branch"
else
# check if the release branch does NOT exist (it must NOT exist for an initial release)
if release_branch_exists; then
die "π₯ Error: Release branch already exists for an initial release!"
fi
fi
}
process_hotfix() { # expected input e.g. "hotfix/1.72.1"
version=$(echo "$1" | cut -d '/' -f 2)
release_branch="$1"
base_branch="$1"
is_hotfix=1
echo "Processing hotfix branch name: $release_branch"
if ! release_branch_exists; then
die "π₯ Error: Hotfix branch ${release_branch} does not exist. It should be created before you run this script."
fi
}
checkout_base_branch() {
eval git checkout "${base_branch}" "$mute"
eval git pull "$mute"
}
release_branch_exists() {
if git show-ref --verify --quiet "refs/heads/${release_branch}"; then
return 0
else
return 1
fi
}
create_release_branch() {
printf '%s' "Creating release branch ... "
if git show-ref --quiet "refs/heads/${release_branch}"; then
die "π₯ Error: Branch ${release_branch} already exists"
fi
eval git checkout -b "${release_branch}" "$mute"
if ! eval git push -u origin "${release_branch}" "$mute"; then
die "π₯ Error: Failed to push ${release_branch} to origin"
fi
echo "β
"
}
create_build_branch() {
printf '%s' "Creating build branch ... "
local temp_file
local latest_build_number
temp_file=$(mktemp)
bundle exec fastlane latest_build_number_for_version version:"$version" file_name:"$temp_file"
latest_build_number="$(<"$temp_file")"
build_number=$((latest_build_number + 1))
build_branch="${release_branch}-build-${build_number}"
if git show-ref --quiet "refs/heads/${build_branch}"; then
die "π₯ Error: Branch ${build_branch} already exists"
fi
eval git checkout -b "${build_branch}" "$mute"
if ! eval git push -u origin "${build_branch}" "$mute"; then
die "π₯ Error: Failed to push ${build_branch} to origin"
fi
echo "β
"
}
update_marketing_version() {
printf '%s' "Setting app version ... "
"$script_dir/set_version.sh" "${version}"
git add "${base_dir}/Configuration/Version.xcconfig" \
"${base_dir}/DuckDuckGo/Settings.bundle/Root.plist"
eval git commit --allow-empty -m \"Update version number\" "$mute"
echo "β
"
}
update_build_version() {
echo "Setting build version ..."
(cd "$base_dir" && bundle exec fastlane increment_build_number_for_version version:"${version}")
git add "${base_dir}/DuckDuckGo-iOS.xcodeproj/project.pbxproj"
if [[ "$(git diff --cached)" ]]; then
eval git commit --allow-empty -m \"Update build number\" "$mute"
echo "Setting build version ... β
"
else
printf "\nNo changes to build number β
\n"
fi
}
update_embedded_files() {
printf '%s' "Updating embedded files ... "
eval "$script_dir/update_embedded.sh" "$mute"
git add "${base_dir}/Core/AppTrackerDataSetProvider.swift" \
"${base_dir}/Core/trackerData.json" \
"${base_dir}/Core/AppPrivacyConfigurationDataProvider.swift" \
"${base_dir}/Core/ios-config.json"
if [[ "$(git diff --cached)" ]]; then
eval git commit -m \"Update embedded files\" "$mute"
echo "β
"
else
printf "\nNo changes to embedded files β
\n"
fi
}
update_release_notes() {
local release_notes_path="${base_dir}/fastlane/metadata/default/release_notes.txt"
echo "Please update release notes and save the file."
eval open -a TextEdit "${release_notes_path}" "$mute"
read -r -p "Press \`Enter\` when you're done to continue ..."
git add "${release_notes_path}"
if [[ "$(git diff --cached)" ]]; then
eval git commit -m \"Update release notes\" "$mute"
echo "Release notes updated β
"
else
echo "No changes to release notes β
"
fi
}
create_pull_request() {
printf '%s' "Creating PR ... "
eval git push origin "${build_branch}" "$mute"
eval gh pr create --title \"Release "${version}-${build_number}"\" --base "${release_branch}" --label \"Merge triggers release\" --assignee @me "$mute" --body-file "${script_dir}/assets/prepare-release-description"
eval gh pr view --web "$mute"
echo "β
"
}
main() {
assert_ios_directory
assert_fastlane_installed
assert_gh_installed_and_authenticated
stash
read_command_line_arguments "$@"
checkout_base_branch
if [[ "$is_subsequent_release" -eq 1 ]]; then
create_build_branch
elif [[ $is_hotfix ]]; then
create_build_branch
update_marketing_version
else # regular release
create_release_branch
create_build_branch
update_marketing_version
update_embedded_files
fi
update_build_version
update_release_notes
create_pull_request
}
main "$@"