Files
AlexKucera 5f92241308 fix(release): make packaging survive real runs
The first real notarization runs exposed five ways the script lied or
stopped short. codesign's Authority output was grepped through a pipe,
so under pipefail a grep -q match SIGPIPEd codesign and a good export
read as "not Developer ID signed"; the dump is captured first. notarytool
--wait exits 0 for an Invalid submission, so the verdict is now the
status field of its JSON, with Apple's log printed on anything but
Accepted. The DMG itself is signed before submission (Gatekeeper's
open-assessment needs a signature for the ticket to ride), and the
staple is retried because the ticket can reach the CDN after Accepted.

Two checks run before anything is notarized: the exported app must not
link libyaml and the bundled apex must run from the export. Both would
have caught the broken bundle the first Accepted DMG carried. The
identity refusal now points at developer.apple.com + a Keychain Access
CSR, because Xcode 27.2's Manage Certificates refuses Developer ID
creation even for the Account Holder.
2026-12-01 15:12:25 +01:00

136 lines
6.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Release packaging: archive → Developer ID export → DMG → notarize → staple.
#
# Contract:
# - Bumps nothing and tags nothing. Run from the commit that already carries
# the version in MARKETING_VERSION; the git tag is the caller's job.
# - Refuses BEFORE building when no valid "Developer ID Application" identity
# is in the keychain. Create it at developer.apple.com ▸ Certificates ▸ + ▸
# Developer ID Application from a Keychain Access ▸ Certificate Assistant CSR —
# Xcode's Manage Certificates refuses this even for the Account Holder (stale
# role cache). The private key lives only on the Mac that made the CSR — a
# portal-side certificate without it is not an identity.
# - Notarizes through the `museion-notary` notarytool keychain profile, created
# once by the Account Holder:
# xcrun notarytool store-credentials museion-notary \
# --apple-id <apple-id> --team-id <team-id> --password <app-specific-password>
# NOTARIZE=0 skips notarize+staple (a Developer-ID-signed DMG Gatekeeper still warns on).
# - Output: dist/Museion-<MARKETING_VERSION>.dmg (gitignored). The version is
# read from build settings, never from the pbxproj text. Apple Silicon only —
# the deployment target is macOS 27, which does not run on Intel.
set -euo pipefail
PROJECT="Museion.xcodeproj"
SCHEME="Museion"
APP_NAME="Museion.app"
CONFIGURATION="Release"
IDENTITY="Developer ID Application"
NOTARY_PROFILE="museion-notary"
DIST="dist"
DESTINATION='platform=macOS,arch=arm64'
cd "$(dirname "$0")/.." # repo root — PROJECT is relative (same guard as deploy.sh)
if ! security find-identity -v -p codesigning | grep -q "\"$IDENTITY:"; then
echo "No valid \"$IDENTITY\" identity in the keychain." >&2
echo "Create it once: developer.apple.com ▸ Certificates ▸ + ▸ $IDENTITY (CSR from Keychain Access ▸ Certificate Assistant)" >&2
exit 1
fi
SIGNER=$(security find-identity -v -p codesigning | sed -n 's/.*"\('"$IDENTITY"':[^"]*\)".*/\1/p' | head -1)
VERSION=$(xcodebuild -project "$PROJECT" -scheme "$SCHEME" -configuration "$CONFIGURATION" \
-destination "$DESTINATION" -showBuildSettings 2>/dev/null \
| awk -F'= ' '/MARKETING_VERSION = / {print $2; exit}')
if [[ -z "$VERSION" ]]; then
echo "MARKETING_VERSION not found in build settings" >&2
exit 1
fi
TEAM=$(xcodebuild -project "$PROJECT" -scheme "$SCHEME" -configuration "$CONFIGURATION" \
-destination "$DESTINATION" -showBuildSettings 2>/dev/null \
| awk -F'= ' '/DEVELOPMENT_TEAM = / {print $2; exit}')
DMG="$DIST/Museion-$VERSION.dmg"
echo "Packaging $SCHEME $VERSION"
# xcodebuild -quiet prints nothing on success; keep the log to show on failure.
log=$(mktemp -t museion-release)
trap 'rm -f "$log"' EXIT
run() { echo "→ $*"; if ! "$@" >"$log" 2>&1; then echo "Failed: $*" >&2; cat "$log" >&2; exit 1; fi; }
rm -rf "$DIST"
mkdir -p "$DIST"
run xcodebuild -project "$PROJECT" -scheme "$SCHEME" -configuration "$CONFIGURATION" \
-destination "$DESTINATION" -archivePath "$DIST/Museion.xcarchive" -quiet archive
cat >"$DIST/export-options.plist" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>method</key><string>developer-id</string>
<key>signingStyle</key><string>automatic</string>
<key>teamID</key><string>$TEAM</string>
<key>destination</key><string>export</string>
</dict></plist>
EOF
run xcodebuild -exportArchive -archivePath "$DIST/Museion.xcarchive" \
-exportOptionsPlist "$DIST/export-options.plist" -exportPath "$DIST/export" \
-allowProvisioningUpdates -quiet
APP="$DIST/export/$APP_NAME"
if [[ ! -d "$APP" ]]; then
echo "Export did not produce $APP" >&2
exit 1
fi
run codesign --verify --deep --strict "$APP"
# Capture first: `codesign -dvv | grep -q` SIGPIPEs codesign under pipefail (grep
# quits at the first Authority line while codesign still writes its Info.plist dump).
SIGNATURE=$(codesign -dvv "$APP" 2>&1)
grep -q '^Authority=Developer ID Application' <<<"$SIGNATURE" \
|| { echo "Export is not Developer ID signed:" >&2; grep '^Authority=' <<<"$SIGNATURE" >&2; exit 1; }
# The bundled renderer must run from the SHIPPED layout (rpath → Resources/libyaml.bundle), and
# the app must not link libyaml — a bare dylib in the synced Resources folder becomes a link target
# silently (Xcode sniffs Mach-O content), which is why the vendored copy sits in a .bundle.
if otool -L "$APP/Contents/MacOS/$SCHEME" | grep -q libyaml; then
echo "The app links libyaml — it must only be apex's dependency, never the app's" >&2; exit 1
fi
run "$APP/Contents/Resources/apex" --version
# A mounted leftover from an earlier run makes the create fail with a busy volume.
hdiutil detach "/Volumes/$SCHEME $VERSION" -quiet 2>/dev/null || true
STAGE="$DIST/stage"
mkdir -p "$STAGE"
ditto "$APP" "$STAGE/$APP_NAME" # ditto preserves the bundle's signature and xattrs
ln -s /Applications "$STAGE/Applications"
# diskutil image create replaces the deprecated `hdiutil create` (macOS 27).
run diskutil image create from --format UDZO --volumeName "$SCHEME $VERSION" "$STAGE" "$DMG"
rm -rf "$STAGE" "$DIST/export"
# Gatekeeper's open-assessment needs the IMAGE signed too; the notary ticket is stapled onto this signature.
run codesign --timestamp --sign "$SIGNER" "$DMG"
if [[ "${NOTARIZE:-1}" == "1" ]]; then
# `--wait` exits 0 on Invalid too — the status FIELD is the verdict. (JSON goes to
# stdout only; notarytool has no output-path option.)
echo "→ xcrun notarytool submit $DMG --keychain-profile $NOTARY_PROFILE --wait"
if ! xcrun notarytool submit "$DMG" --keychain-profile "$NOTARY_PROFILE" --wait --output-format json >"$DIST/notary.json" 2>"$log"; then
echo "Failed: notarytool submit" >&2; cat "$log" >&2; exit 1
fi
if ! grep -Eq '"status" *: *"Accepted"' "$DIST/notary.json"; then
echo "Notarization not accepted — Apple's log:" >&2
xcrun notarytool log --keychain-profile "$NOTARY_PROFILE" \
"$(awk -F'"' '/"id" *:/ {print $4; exit}' "$DIST/notary.json")" >&2
exit 1
fi
# The ticket can reach the CDN a few seconds after "Accepted"; stapler 404s in that window.
for attempt in 1 2 3 4 5 6; do
xcrun stapler staple "$DMG" && break
[[ "$attempt" == 6 ]] && { echo "Stapling failed after 6 attempts; retry: xcrun stapler staple $DMG" >&2; exit 1; }
echo " ticket not on the CDN yet, retrying in 10 s ($attempt/6)…"
sleep 10
done
# The Gatekeeper verdict a downloader's Mac will reach.
spctl -a -t open --context context:primary-signature -v "$DMG"
fi
echo "Built: $DMG"