-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrooted-ota.sh
executable file
·565 lines (461 loc) · 20.6 KB
/
rooted-ota.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env bash
# Requires git, jq, and curl
KEY_AVB=${KEY_AVB:-avb.key}
KEY_OTA=${KEY_OTA:-ota.key}
CERT_OTA=${CERT_OTA:-ota.crt}
# Or else, set these env vars
KEY_AVB_BASE64=${KEY_AVB_BASE64:-''}
KEY_OTA_BASE64=${KEY_OTA_BASE64:-''}
CERT_OTA_BASE64=${CERT_OTA_BASE64:-''}
# Set these env vars, or else these params will be queries interactively
# PASSPHRASE_AVB
# PASSPHRASE_OTA
# Enable debug output only after sensitive vars have been set, to reduce risk of leak
DEBUG=${DEBUG:-''}
if [[ -n "${DEBUG}" ]]; then set -x; fi
# Mandatory params
DEVICE_ID=${DEVICE_ID:-} # See here for device IDs https://grapheneos.org/releases
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
GITHUB_REPO=${GITHUB_REPO:-''}
# Optional
# If you want an OTA patched with magisk, set the preinit for your device
MAGISK_PREINIT_DEVICE=${MAGISK_PREINIT_DEVICE:-}
# Skip creation of rootless OTA by setting to "true"
SKIP_ROOTLESS=${SKIP_ROOTLESS:-'false'}
# https://grapheneos.org/releases#stable-channel
OTA_VERSION=${OTA_VERSION:-'latest'}
# It's recommended to pin magisk version in combination with AVB_ROOT_VERSION.
# Breaking changes in magisk might need to be adapted in new avbroot version
# Find latest magisk version here: https://github.com/topjohnwu/Magisk/releases, or:
# curl --fail -sL -I -o /dev/null -w '%{url_effective}' https://github.com/topjohnwu/Magisk/releases/latest | sed 's/.*\/tag\///;'
# renovate: datasource=github-releases packageName=topjohnwu/Magisk versioning=semver-coerced
DEFAULT_MAGISK_VERSION=v28.1
MAGISK_VERSION=${MAGISK_VERSION:-${DEFAULT_MAGISK_VERSION}}
SKIP_CLEANUP=${SKIP_CLEANUP:-''}
# Set asset released by this script to latest version, even when OTA_VERSION already exists for this device
FORCE_OTA_SERVER_UPLOAD=${FORCE_OTA_SERVER_UPLOAD:-'false'}
# Forces the artifacts to be built (and uploaded to a release)
# even it a release already contains the combination of device and flavor.
# This will lead to multiple artifacts with different commits on the release (that are not linked in the OTA server and thus are likely never used).
# However, except for test builds, we want the changes to be rolled out with new version.
# So these artifacts are just a waste of storage resources. Example
# shiba-2025020500-3e0add9-rootless.zip
# shiba-2025020500-6718632-rootless.zip
FORCE_BUILD=${FORCE_BUILD:-'false'}
# Skip setting asset released by this script to latest version, even when OTA_VERSION is latest for this device
# Takes precedence over FORCE_OTA_SERVER_UPLOAD
SKIP_OTA_SERVER_UPLOAD=${SKIP_OTA_SERVER_UPLOAD:-'false'}
# Upload OTA to test folder on OTA server
UPLOAD_TEST_OTA=${UPLOAD_TEST_OTA:-false}
OTA_CHANNEL=${OTA_CHANNEL:-stable} # Alternative: 'alpha'
NO_COLOR=${NO_COLOR:-''}
OTA_BASE_URL="https://releases.grapheneos.org"
# renovate: datasource=github-releases packageName=chenxiaolong/avbroot versioning=semver
AVB_ROOT_VERSION=3.12.0
# renovate: datasource=github-releases packageName=chenxiaolong/Custota versioning=semver-coerced
CUSTOTA_VERSION=5.6
# renovate: datasource=git-refs packageName=https://github.com/chenxiaolong/my-avbroot-setup currentValue=master
PATCH_PY_COMMIT=16636c3
# renovate: datasource=docker packageName=python
PYTHON_VERSION=3.13.2-alpine
# renovate: datasource=github-releases packageName=chenxiaolong/OEMUnlockOnBoot versioning=semver-coerced
OEMUNLOCKONBOOT_VERSION=1.1
# renovate: datasource=github-releases packageName=chenxiaolong/afsr versioning=semver
AFSR_VERSION=1.0.3
CHENXIAOLONG_PK='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDOe6/tBnO7xZhAWXRj3ApUYgn+XZ0wnQiXM8B7tPgv4'
GIT_PUSH_RETRIES=10
set -o nounset -o pipefail -o errexit
declare -A POTENTIAL_ASSETS
function generateKeys() {
downloadAvBroot
# https://github.com/chenxiaolong/avbroot/tree/077a80f4ce7233b0e93d4a1477d09334af0da246#generating-keys
# Generate the AVB and OTA signing keys.
.tmp/avbroot key generate-key -o $KEY_AVB
.tmp/avbroot key generate-key -o $KEY_OTA
# Convert the public key portion of the AVB signing key to the AVB public key metadata format.
# This is the format that the bootloader requires when setting the custom root of trust.
.tmp/avbroot key extract-avb -k $KEY_AVB -o avb_pkmd.bin
# Generate a self-signed certificate for the OTA signing key. This is used by recovery to verify OTA updates when sideloading.
.tmp/avbroot key generate-cert -k $KEY_OTA -o $CERT_OTA
echo Upload these to your CI server, if necessary.
echo The script takes these values as env or file
key2base64
}
function key2base64() {
KEY_AVB_BASE64=$(base64 -w0 "$KEY_AVB") && echo "KEY_AVB_BASE64=$KEY_AVB_BASE64"
KEY_OTA_BASE64=$(base64 -w0 "$KEY_OTA") && echo "KEY_OTA_BASE64=$KEY_OTA_BASE64"
CERT_OTA_BASE64=$(base64 -w0 "$CERT_OTA") && echo "CERT_OTA_BASE64=$CERT_OTA_BASE64"
export KEY_AVB_BASE64 KEY_OTA_BASE64 CERT_OTA_BASE64
}
function createAndReleaseRootedOta() {
createRootedOta
releaseOta
createOtaServerData
uploadOtaServerData
}
function createRootedOta() {
[[ "$SKIP_CLEANUP" != 'true' ]] && trap cleanup EXIT ERR
findLatestVersion
checkBuildNecessary
downloadAndroidDependencies
patchOTAs
}
function cleanup() {
print "Cleaning up..."
rm -rf .tmp
unset KEY_AVB_BASE64 KEY_OTA_BASE64 CERT_OTA_BASE64
print "Cleanup complete."
}
function checkBuildNecessary() {
local currentCommit
currentCommit=$(git rev-parse --short HEAD)
POTENTIAL_ASSETS=()
if [[ -n "$MAGISK_PREINIT_DEVICE" ]]; then
# e.g. oriole-2023121200-magisk-v26.4-4647f74-dirty.zip
POTENTIAL_ASSETS['magisk']="${DEVICE_ID}-${OTA_VERSION}-${currentCommit}-magisk-${MAGISK_VERSION}$(createAssetSuffix).zip"
else
printGreen "MAGISK_PREINIT_DEVICE not set for device, not creating magisk OTA"
fi
if [[ "$SKIP_ROOTLESS" != 'true' ]]; then
POTENTIAL_ASSETS['rootless']="${DEVICE_ID}-${OTA_VERSION}-${currentCommit}-rootless$(createAssetSuffix).zip"
else
printGreen "SKIP_ROOTLESS set, not creating rootless OTA"
fi
RELEASE_ID=''
local response
if [[ -z "$GITHUB_REPO" ]]; then print "Env Var GITHUB_REPO not set, skipping check for existing release" && return; fi
print "Potential release: ${OTA_VERSION}"
local params=()
local url="https://api.github.com/repos/${GITHUB_REPO}/releases"
if [ -n "${GITHUB_TOKEN}" ]; then
params+=("-H" "Authorization: token ${GITHUB_TOKEN}")
fi
params+=("-H" "Accept: application/vnd.github.v3+json")
response=$(
curl --fail -sL "${params[@]}" "${url}" |
jq --arg release_tag "${OTA_VERSION}" '.[] | select(.tag_name == $release_tag) | {id, tag_name, name, published_at, assets}'
)
if [[ -n ${response} ]]; then
RELEASE_ID=$(echo "${response}" | jq -r '.id')
print "Release ${OTA_VERSION} exists. ID=$RELEASE_ID"
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local selectedAsset POTENTIAL_ASSET_NAME="${POTENTIAL_ASSETS[$flavor]}"
print "Checking if asset exists ${POTENTIAL_ASSET_NAME}"
# Save some storage by not building and uploading every new commit as asset
selectedAsset=$(echo "${response}" | jq -r --arg assetPrefix "${DEVICE_ID}-${OTA_VERSION}" \
'.assets[] | select(.name | startswith($assetPrefix)) | .name' \
| grep "${flavor}" || true)
if [[ -n "${selectedAsset}" ]] && [[ "$FORCE_BUILD" != 'true' ]] && [[ "$UPLOAD_TEST_OTA" != 'true' ]]; then
printGreen "Skipping build of asset name '$POTENTIAL_ASSET_NAME'. Because this flavor already is released with a different commit." \
"Set FORCE_BUILD or UPLOAD_TEST_OTA to force. Assets found on release: ${selectedAsset//$'\n'/ }"
unset "POTENTIAL_ASSETS[$flavor]"
else
print "No asset found with name '$POTENTIAL_ASSET_NAME'."
fi
done
if [ "${#POTENTIAL_ASSETS[@]}" -eq 0 ]; then
printGreen "All potential assets already exist. Exiting"
exit 0
fi
else
print "Release ${OTA_VERSION} does not exist."
fi
}
function checkMandatoryVariable() {
for var_name in "$@"; do
local var_value="${!var_name}"
if [[ -z "$var_value" ]]; then
printRed "Missing mandatory param $var_name"
exit 1
fi
done
}
function createAssetSuffix() {
local suffix=''
if [[ "${UPLOAD_TEST_OTA}" == 'true' ]]; then
suffix+='-test'
fi
if [[ -n "$(git status --porcelain --untracked-files=no)" ]]; then
suffix+='-dirty'
fi
echo "$suffix"
}
function downloadAndroidDependencies() {
checkMandatoryVariable 'MAGISK_VERSION' 'OTA_TARGET'
mkdir -p .tmp
if ! ls ".tmp/magisk-$MAGISK_VERSION.apk" >/dev/null 2>&1 && [[ "${POTENTIAL_ASSETS['magisk']+isset}" ]]; then
curl --fail -sLo ".tmp/magisk-$MAGISK_VERSION.apk" "https://github.com/topjohnwu/Magisk/releases/download/$MAGISK_VERSION/Magisk-$MAGISK_VERSION.apk"
fi
if ! ls ".tmp/$OTA_TARGET.zip" >/dev/null 2>&1; then
curl --fail -sLo ".tmp/$OTA_TARGET.zip" "$OTA_URL"
fi
}
function findLatestVersion() {
checkMandatoryVariable DEVICE_ID
if [[ "$MAGISK_VERSION" == 'latest' ]]; then
MAGISK_VERSION=$(curl --fail -sL -I -o /dev/null -w '%{url_effective}' https://github.com/topjohnwu/Magisk/releases/latest | sed 's/.*\/tag\///;')
fi
print "Magisk version: $MAGISK_VERSION"
# Search for a new version grapheneos.
# e.g. https://releases.grapheneos.org/shiba-stable
if [[ "$OTA_VERSION" == 'latest' ]]; then
OTA_VERSION=$(curl --fail -sL "$OTA_BASE_URL/$DEVICE_ID-$OTA_CHANNEL" | head -n1 | awk '{print $1;}')
fi
GRAPHENE_TYPE=${GRAPHENE_TYPE:-'ota_update'} # Other option: factory
OTA_TARGET="$DEVICE_ID-$GRAPHENE_TYPE-$OTA_VERSION"
OTA_URL="$OTA_BASE_URL/$OTA_TARGET.zip"
# e.g. shiba-ota_update-2023121200
print "OTA target: $OTA_TARGET; OTA URL: $OTA_URL"
}
function downloadAvBroot() {
downloadAndVerifyFromChenxiaolong 'avbroot' "$AVB_ROOT_VERSION"
}
function downloadAndVerifyFromChenxiaolong() {
local repo="$1"
local version="$2"
local artifact="${3:-$1}" # optional: If not set, use repo name
local url="https://github.com/chenxiaolong/${repo}/releases/download/v${version}/${artifact}-${version}-x86_64-unknown-linux-gnu.zip"
local downloadedZipFile
downloadedZipFile="$(mktemp)"
mkdir -p .tmp
if ! ls ".tmp/${artifact}" >/dev/null 2>&1; then
curl --fail -sL "${url}" > "${downloadedZipFile}"
curl --fail -sL "${url}.sig" > "${downloadedZipFile}.sig"
# Validate against author's public key
ssh-keygen -Y verify -I chenxiaolong -f <(echo "chenxiaolong $CHENXIAOLONG_PK") -n file \
-s "${downloadedZipFile}.sig" < "${downloadedZipFile}"
echo N | unzip "${downloadedZipFile}" -d .tmp
rm "${downloadedZipFile}"*
chmod +x ".tmp/${artifact}" # e.g. .tmp/custota-tool
fi
}
function patchOTAs() {
downloadAvBroot
downloadAndVerifyFromChenxiaolong 'afsr' "$AFSR_VERSION"
if ! ls ".tmp/custota.zip" >/dev/null 2>&1; then
curl --fail -sL "https://github.com/chenxiaolong/Custota/releases/download/v${CUSTOTA_VERSION}/Custota-${CUSTOTA_VERSION}-release.zip" > .tmp/custota.zip
curl --fail -sL "https://github.com/chenxiaolong/Custota/releases/download/v${CUSTOTA_VERSION}/Custota-${CUSTOTA_VERSION}-release.zip.sig" > .tmp/custota.zip.sig
fi
if ! ls ".tmp/oemunlockonboot.zip" >/dev/null 2>&1; then
curl --fail -sL "https://github.com/chenxiaolong/OEMUnlockOnBoot/releases/download/v${OEMUNLOCKONBOOT_VERSION}/OEMUnlockOnBoot-${OEMUNLOCKONBOOT_VERSION}-release.zip" > .tmp/oemunlockonboot.zip
curl --fail -sL "https://github.com/chenxiaolong/OEMUnlockOnBoot/releases/download/v${OEMUNLOCKONBOOT_VERSION}/OEMUnlockOnBoot-${OEMUNLOCKONBOOT_VERSION}-release.zip.sig" > .tmp/oemunlockonboot.zip.sig
fi
if ! ls ".tmp/my-avbroot-setup" >/dev/null 2>&1; then
git clone https://github.com/chenxiaolong/my-avbroot-setup .tmp/my-avbroot-setup
(cd .tmp/my-avbroot-setup && git checkout ${PATCH_PY_COMMIT})
fi
base642key
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local targetFile=".tmp/${POTENTIAL_ASSETS[$flavor]}"
if ls "$targetFile" >/dev/null 2>&1; then
printGreen "File $targetFile already exists locally, not patching."
else
local args=()
args+=("--output" "$targetFile")
args+=("--input" ".tmp/$OTA_TARGET.zip")
args+=("--sign-key-avb" "$KEY_AVB")
args+=("--sign-key-ota" "$KEY_OTA")
args+=("--sign-cert-ota" "$CERT_OTA")
if [[ "$flavor" == 'magisk' ]]; then
args+=("--patch-arg=--magisk" "--patch-arg" ".tmp/magisk-$MAGISK_VERSION.apk")
args+=("--patch-arg=--magisk-preinit-device" "--patch-arg" "$MAGISK_PREINIT_DEVICE")
fi
# If env vars not set, passphrases will be queried interactively
if [ -v PASSPHRASE_AVB ]; then
args+=("--pass-avb-env-var" "PASSPHRASE_AVB")
fi
if [ -v PASSPHRASE_OTA ]; then
args+=("--pass-ota-env-var" "PASSPHRASE_OTA")
fi
args+=("--module-custota" ".tmp/custota.zip")
args+=("--module-oemunlockonboot" ".tmp/oemunlockonboot.zip")
# We patch it later if necessary
args+=("--skip-custota-tool")
# We need to add .tmp to PATH, but we can't use $PATH: because this would be the PATH of the host not the container
# Python image is designed to run as root, so chown the files it creates back at the end
# ... room for improvement 😐️
docker run --rm -v "$PWD:/app" -v "$PWD/.tmp:/app/.tmp" -w /app \
-e PATH='/bin:/usr/local/bin:/sbin:/usr/bin/:/app/.tmp' \
-e PASSPHRASE_AVB="$PASSPHRASE_AVB" -e PASSPHRASE_OTA="$PASSPHRASE_OTA" \
python:${PYTHON_VERSION} sh -c \
"apk add openssh && \
pip install -r .tmp/my-avbroot-setup/requirements.txt && \
python .tmp/my-avbroot-setup/patch.py ${args[*]} && \
chown -R $(id -u):$(id -g) .tmp"
printGreen "Finished patching file ${targetFile}"
fi
done
}
function base642key() {
set +x # Don't expose secrets to log
if [ -n "$KEY_AVB_BASE64" ]; then
echo "$KEY_AVB_BASE64" | base64 -d >.tmp/$KEY_AVB
KEY_AVB=.tmp/$KEY_AVB
fi
if [ -n "$KEY_OTA_BASE64" ]; then
echo "$KEY_OTA_BASE64" | base64 -d >.tmp/$KEY_OTA
KEY_OTA=.tmp/$KEY_OTA
fi
if [ -n "$CERT_OTA_BASE64" ]; then
echo "$CERT_OTA_BASE64" | base64 -d >.tmp/$CERT_OTA
CERT_OTA=.tmp/$CERT_OTA
fi
if [[ -n "${DEBUG}" ]]; then set -x; fi
}
function releaseOta() {
checkMandatoryVariable 'GITHUB_REPO' 'GITHUB_TOKEN'
local response changelog
if [[ -z "$RELEASE_ID" ]]; then
changelog=$(curl -sL -X POST -H "Authorization: token $GITHUB_TOKEN" \
-d "{
\"tag_name\": \"$OTA_VERSION\",
\"target_commitish\": \"main\"
}" \
"https://api.github.com/repos/$GITHUB_REPO/releases/generate-notes" | jq -r '.body // empty')
# Replace \n by \\n to keep them as chars
changelog="Update to [GrapheneOS ${OTA_VERSION}](https://grapheneos.org/releases#${OTA_VERSION}).\n\n$(echo "${changelog}" | sed ':a;N;$!ba;s/\n/\\n/g')"
response=$(curl -sL -X POST -H "Authorization: token $GITHUB_TOKEN" \
-d "{
\"tag_name\": \"$OTA_VERSION\",
\"target_commitish\": \"main\",
\"name\": \"$OTA_VERSION\",
\"body\": \"${changelog}\"
}" \
"https://api.github.com/repos/$GITHUB_REPO/releases")
RELEASE_ID=$(echo "${response}" | jq -r '.id // empty')
if [[ -n "${RELEASE_ID}" ]]; then
printGreen "Release created successfully with ID: ${RELEASE_ID}"
elif echo "${response}" | jq -e '.status == "422"' > /dev/null; then
# In case release has been created in the meantime (e.g. matrix job for multiple devices concurrently)
RELEASE_ID=$(curl -sL \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${GITHUB_REPO}/releases" | \
jq -r --arg release_tag "${OTA_VERSION}" '.[] | select(.tag_name == $release_tag) | .id // empty')
if [[ -n "${RELEASE_ID}" ]]; then
printGreen "Cannot create release but found existing release for ${OTA_VERSION}. ID=$RELEASE_ID"
else
printRed "Cannot create release for ${OTA_VERSION} because it seems to exist but still cannot find ID."
exit 1
fi
else
errors=$(echo "${response}" | jq -r '.errors')
printRed "Failed to create release for ${OTA_VERSION}. Errors: ${errors}"
exit 1
fi
fi
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local assetName="${POTENTIAL_ASSETS[$flavor]}"
uploadFile ".tmp/$assetName" "$assetName" "application/zip"
done
}
function uploadFile() {
local sourceFileName="$1"
local targetFileName="$2"
local contentType="$3"
# Note that --data-binary might lead to out of memory
curl --fail -X POST -H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: $contentType" \
--upload-file "$sourceFileName" \
"https://uploads.github.com/repos/$GITHUB_REPO/releases/$RELEASE_ID/assets?name=$targetFileName"
}
function createOtaServerData() {
downloadCusotaTool
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local POTENTIAL_ASSET_NAME="${POTENTIAL_ASSETS[$flavor]}"
local targetFile=".tmp/${POTENTIAL_ASSET_NAME}"
local args=()
args+=("--input" "${targetFile}")
args+=("--output" "${targetFile}.csig")
args+=("--key" "$KEY_OTA")
args+=("--cert" "$CERT_OTA")
# If env vars not set, passphrases will be queried interactively
if [ -v PASSPHRASE_OTA ]; then
args+=("--passphrase-env-var" "PASSPHRASE_OTA")
fi
.tmp/custota-tool gen-csig "${args[@]}"
mkdir -p ".tmp/${flavor}"
local args=()
args+=("--file" ".tmp/${flavor}/${DEVICE_ID}.json")
# e.g. https://github.com/schnatterer/rooted-graphene/releases/download/2023121200-v26.4-e54c67f/oriole-ota_update-2023121200.zip
# Instead of constructing the location we could also parse it from the upload response
args+=("--location" "https://github.com/$GITHUB_REPO/releases/download/$OTA_VERSION/$POTENTIAL_ASSET_NAME")
.tmp/custota-tool gen-update-info "${args[@]}"
done
}
function downloadCusotaTool() {
downloadAndVerifyFromChenxiaolong 'Custota' "$CUSTOTA_VERSION" 'custota-tool'
}
function uploadOtaServerData() {
# Update OTA server (github pages)
local current_branch current_commit current_author
current_branch=$(git rev-parse --abbrev-ref HEAD)
current_commit=$(git rev-parse --short HEAD)
current_author=$(git log -1 --format="%an <%ae>")
folderPrefix=''
if [[ "${UPLOAD_TEST_OTA}" == 'true' ]]; then
folderPrefix='test/'
fi
git checkout gh-pages
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local POTENTIAL_ASSET_NAME="${POTENTIAL_ASSETS[$flavor]}"
local targetFile="${folderPrefix}${flavor}/${DEVICE_ID}.json"
uploadFile ".tmp/${POTENTIAL_ASSET_NAME}.csig" "$POTENTIAL_ASSET_NAME.csig" "application/octet-stream"
mkdir -p "${folderPrefix}${flavor}"
# update only, if current $DEVICE_ID.json does not contain $OTA_VERSION
# We don't want to trigger users to upgrade on new commits from this repo or new magisk versions
# They can manually upgrade by downloading the OTAs from the releases and "adb sideload" them
if ! grep -q "$OTA_VERSION" "${targetFile}" || [[ "$FORCE_OTA_SERVER_UPLOAD" == 'true' ]] && [[ "$SKIP_OTA_SERVER_UPLOAD" != 'true' ]]; then
cp ".tmp/${flavor}/$DEVICE_ID.json" "${targetFile}"
git add "${targetFile}"
elif grep -q "${OTA_VERSION}" "${targetFile}"; then
printGreen "Skipping update of OTA server, because ${OTA_VERSION} already in ${folderPrefix}${flavor}/${DEVICE_ID}.json and FORCE_OTA_SERVER_UPLOAD is false."
else
printGreen "Skipping update of OTA server, because SKIP_OTA_SERVER_UPLOAD is true."
fi
done
if ! git diff-index --quiet HEAD; then
# Commit and push only when there are changes
git config user.name "GitHub Actions" && git config user.email "actions@github.com"
git commit \
--message "Update device $DEVICE_ID basing on commit $current_commit" \
--author="$current_author"
gitPushWithRetries
fi
# Switch back to the original branch
git checkout "$current_branch"
}
function gitPushWithRetries() {
local count=0
while [ $count -lt $GIT_PUSH_RETRIES ]; do
git pull --rebase
if git push origin gh-pages; then
break
else
count=$((count + 1))
printGreen "Retry $count/$GIT_PUSH_RETRIES failed. Retrying..."
sleep 2
fi
done
if [ $count -eq $GIT_PUSH_RETRIES ]; then
printRed "Failed to push to gh-pages after $GIT_PUSH_RETRIES attempts."
exit 1
fi
}
function print() {
echo -e "$(date '+%Y-%m-%d %H:%M:%S'): $*"
}
function printGreen() {
if [[ -z "${NO_COLOR}" ]]; then
echo -e "\e[32m$(date '+%Y-%m-%d %H:%M:%S'): $*\e[0m"
else
print "$@"
fi
}
function printRed() {
if [[ -z "${NO_COLOR}" ]]; then
echo -e "\e[31m$(date '+%Y-%m-%d %H:%M:%S'): $*\e[0m"
else
print "$@"
fi
}