Branchwell

Bucksley / app · bucksley.ai / scripts / release-mac.sh

Raw 12.2 KB on 07e410940d4dd3ce1e1e51286babef75b272eb4f
1#!/bin/zsh
2# Build, sign, notarize and publish a Bucksley Mac release.
3#
4# 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 bucksley-site main, pushes, and waits for bucksley.ai to serve it.
8# See 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}"
26config="$root/project.yml"
27site="${BUCKSLEY_SITE_DIR:-$root/../bucksley-site}"
28[[ -e "$site/.git" ]] || { echo "No site repo at $site. Clone protomota/bucksley-site there or set BUCKSLEY_SITE_DIR." >&2; exit 1; }
29team="22HV69L32T"
30bundle_id="com.protomota.bucksley"
31site_url="https://bucksley.ai"
32
33setting() { sed -n "s/^ *$1: \"\\{0,1\\}\\([^\"]*\\)\"\\{0,1\\}$/\\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="${BUCKSLEY_RELEASE_DIR:-/Volumes/Expand/Projects/Bucksley/release}"
38if [[ -z "${BUCKSLEY_RELEASE_DIR:-}" && ! -d /Volumes/Expand ]]; then
39 echo "The Expand drive is not mounted. Mount it or set BUCKSLEY_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 for repo in "$root" "$site"; do
50 [[ "$(git -C "$repo" branch --show-current)" == main ]] || { echo "Publish from main ($repo)." >&2; exit 1; }
51 [[ -z "$(git -C "$repo" status --porcelain --untracked-files=no)" ]] || { echo "Commit or stash tracked changes in $repo first." >&2; exit 1; }
52 done
53 git -C "$site" pull -q --ff-only origin main
54fi
55
56step() { print -P "\n%B== $1%b"; }
57
58# --- Credentials -----------------------------------------------------------------
59# Apple notarization key: APPLE_API_KEY_PATH/APPLE_API_KEY_ID/APPLE_API_ISSUER, or Bitwarden Secrets Manager.
60bws_value() {
61 local token="${BWS_ACCESS_TOKEN:-$(launchctl getenv BWS_ACCESS_TOKEN 2>/dev/null || true)}"
62 [[ -n "$token" ]] || return 1
63 BWS_ACCESS_TOKEN="$token" bws secret list --output json 2>/dev/null |
64 /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"
65}
66if [[ -z "${APPLE_API_KEY_PATH:-}" ]]; then
67 APPLE_API_KEY_PATH="$secrets/AuthKey.p8"
68 ( umask 077; bws_value APP_STORE_CONNECT_API_KEY > "$APPLE_API_KEY_PATH" ) ||
69 { echo "No Apple API key. Set APPLE_API_KEY_PATH or add APP_STORE_CONNECT_API_KEY to Secrets Manager." >&2; exit 1; }
70 APPLE_API_KEY_ID="$(bws_value APP_STORE_CONNECT_KEY_ID)"
71 APPLE_API_ISSUER="$(bws_value APP_STORE_CONNECT_ISSUER_ID)"
72fi
73notary=(--key "$APPLE_API_KEY_PATH" --key-id "$APPLE_API_KEY_ID" --issuer "$APPLE_API_ISSUER")
74
75# Sparkle update key: BUCKSLEY_SPARKLE_EDDSA_PRIVATE_KEY, or the login Keychain entry made by generate_keys.
76sparkle="$derived/SourcePackages/artifacts/sparkle/Sparkle/bin"
77update_key="$secrets/sparkle.key"
78
79notarize() {
80 local receipt="$out/${1:t}.notarization.json"
81 xcrun notarytool submit "$1" "${notary[@]}" --wait --output-format json > "$receipt"
82 local notary_status="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["status"])' "$receipt")"
83 echo "Apple notarization: $notary_status"
84 [[ "$notary_status" == Accepted ]] || { echo "Apple did not accept ${1:t}. See $receipt" >&2; exit 1; }
85}
86
87# --- Build and sign ---------------------------------------------------------------
88# iCloud sync needs a Developer ID provisioning profile named below. With it installed (or checked in
89# under signing/) the release carries iCloud. Without it the release is signed with the same
90# Developer ID certificate but leaves iCloud out, and the app says sync is unavailable.
91profile_name="Bucksley Developer ID"
92profile_dir="$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles"
93has_profile() {
94 local file
95 for file in "$profile_dir"/*.provisionprofile(N) "$HOME/Library/MobileDevice/Provisioning Profiles"/*.provisionprofile(N); do
96 local decoded="$(security cms -D -i "$file" 2>/dev/null)"
97 grep -A1 '<key>Name</key>' <<< "$decoded" | grep -q ">$profile_name<" && return 0
98 done
99 return 1
100}
101if ! has_profile && [[ -f "$root/signing/Bucksley_Developer_ID.provisionprofile" ]]; then
102 mkdir -p "$profile_dir"
103 cp "$root/signing/Bucksley_Developer_ID.provisionprofile" "$profile_dir/"
104fi
105signing=(CODE_SIGN_STYLE=Manual "CODE_SIGN_IDENTITY=Developer ID Application" "DEVELOPMENT_TEAM=$team")
106export_profiles=""
107icloud=1
108if has_profile; then
109 signing+=("BUCKSLEY_RELEASE_PROFILE=$profile_name")
110 export_profiles="<key>provisioningProfiles</key><dict><key>$bundle_id</key><string>$profile_name</string></dict>"
111else
112 icloud=0
113 echo "No \"$profile_name\" provisioning profile is installed or in signing/. iCloud needs it; stopping." >&2
114 exit 1
115fi
116
117step "Generate the Xcode project"
118( cd "$root" && xcodegen generate --quiet )
119
120step "Archive Bucksley $version ($build)"
121archive="$out/Bucksley.xcarchive"
122xcodebuild archive -project "$root/Bucksley.xcodeproj" -scheme BucksleyMac -configuration Release \
123 -destination 'generic/platform=macOS' -archivePath "$archive" -derivedDataPath "$derived" \
124 "${signing[@]}" MARKETING_VERSION="$version" CURRENT_PROJECT_VERSION="$build" > "$out/archive.log" 2>&1 ||
125 { grep -E "error:|warning: .*provision" "$out/archive.log" | tail -20 >&2; exit 1; }
126
127step "Export with Developer ID"
128cat > "$out/ExportOptions.plist" <<PLIST
129<?xml version="1.0" encoding="UTF-8"?>
130<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
131<plist version="1.0"><dict>
132 <key>method</key><string>developer-id</string>
133 <key>teamID</key><string>$team</string>
134 <key>signingStyle</key><string>manual</string>
135 <key>signingCertificate</key><string>Developer ID Application</string>
136 $export_profiles
137</dict></plist>
138PLIST
139xcodebuild -exportArchive -archivePath "$archive" -exportPath "$out/export" \
140 -exportOptionsPlist "$out/ExportOptions.plist" \
141 > "$out/export.log" 2>&1 || { grep -v IDEDistribution "$out/export.log" | tail -20 >&2; exit 1; }
142app="$out/export/Bucksley.app"
143codesign --verify --deep --strict --verbose=2 "$app"
144# Capture each signature first: with pipefail, grep -q closing the pipe early looks like a failure.
145signature="$(codesign -dv --verbose=4 "$app" 2>&1)"
146grep -q "Authority=Developer ID Application: Protomota, LLC ($team)" <<< "$signature" ||
147 { echo "The exported app is not signed with the Protomota Developer ID." >&2; exit 1; }
148entitlements="$(codesign -d --entitlements - --xml "$app" 2>/dev/null)"
149if (( icloud )) && ! grep -q "iCloud.$bundle_id" <<< "$entitlements"; then
150 echo "The exported app lost its iCloud entitlement." >&2; exit 1
151fi
152grep -q "com.apple.security.network.client" <<< "$entitlements" ||
153 { echo "The exported app cannot reach the update feed (no network entitlement)." >&2; exit 1; }
154for helper in Autoupdate Updater.app XPCServices/Installer.xpc XPCServices/Downloader.xpc; do
155 signature="$(codesign -dv "$app/Contents/Frameworks/Sparkle.framework/Versions/B/$helper" 2>&1)"
156 grep -q "TeamIdentifier=$team" <<< "$signature" ||
157 { echo "Sparkle's $helper is not signed with the Developer ID." >&2; exit 1; }
158done
159
160step "Notarize the app"
161ditto -c -k --keepParent "$app" "$out/Bucksley.zip"
162notarize "$out/Bucksley.zip"
163xcrun stapler staple "$app"
164spctl --assess --type execute --verbose=2 "$app"
165
166# --- Installer ---------------------------------------------------------------------
167step "Build the installer"
168dmg="$out/Bucksley-$version.dmg"
169# The shared Protomota Finder layout (installer/), built with dmgbuild. The packaging tools live in
170# their own Python environment, made on first use.
171packaging_python="${BUCKSLEY_PACKAGING_PYTHON:-$work/installer-tools/bin/python}"
172if ! "$packaging_python" -c 'import dmgbuild, ds_store, mac_alias' 2>/dev/null; then
173 python3 -m venv "$work/installer-tools"
174 "$work/installer-tools/bin/pip" install --quiet -r "$root/installer/requirements.txt"
175 packaging_python="$work/installer-tools/bin/python"
176fi
177"$packaging_python" "$root/installer/package.py" "$app" "$dmg" > "$out/dmg.log" 2>&1 || { tail -20 "$out/dmg.log" >&2; exit 1; }
178# Sign the installer with the same certificate Xcode used for the app.
179codesign -d --extract-certificates="$secrets/cert" "$app"
180identity="$(openssl x509 -inform DER -in "$secrets/cert0" -noout -fingerprint -sha1 | sed 's/.*=//; s/://g')"
181codesign --force --sign "$identity" --timestamp "$dmg"
182notarize "$dmg"
183xcrun stapler staple "$dmg"
184spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg"
185
186# --- Update feed -------------------------------------------------------------------
187step "Sign the update and write the feed"
188if [[ -n "${BUCKSLEY_SPARKLE_EDDSA_PRIVATE_KEY:-}" ]]; then
189 ( umask 077; print -rn -- "$BUCKSLEY_SPARKLE_EDDSA_PRIVATE_KEY" > "$update_key" )
190else
191 "$sparkle/generate_keys" --account "$bundle_id" -x "$update_key" > /dev/null
192fi
193feed="$out/feed"; mkdir "$feed"; cp "$dmg" "$feed/"
194print -r -- "<p>${notes}</p>" > "$feed/Bucksley-$version.html"
195"$sparkle/generate_appcast" --ed-key-file "$update_key" --download-url-prefix "$site_url/downloads/" \
196 --link "$site_url" --embed-release-notes -o "$feed/appcast.xml" "$feed"
197grep -q "sparkle:version>$build<" "$feed/appcast.xml" && grep -q 'sparkle:edSignature=' "$feed/appcast.xml" ||
198 { echo "The update feed is missing this build or its signature." >&2; exit 1; }
199
200step "Stage the site files"
201mkdir -p "$site/downloads"
202cp "$dmg" "$site/downloads/"
203cp "$feed/appcast.xml" "$site/appcast.xml"
204sha="$(shasum -a 256 "$dmg" | cut -d' ' -f1)"
205bytes="$(stat -f %z "$dmg")"
206# Older installers stay in downloads/ so the appcast's earlier entries and old links keep working.
207"$site/tools/set-release.sh" "$version" "$build" "$sha" "$bytes"
208sed -i '' -e "s/^\\( MARKETING_VERSION: \\).*/\\1\"$version\"/" \
209 -e "s/^\\( CURRENT_PROJECT_VERSION: \\).*/\\1\"$build\"/" "$config"
210
211echo "\nBucksley $version ($build) is signed, notarized and staged."
212echo "Installer: $dmg"
213echo "SHA-256: $sha"
214
215if (( ! publish )); then
216 echo "Review $site, then commit and push both repos to main to publish (or rerun with --publish)."
217 exit 0
218fi
219
220# --- Publish -----------------------------------------------------------------------
221step "Publish to main"
222# Installers live in the R2 bucket bucksley-downloads (bucksley.ai/downloads/* streams from it); upload before the feed goes live.
223(cd "$site" && npx -y wrangler r2 object put "bucksley-downloads/Bucksley-$version.dmg" --file "downloads/Bucksley-$version.dmg" \
224 --remote --content-type application/x-apple-diskimage)
225git -C "$site" add appcast.xml
226git -C "$site" commit -q -a -m "[bucksley-site] release Mac $version ($build)"
227git -C "$site" push origin main
228git -C "$root" add project.yml
229git -C "$root" commit -q -m "[bucksley] release Mac $version ($build)"
230git -C "$root" push origin main
231
232step "Wait for $site_url"
233for attempt in {1..40}; do
234 live_feed="$(curl -fsS "$site_url/appcast.xml?build=$build" 2>/dev/null || true)"
235 if grep -q "sparkle:version>$build<" <<< "$live_feed"; then
236 live_sha="$(curl -fsSL "$site_url/downloads/Bucksley-$version.dmg" | shasum -a 256 | cut -d' ' -f1)"
237 [[ "$live_sha" == "$sha" ]] || { echo "The live download does not match the signed installer." >&2; exit 1; }
238 echo "Live: $site_url/download offers Bucksley $version ($build), byte-identical to the signed installer."
239 exit 0
240 fi
241 sleep 15
242done
243echo "The site did not serve build $build within 10 minutes. Check the Cloudflare Pages deployment." >&2
244exit 1