Branchwell

Glimmer / app / native / scripts / release-mac.sh

1#!/bin/zsh
2# Build, sign, notarize and publish a Glimmer Mac release.
3#
4# native/scripts/release-mac.sh <version> [--notes "What changed"] [--build N] [--publish]
5#
6# Without --publish the script stops after staging the site files so you can review them.
7# With --publish it commits the release to main, pushes, and waits for glimmertoday.ai to serve it.
8# See native/RELEASING.md for the credentials it expects.
9set -euo pipefail
10
11usage() { sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 2; }
12
13version="" notes="Bug fixes and improvements." build="" publish=0
14while (( $# )); do
15 case "$1" in
16 --notes) notes="$2"; shift 2 ;;
17 --build) build="$2"; shift 2 ;;
18 --publish) publish=1; shift ;;
19 -h|--help) usage ;;
20 *) [[ -z "$version" ]] && version="$1" && shift || usage ;;
21 esac
22done
23[[ "$version" =~ '^[0-9]+(\.[0-9]+){1,2}$' ]] || usage
24
25root="${0:A:h:h:h}"
26native="$root/native"
27config="$native/Glimmer.xcconfig"
28site="$root/landing"
29team="22HV69L32T"
30bundle_id="com.protomota.glimmer"
31site_url="https://glimmertoday.ai"
32
33setting() { sed -n "s/^$1 = //p" "$config" | head -1; }
34if [[ -z "$build" ]]; then build=$(( $(setting CURRENT_PROJECT_VERSION) + 1 )); fi
35[[ "$build" =~ '^[0-9]+$' ]] || { echo "The build number must be a whole number." >&2; exit 2; }
36
37work="${GLIMMER_RELEASE_DIR:-/Volumes/Expand/Projects/Glimmer/glimmer-release}"
38if [[ -z "${GLIMMER_RELEASE_DIR:-}" && ! -d /Volumes/Expand ]]; then
39 echo "The Expand drive is not mounted. Mount it or set GLIMMER_RELEASE_DIR." >&2; exit 1
40fi
41out="$work/$version-$build"
42derived="$work/derived"
43[[ ! -e "$out" ]] || { echo "$out already exists. Choose a new build number or remove it." >&2; exit 1; }
44mkdir -p "$out"
45secrets="$(mktemp -d)"; chmod 700 "$secrets"
46trap 'rm -rf "$secrets"' EXIT
47
48if (( publish )); then
49 [[ "$(git -C "$root" branch --show-current)" == main ]] || { echo "Publish from main." >&2; exit 1; }
50 [[ -z "$(git -C "$root" status --porcelain --untracked-files=no)" ]] || { echo "Commit or stash tracked changes first." >&2; exit 1; }
51fi
52
53step() { print -P "\n%B== $1%b"; }
54
55# --- Credentials -----------------------------------------------------------------
56# Apple notarization key: APPLE_API_KEY_PATH/APPLE_API_KEY_ID/APPLE_API_ISSUER, or Bitwarden Secrets Manager.
57bws_value() {
58 local token="${BWS_ACCESS_TOKEN:-$(launchctl getenv BWS_ACCESS_TOKEN 2>/dev/null || true)}"
59 [[ -n "$token" ]] || return 1
60 BWS_ACCESS_TOKEN="$token" bws secret list --output json 2>/dev/null |
61 /usr/bin/python3 -c 'import json,sys; print(next(s["value"] for s in json.load(sys.stdin) if s["key"]==sys.argv[1]), end="")' "$1"
62}
63if [[ -z "${APPLE_API_KEY_PATH:-}" ]]; then
64 APPLE_API_KEY_PATH="$secrets/AuthKey.p8"
65 ( umask 077; bws_value APP_STORE_CONNECT_API_KEY > "$APPLE_API_KEY_PATH" ) ||
66 { echo "No Apple API key. Set APPLE_API_KEY_PATH or add APP_STORE_CONNECT_API_KEY to Secrets Manager." >&2; exit 1; }
67 APPLE_API_KEY_ID="$(bws_value APP_STORE_CONNECT_KEY_ID)"
68 APPLE_API_ISSUER="$(bws_value APP_STORE_CONNECT_ISSUER_ID)"
69fi
70notary=(--key "$APPLE_API_KEY_PATH" --key-id "$APPLE_API_KEY_ID" --issuer "$APPLE_API_ISSUER")
71
72# Sparkle update key: GLIMMER_SPARKLE_EDDSA_PRIVATE_KEY, or the login Keychain entry made by generate_keys.
73sparkle="$derived/SourcePackages/artifacts/sparkle/Sparkle/bin"
74update_key="$secrets/sparkle.key"
75
76notarize() {
77 local receipt="$out/${1:t}.notarization.json"
78 xcrun notarytool submit "$1" "${notary[@]}" --wait --output-format json > "$receipt"
79 local notary_status="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["status"])' "$receipt")"
80 echo "Apple notarization: $notary_status"
81 [[ "$notary_status" == Accepted ]] || { echo "Apple did not accept ${1:t}. See $receipt" >&2; exit 1; }
82}
83
84# --- Build and sign ---------------------------------------------------------------
85# iCloud sync needs a Developer ID provisioning profile named below. With it installed (or checked in
86# under native/signing) the release carries iCloud. Without it the release is signed with the same
87# Developer ID certificate but leaves iCloud out, and the app says sync is unavailable.
88profile_name="Glimmer Developer ID"
89profile_dir="$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles"
90has_profile() {
91 local file
92 for file in "$profile_dir"/*.provisionprofile(N) "$HOME/Library/MobileDevice/Provisioning Profiles"/*.provisionprofile(N); do
93 local decoded="$(security cms -D -i "$file" 2>/dev/null)"
94 grep -A1 '<key>Name</key>' <<< "$decoded" | grep -q ">$profile_name<" && return 0
95 done
96 return 1
97}
98if ! has_profile && [[ -f "$native/signing/Glimmer_Developer_ID.provisionprofile" ]]; then
99 mkdir -p "$profile_dir"
100 cp "$native/signing/Glimmer_Developer_ID.provisionprofile" "$profile_dir/"
101fi
102signing=(CODE_SIGN_STYLE=Manual "CODE_SIGN_IDENTITY=Developer ID Application" "DEVELOPMENT_TEAM=$team")
103export_profiles=""
104icloud=1
105if has_profile; then
106 signing+=("GLIMMER_RELEASE_PROFILE=$profile_name")
107 export_profiles="<key>provisioningProfiles</key><dict><key>$bundle_id</key><string>$profile_name</string></dict>"
108else
109 icloud=0
110 signing+=("GLIMMER_MAC_ENTITLEMENTS=Glimmer/Glimmer-Mac-Direct.entitlements")
111 echo "No \"$profile_name\" provisioning profile is installed. This release will not include iCloud sync."
112fi
113
114step "Archive Glimmer $version ($build)"
115archive="$out/Glimmer.xcarchive"
116xcodebuild archive -project "$native/Glimmer.xcodeproj" -scheme Glimmer -configuration Release \
117 -destination 'generic/platform=macOS' -archivePath "$archive" -derivedDataPath "$derived" \
118 "${signing[@]}" MARKETING_VERSION="$version" CURRENT_PROJECT_VERSION="$build" > "$out/archive.log" 2>&1 ||
119 { grep -E "error:|warning: .*provision" "$out/archive.log" | tail -20 >&2; exit 1; }
120
121step "Export with Developer ID"
122cat > "$out/ExportOptions.plist" <<PLIST
123<?xml version="1.0" encoding="UTF-8"?>
124<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
125<plist version="1.0"><dict>
126 <key>method</key><string>developer-id</string>
127 <key>teamID</key><string>$team</string>
128 <key>signingStyle</key><string>manual</string>
129 <key>signingCertificate</key><string>Developer ID Application</string>
130 $export_profiles
131</dict></plist>
132PLIST
133xcodebuild -exportArchive -archivePath "$archive" -exportPath "$out/export" \
134 -exportOptionsPlist "$out/ExportOptions.plist" \
135 > "$out/export.log" 2>&1 || { grep -v IDEDistribution "$out/export.log" | tail -20 >&2; exit 1; }
136app="$out/export/Glimmer.app"
137codesign --verify --deep --strict --verbose=2 "$app"
138# Capture each signature first: with pipefail, grep -q closing the pipe early looks like a failure.
139signature="$(codesign -dv --verbose=4 "$app" 2>&1)"
140grep -q "Authority=Developer ID Application: Protomota, LLC ($team)" <<< "$signature" ||
141 { echo "The exported app is not signed with the Protomota Developer ID." >&2; exit 1; }
142entitlements="$(codesign -d --entitlements - --xml "$app" 2>/dev/null)"
143if (( icloud )) && ! grep -q "iCloud.$bundle_id" <<< "$entitlements"; then
144 echo "The exported app lost its iCloud entitlement." >&2; exit 1
145fi
146grep -q "com.apple.security.network.client" <<< "$entitlements" ||
147 { echo "The exported app cannot reach the update feed (no network entitlement)." >&2; exit 1; }
148for helper in Autoupdate Updater.app XPCServices/Installer.xpc XPCServices/Downloader.xpc; do
149 signature="$(codesign -dv "$app/Contents/Frameworks/Sparkle.framework/Versions/B/$helper" 2>&1)"
150 grep -q "TeamIdentifier=$team" <<< "$signature" ||
151 { echo "Sparkle's $helper is not signed with the Developer ID." >&2; exit 1; }
152done
153
154step "Notarize the app"
155ditto -c -k --keepParent "$app" "$out/Glimmer.zip"
156notarize "$out/Glimmer.zip"
157xcrun stapler staple "$app"
158spctl --assess --type execute --verbose=2 "$app"
159
160# --- Installer ---------------------------------------------------------------------
161step "Build the installer"
162dmg="$out/Glimmer-$version.dmg"
163# The shared Protomota Finder layout (native/installer), built with dmgbuild. The packaging tools live in
164# their own Python environment, made on first use.
165packaging_python="${GLIMMER_PACKAGING_PYTHON:-$work/installer-tools/bin/python}"
166if ! "$packaging_python" -c 'import dmgbuild, ds_store, mac_alias' 2>/dev/null; then
167 python3 -m venv "$work/installer-tools"
168 "$work/installer-tools/bin/pip" install --quiet -r "$native/installer/requirements.txt"
169 packaging_python="$work/installer-tools/bin/python"
170fi
171"$packaging_python" "$native/installer/package.py" "$app" "$dmg" > "$out/dmg.log" 2>&1 || { tail -20 "$out/dmg.log" >&2; exit 1; }
172# Sign the installer with the same certificate Xcode used for the app.
173codesign -d --extract-certificates="$secrets/cert" "$app"
174identity="$(openssl x509 -inform DER -in "$secrets/cert0" -noout -fingerprint -sha1 | sed 's/.*=//; s/://g')"
175codesign --force --sign "$identity" --timestamp "$dmg"
176notarize "$dmg"
177xcrun stapler staple "$dmg"
178spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg"
179
180# --- Update feed -------------------------------------------------------------------
181step "Sign the update and write the feed"
182if [[ -n "${GLIMMER_SPARKLE_EDDSA_PRIVATE_KEY:-}" ]]; then
183 ( umask 077; print -rn -- "$GLIMMER_SPARKLE_EDDSA_PRIVATE_KEY" > "$update_key" )
184else
185 "$sparkle/generate_keys" --account "$bundle_id" -x "$update_key" > /dev/null
186fi
187feed="$out/feed"; mkdir "$feed"; cp "$dmg" "$feed/"
188print -r -- "<p>${notes}</p>" > "$feed/Glimmer-$version.html"
189"$sparkle/generate_appcast" --ed-key-file "$update_key" --download-url-prefix "$site_url/updates/" \
190 --link "$site_url" --embed-release-notes -o "$feed/appcast.xml" "$feed"
191grep -q "sparkle:version>$build<" "$feed/appcast.xml" && grep -q 'sparkle:edSignature=' "$feed/appcast.xml" ||
192 { echo "The update feed is missing this build or its signature." >&2; exit 1; }
193
194step "Stage the site files"
195mkdir -p "$site/updates"
196find "$site/updates" -name 'Glimmer-*.dmg' -delete
197cp "$dmg" "$site/updates/"
198cp "$feed/appcast.xml" "$site/updates/appcast.xml"
199sha="$(shasum -a 256 "$dmg" | cut -d' ' -f1)"
200# The site build reads latest.json, so the download page and buttons pick up this release when it deploys.
201/usr/bin/python3 - "$site/updates/latest.json" "$version" "$build" "$sha" "$dmg" "$notes" <<'PY'
202import json, os, sys, datetime
203latest, version, build, sha, dmg, notes = sys.argv[1:]
204json.dump({"version": version, "build": int(build), "file": f"/updates/{os.path.basename(dmg)}", "bytes": os.path.getsize(dmg),
205 "sha256": sha, "released": datetime.date.today().isoformat(), "minimumSystem": "macOS 15", "notes": notes},
206 open(latest, "w"), indent=2)
207open(latest, "a").write("\n")
208PY
209sed -i '' -e "s/^MARKETING_VERSION = .*/MARKETING_VERSION = $version/" \
210 -e "s/^CURRENT_PROJECT_VERSION = .*/CURRENT_PROJECT_VERSION = $build/" "$config"
211
212echo "\nGlimmer $version ($build) is signed, notarized and staged."
213echo "Installer: $dmg"
214echo "SHA-256: $sha"
215
216if (( ! publish )); then
217 echo "Review landing/updates, then commit and push to main to publish (or rerun with --publish)."
218 exit 0
219fi
220
221# --- Publish -----------------------------------------------------------------------
222step "Publish to main"
223git -C "$root" add landing/updates native/Glimmer.xcconfig
224git -C "$root" commit -q -m "[glimmer] release Mac $version ($build)"
225git -C "$root" push origin main
226
227step "Wait for $site_url"
228for attempt in {1..40}; do
229 live_feed="$(curl -fsS "$site_url/updates/appcast.xml?build=$build" 2>/dev/null || true)"
230 if grep -q "sparkle:version>$build<" <<< "$live_feed"; then
231 live_sha="$(curl -fsSL "$site_url/updates/Glimmer-$version.dmg" | shasum -a 256 | cut -d' ' -f1)"
232 [[ "$live_sha" == "$sha" ]] || { echo "The live download does not match the signed installer." >&2; exit 1; }
233 echo "Live: $site_url/download offers Glimmer $version ($build), byte-identical to the signed installer."
234 exit 0
235 fi
236 sleep 15
237done
238echo "The site did not serve build $build within 10 minutes. Check the Cloudflare Pages deployment (glimmer-site)." >&2
239exit 1