From 42272ccac70fdc592a2e92ecd235d84fd74ae60a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 2 Sep 2026 21:03:19 -0700 Subject: [PATCH] scripts: improve release.sh build and upload flow - Generate an ephemeral build config inheriting only signing properties so local developer settings in config.prop remain untouched - Remove gsed requirement by making in-place edits POSIX-compliant - Add ver_to_code and code_to_ver conversion functions - Add pre-upload validation and safe git staging - Add command usage handling Assisted-by: Gemini 3.8 Flash --- scripts/release.sh | 133 +++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 60 deletions(-) diff --git a/scripts/release.sh b/scripts/release.sh index babd736a1..0d9ceaafe 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash set -e +set -o pipefail -# On macOS, gsed is required (brew install gnu-sed) -# Required tools: gh +# Required tools: gh, apksigner # The GitHub cli (gh) has to be properly authenticated # These variables can be modified as needed @@ -12,8 +12,6 @@ APK_CERT=b4cb83b4dad99f997dbe872f013aa16c14eec41d167021f371f7e1330f273ee6 # These are constants, do not modify GCONFIG=app/gradle.properties -BUILDCMD="./build.py -c $CONFIG" -CWD=$(pwd) grep_prop() { local REGEX="s/^$1=//p" @@ -22,6 +20,14 @@ grep_prop() { sed -n "$REGEX" $FILES | head -n 1 } +ver_to_code() { + echo "$1" | awk '{ print int($1 * 1000) }' +} + +code_to_ver() { + echo "$1" | awk '{ v = $1 / 1000; if (v == int(v)) printf "%.1f\n", v; else print v }' +} + find_apksigner() { local sdk="${ANDROID_HOME:-$ANDROID_SDK_ROOT}" if [ -n "$sdk" ] && [ -d "$sdk/build-tools" ]; then @@ -40,20 +46,12 @@ verify_apks() { local apksigner apksigner=$(find_apksigner) || exit 1 - local out=$(grep_prop outdir $CONFIG) - if [ -z "$out" ]; then - out=out - fi - - local apks=("$out"/*.apk) + local apks=(out/*.apk) if [ ! -e "${apks[0]}" ]; then - echo "Error: No APKs found in $out" >&2 + echo "Error: No APKs found in out" >&2 exit 1 fi - local expected_cert - expected_cert=$(echo "$APK_CERT" | tr '[:upper:]' '[:lower:]') - echo "* Verifying APK signatures" for apk in "${apks[@]}"; do echo "Verifying $(basename "$apk")..." @@ -74,99 +72,112 @@ verify_apks() { fi for cert in $certs; do - if [ "$cert" != "$expected_cert" ]; then + if [ "$cert" != "$APK_CERT" ]; then echo "Error: $apk signature mismatch!" >&2 - echo " Expected: $expected_cert" >&2 + echo " Expected: $APK_CERT" >&2 echo " Found: $cert" >&2 exit 1 fi done - echo " -> Verified: $expected_cert" + echo " -> Verified: $APK_CERT" done echo "All APKs successfully verified!" } -ensure_config() { - # Make sure version is not commented out and exists - sed -i "s:^# version=:version=:g" $CONFIG - if ! grep -qE '^version=' $CONFIG; then - echo 'version=' >> $CONFIG - fi - # Make sure abiList is not set when building for release - sed -i "s:^abiList=:# abiList=:g" $CONFIG -} +# Generate a clean config for release builds inheriting only signing properties +gen_release_config() { + local ver=$1 + local cfg=$2 -disable_version_config() { - # Comment out version config - sed -i "s:^version=:# version=:g" $CONFIG + echo "version=$ver" > "$cfg" + + if [ -f "$CONFIG" ]; then + for prop in keyStore keyStorePass keyAlias keyPass; do + local val + val=$(grep_prop "$prop" "$CONFIG") + if [ -n "$val" ]; then + echo "$prop=$val" >> "$cfg" + fi + done + fi } # $1 = ver set_version() { local ver=$1 - local code=$(echo - | awk "{ print $ver * 1000 }") - local tag="v$ver" + local code=$(ver_to_code "$ver") - sed -i "s:versionCode=.*:versionCode=${code}:g" $GCONFIG - sed -i "s:version=.*:version=${ver}:g" $CONFIG + sed "s:versionCode=.*:versionCode=${code}:g" "$GCONFIG" > "$GCONFIG.tmp" + mv -f "$GCONFIG.tmp" "$GCONFIG" # Commit version code changes - git add -u . + git add "$GCONFIG" git status git commit -m "Release Magisk v$ver" -m "[skip ci]" } # $1 = ver build() { - [ -z $1 ] && exit 1 local ver=$1 git pull - set_version $ver - $BUILDCMD clean - $BUILDCMD all - $BUILDCMD -r all - $BUILDCMD -r app-legacy + set_version "$ver" + + local rel_config + rel_config=$(mktemp 2>/dev/null || mktemp -t 'release_config') + trap 'rm -f "$rel_config"' EXIT + gen_release_config "$ver" "$rel_config" + + local build_cmd="./build.py -c $rel_config" + $build_cmd clean + $build_cmd all + $build_cmd -r all + $build_cmd -r app-legacy + verify_apks } upload() { gh auth status + + if [ ! -f "$NOTES" ]; then + echo "Error: Release notes file '$NOTES' not found!" >&2 + exit 1 + fi + verify_apks + if [ ! -f "out/app-release.apk" ] || [ ! -f "out/apk-legacy-release.apk" ] || [ ! -f "out/app-debug.apk" ]; then + echo "Error: Required APKs missing in out" >&2 + exit 1 + fi + local code=$(grep_prop magisk.versionCode $GCONFIG) - local ver=$(echo - | awk "{ print $code / 1000 }") + local ver=$(code_to_ver "$code") local tag="v$ver" local title="Magisk v$ver" - local out=$(grep_prop outdir $CONFIG) - if [ -z $out ]; then - out=out - fi - - git tag $tag + git tag "$tag" git push origin master git push --tags # Publish release local release_apk="Magisk-v${ver}.apk" local legacy_apk="Magisk-v${ver}-legacy.apk" - cp $out/app-release.apk $release_apk - cp $out/apk-legacy-release.apk $legacy_apk - gh release create --verify-tag $tag -d -t "$title" -F $NOTES $release_apk $legacy_apk $out/app-debug.apk + cp "out/app-release.apk" "$release_apk" + cp "out/apk-legacy-release.apk" "$legacy_apk" + gh release create --verify-tag "$tag" -d -t "$title" -F "$NOTES" "$release_apk" "$legacy_apk" "out/app-debug.apk" - rm -f $release_apk $legacy_apk + rm -f "$release_apk" "$legacy_apk" } -# Use GNU sed on macOS -if command -v gsed >/dev/null; then - function sed() { gsed "$@"; } - export -f sed -fi +usage() { + echo "Usage: $0 | upload | verify>" >&2 + exit 1 +} case $1 in build ) - trap disable_version_config EXIT - ensure_config - build $2 + [ -z "$2" ] && usage + build "$2" ;; upload ) upload @@ -174,5 +185,7 @@ case $1 in verify ) verify_apks ;; - * ) exit 1 ;; + * ) + usage + ;; esac