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. |
| 9 | set -euo pipefail |
| 10 | |
| 11 | usage() { sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 2; } |
| 12 | |
| 13 | version="" notes="Bug fixes and improvements." build="" publish=0 |
| 14 | while (( $# )); 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 |
| 22 | done |
| 23 | [[ "$version" =~ '^[0-9]+(\.[0-9]+){1,2}$' ]] || usage |
| 24 | |
| 25 | root="${0:A:h:h:h}" |
| 26 | native="$root/native" |
| 27 | config="$native/Glimmer.xcconfig" |
| 28 | site="$root/landing" |
| 29 | team="22HV69L32T" |
| 30 | bundle_id="com.protomota.glimmer" |
| 31 | site_url="https://glimmertoday.ai" |
| 32 | |
| 33 | setting() { sed -n "s/^$1 = //p" "$config" | head -1; } |
| 34 | if [[ -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 | |
| 37 | work="${GLIMMER_RELEASE_DIR:-/Volumes/Expand/Projects/Glimmer/glimmer-release}" |
| 38 | if [[ -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 |
| 40 | fi |
| 41 | out="$work/$version-$build" |
| 42 | derived="$work/derived" |
| 43 | [[ ! -e "$out" ]] || { echo "$out already exists. Choose a new build number or remove it." >&2; exit 1; } |
| 44 | mkdir -p "$out" |
| 45 | secrets="$(mktemp -d)"; chmod 700 "$secrets" |
| 46 | trap 'rm -rf "$secrets"' EXIT |
| 47 | |
| 48 | if (( 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; } |
| 51 | fi |
| 52 | |
| 53 | step() { 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. |
| 57 | bws_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 | } |
| 63 | if [[ -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)" |
| 69 | fi |
| 70 | notary=(--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. |
| 73 | sparkle="$derived/SourcePackages/artifacts/sparkle/Sparkle/bin" |
| 74 | update_key="$secrets/sparkle.key" |
| 75 | |
| 76 | notarize() { |
| 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. |
| 88 | profile_name="Glimmer Developer ID" |
| 89 | profile_dir="$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles" |
| 90 | has_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 | } |
| 98 | if ! 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/" |
| 101 | fi |
| 102 | signing=(CODE_SIGN_STYLE=Manual "CODE_SIGN_IDENTITY=Developer ID Application" "DEVELOPMENT_TEAM=$team") |
| 103 | export_profiles="" |
| 104 | icloud=1 |
| 105 | if 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>" |
| 108 | else |
| 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." |
| 112 | fi |
| 113 | |
| 114 | step "Archive Glimmer $version ($build)" |
| 115 | archive="$out/Glimmer.xcarchive" |
| 116 | xcodebuild 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 | |
| 121 | step "Export with Developer ID" |
| 122 | cat > "$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> |
| 132 | PLIST |
| 133 | xcodebuild -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; } |
| 136 | app="$out/export/Glimmer.app" |
| 137 | codesign --verify --deep --strict --verbose=2 "$app" |
| 138 | # Capture each signature first: with pipefail, grep -q closing the pipe early looks like a failure. |
| 139 | signature="$(codesign -dv --verbose=4 "$app" 2>&1)" |
| 140 | grep -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; } |
| 142 | entitlements="$(codesign -d --entitlements - --xml "$app" 2>/dev/null)" |
| 143 | if (( icloud )) && ! grep -q "iCloud.$bundle_id" <<< "$entitlements"; then |
| 144 | echo "The exported app lost its iCloud entitlement." >&2; exit 1 |
| 145 | fi |
| 146 | grep -q "com.apple.security.network.client" <<< "$entitlements" || |
| 147 | { echo "The exported app cannot reach the update feed (no network entitlement)." >&2; exit 1; } |
| 148 | for 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; } |
| 152 | done |
| 153 | |
| 154 | step "Notarize the app" |
| 155 | ditto -c -k --keepParent "$app" "$out/Glimmer.zip" |
| 156 | notarize "$out/Glimmer.zip" |
| 157 | xcrun stapler staple "$app" |
| 158 | spctl --assess --type execute --verbose=2 "$app" |
| 159 | |
| 160 | # --- Installer --------------------------------------------------------------------- |
| 161 | step "Build the installer" |
| 162 | dmg="$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. |
| 165 | packaging_python="${GLIMMER_PACKAGING_PYTHON:-$work/installer-tools/bin/python}" |
| 166 | if ! "$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" |
| 170 | fi |
| 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. |
| 173 | codesign -d --extract-certificates="$secrets/cert" "$app" |
| 174 | identity="$(openssl x509 -inform DER -in "$secrets/cert0" -noout -fingerprint -sha1 | sed 's/.*=//; s/://g')" |
| 175 | codesign --force --sign "$identity" --timestamp "$dmg" |
| 176 | notarize "$dmg" |
| 177 | xcrun stapler staple "$dmg" |
| 178 | spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg" |
| 179 | |
| 180 | # --- Update feed ------------------------------------------------------------------- |
| 181 | step "Sign the update and write the feed" |
| 182 | if [[ -n "${GLIMMER_SPARKLE_EDDSA_PRIVATE_KEY:-}" ]]; then |
| 183 | ( umask 077; print -rn -- "$GLIMMER_SPARKLE_EDDSA_PRIVATE_KEY" > "$update_key" ) |
| 184 | else |
| 185 | "$sparkle/generate_keys" --account "$bundle_id" -x "$update_key" > /dev/null |
| 186 | fi |
| 187 | feed="$out/feed"; mkdir "$feed"; cp "$dmg" "$feed/" |
| 188 | print -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" |
| 191 | grep -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 | |
| 194 | step "Stage the site files" |
| 195 | mkdir -p "$site/updates" |
| 196 | find "$site/updates" -name 'Glimmer-*.dmg' -delete |
| 197 | cp "$dmg" "$site/updates/" |
| 198 | cp "$feed/appcast.xml" "$site/updates/appcast.xml" |
| 199 | sha="$(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' |
| 202 | import json, os, sys, datetime |
| 203 | latest, version, build, sha, dmg, notes = sys.argv[1:] |
| 204 | json.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) |
| 207 | open(latest, "a").write("\n") |
| 208 | PY |
| 209 | sed -i '' -e "s/^MARKETING_VERSION = .*/MARKETING_VERSION = $version/" \ |
| 210 | -e "s/^CURRENT_PROJECT_VERSION = .*/CURRENT_PROJECT_VERSION = $build/" "$config" |
| 211 | |
| 212 | echo "\nGlimmer $version ($build) is signed, notarized and staged." |
| 213 | echo "Installer: $dmg" |
| 214 | echo "SHA-256: $sha" |
| 215 | |
| 216 | if (( ! publish )); then |
| 217 | echo "Review landing/updates, then commit and push to main to publish (or rerun with --publish)." |
| 218 | exit 0 |
| 219 | fi |
| 220 | |
| 221 | # --- Publish ----------------------------------------------------------------------- |
| 222 | step "Publish to main" |
| 223 | git -C "$root" add landing/updates native/Glimmer.xcconfig |
| 224 | git -C "$root" commit -q -m "[glimmer] release Mac $version ($build)" |
| 225 | git -C "$root" push origin main |
| 226 | |
| 227 | step "Wait for $site_url" |
| 228 | for 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 |
| 237 | done |
| 238 | echo "The site did not serve build $build within 10 minutes. Check the Cloudflare Pages deployment (glimmer-site)." >&2 |
| 239 | exit 1 |