From bf03e6ad2f82bc5b975f42a3cc46bb4ecf9c7eae Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 1 Nov 2026 12:48:06 -0500 Subject: [PATCH] Retry macOS stapling and validation (#36) --- packaging/macos/package.sh | 22 ++++++++++++++++++---- packaging/macos/verify.sh | 14 +++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packaging/macos/package.sh b/packaging/macos/package.sh index 75a00372..c39774e3 100755 --- a/packaging/macos/package.sh +++ b/packaging/macos/package.sh @@ -97,6 +97,20 @@ notarize() { fi } +# Apple's CloudKit ticket lookup can drop its connection after notarization succeeded. +# Stapling and validation are idempotent; a missing or bad ticket still fails every attempt. +stapler_retry() { + local action="$1" target="$2" try + for try in 1 2 3 4; do + xcrun stapler "$action" "$target" && return 0 + [ "$try" = 4 ] && break + echo "stapler $action failed (try $try of 4); retrying in $((try * 20)) s" >&2 + sleep $((try * 20)) + done + echo "error: stapler $action $(basename "$target") failed 4 times" >&2 + return 1 +} + # ---- TextureCraft.app ---------------------------------------------------------------------------- echo "==> assembling $APP" mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Resources" @@ -120,8 +134,8 @@ codesign --verify --strict --deep --verbose=2 "$APP" if [ "$NOTARIZE" = 1 ]; then ditto -c -k --keepParent "$APP" "$WORK/TextureCraft-notarize.zip" notarize "$WORK/TextureCraft-notarize.zip" - xcrun stapler staple "$APP" - xcrun stapler validate "$APP" + stapler_retry staple "$APP" + stapler_retry validate "$APP" spctl --assess --type execute -vvv "$APP" fi @@ -141,8 +155,8 @@ sign "$DMG" codesign --verify --strict --verbose=2 "$DMG" if [ "$NOTARIZE" = 1 ]; then notarize "$DMG" - xcrun stapler staple "$DMG" - xcrun stapler validate "$DMG" + stapler_retry staple "$DMG" + stapler_retry validate "$DMG" spctl --assess --type open --context context:primary-signature -vvv "$DMG" fi diff --git a/packaging/macos/verify.sh b/packaging/macos/verify.sh index 30dc1458..948107f0 100755 --- a/packaging/macos/verify.sh +++ b/packaging/macos/verify.sh @@ -70,6 +70,18 @@ assess() { return 1 } +# Validation fetches the ticket from Apple's CloudKit and can fail on a dropped connection. +stapler_validate() { + local target="$1" try + for try in 1 2 3 4; do + xcrun stapler validate "$target" && return 0 + [ "$try" = 4 ] && break + echo "stapler validate failed (try $try of 4); retrying in $((try * 20)) s" >&2 + sleep $((try * 20)) + done + return 1 +} + for f in "$DMG" "$CLI_ZIP"; do if [ ! -f "$f" ]; then echo "error: missing $f" >&2 @@ -126,7 +138,7 @@ fi echo "==> $(basename "$DMG")" codesign --verify --strict --verbose=2 "$DMG" if [ "$STRICT" = 1 ]; then - xcrun stapler validate "$DMG" || fail "no notarization ticket stapled to the DMG" + stapler_validate "$DMG" || fail "no notarization ticket stapled to the DMG" assess --assess --type open --context context:primary-signature -vv "$DMG" \ || fail "Gatekeeper rejects the DMG" fi