Bucksley / app · bucksley.ai / scripts / release-mac.sh
| 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. |
| 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}" |
| 26 | config="$root/project.yml" |
| 27 | site="${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; } |
| 29 | team="22HV69L32T" |
| 30 | bundle_id="com.protomota.bucksley" |
| 31 | site_url="https://bucksley.ai" |
| 32 | |
| 33 | setting() { sed -n "s/^ *$1: \"\\{0,1\\}\\([^\"]*\\)\"\\{0,1\\}$/\\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="${BUCKSLEY_RELEASE_DIR:-/Volumes/Expand/Projects/Bucksley/release}" |
| 38 | if [[ -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 |
| 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 | 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 |
| 54 | fi |
| 55 | |
| 56 | step() { 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. |
| 60 | bws_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 | } |
| 66 | if [[ -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)" |
| 72 | fi |
| 73 | notary=(--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. |
| 76 | sparkle="$derived/SourcePackages/artifacts/sparkle/Sparkle/bin" |
| 77 | update_key="$secrets/sparkle.key" |
| 78 | |
| 79 | notarize() { |
| 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. |
| 91 | profile_name="Bucksley Developer ID" |
| 92 | profile_dir="$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles" |
| 93 | has_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 | } |
| 101 | if ! 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/" |
| 104 | fi |
| 105 | signing=(CODE_SIGN_STYLE=Manual "CODE_SIGN_IDENTITY=Developer ID Application" "DEVELOPMENT_TEAM=$team") |
| 106 | export_profiles="" |
| 107 | icloud=1 |
| 108 | if 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>" |
| 111 | else |
| 112 | icloud=0 |
| 113 | echo "No \"$profile_name\" provisioning profile is installed or in signing/. iCloud needs it; stopping." >&2 |
| 114 | exit 1 |
| 115 | fi |
| 116 | |
| 117 | step "Generate the Xcode project" |
| 118 | ( cd "$root" && xcodegen generate --quiet ) |
| 119 | |
| 120 | step "Archive Bucksley $version ($build)" |
| 121 | archive="$out/Bucksley.xcarchive" |
| 122 | xcodebuild 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 | |
| 127 | step "Export with Developer ID" |
| 128 | cat > "$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> |
| 138 | PLIST |
| 139 | xcodebuild -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; } |
| 142 | app="$out/export/Bucksley.app" |
| 143 | codesign --verify --deep --strict --verbose=2 "$app" |
| 144 | # Capture each signature first: with pipefail, grep -q closing the pipe early looks like a failure. |
| 145 | signature="$(codesign -dv --verbose=4 "$app" 2>&1)" |
| 146 | grep -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; } |
| 148 | entitlements="$(codesign -d --entitlements - --xml "$app" 2>/dev/null)" |
| 149 | if (( icloud )) && ! grep -q "iCloud.$bundle_id" <<< "$entitlements"; then |
| 150 | echo "The exported app lost its iCloud entitlement." >&2; exit 1 |
| 151 | fi |
| 152 | grep -q "com.apple.security.network.client" <<< "$entitlements" || |
| 153 | { echo "The exported app cannot reach the update feed (no network entitlement)." >&2; exit 1; } |
| 154 | for 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; } |
| 158 | done |
| 159 | |
| 160 | step "Notarize the app" |
| 161 | ditto -c -k --keepParent "$app" "$out/Bucksley.zip" |
| 162 | notarize "$out/Bucksley.zip" |
| 163 | xcrun stapler staple "$app" |
| 164 | spctl --assess --type execute --verbose=2 "$app" |
| 165 | |
| 166 | # --- Installer --------------------------------------------------------------------- |
| 167 | step "Build the installer" |
| 168 | dmg="$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. |
| 171 | packaging_python="${BUCKSLEY_PACKAGING_PYTHON:-$work/installer-tools/bin/python}" |
| 172 | if ! "$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" |
| 176 | fi |
| 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. |
| 179 | codesign -d --extract-certificates="$secrets/cert" "$app" |
| 180 | identity="$(openssl x509 -inform DER -in "$secrets/cert0" -noout -fingerprint -sha1 | sed 's/.*=//; s/://g')" |
| 181 | codesign --force --sign "$identity" --timestamp "$dmg" |
| 182 | notarize "$dmg" |
| 183 | xcrun stapler staple "$dmg" |
| 184 | spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg" |
| 185 | |
| 186 | # --- Update feed ------------------------------------------------------------------- |
| 187 | step "Sign the update and write the feed" |
| 188 | if [[ -n "${BUCKSLEY_SPARKLE_EDDSA_PRIVATE_KEY:-}" ]]; then |
| 189 | ( umask 077; print -rn -- "$BUCKSLEY_SPARKLE_EDDSA_PRIVATE_KEY" > "$update_key" ) |
| 190 | else |
| 191 | "$sparkle/generate_keys" --account "$bundle_id" -x "$update_key" > /dev/null |
| 192 | fi |
| 193 | feed="$out/feed"; mkdir "$feed"; cp "$dmg" "$feed/" |
| 194 | print -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" |
| 197 | grep -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 | |
| 200 | step "Stage the site files" |
| 201 | mkdir -p "$site/downloads" |
| 202 | cp "$dmg" "$site/downloads/" |
| 203 | cp "$feed/appcast.xml" "$site/appcast.xml" |
| 204 | sha="$(shasum -a 256 "$dmg" | cut -d' ' -f1)" |
| 205 | bytes="$(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" |
| 208 | sed -i '' -e "s/^\\( MARKETING_VERSION: \\).*/\\1\"$version\"/" \ |
| 209 | -e "s/^\\( CURRENT_PROJECT_VERSION: \\).*/\\1\"$build\"/" "$config" |
| 210 | |
| 211 | echo "\nBucksley $version ($build) is signed, notarized and staged." |
| 212 | echo "Installer: $dmg" |
| 213 | echo "SHA-256: $sha" |
| 214 | |
| 215 | if (( ! publish )); then |
| 216 | echo "Review $site, then commit and push both repos to main to publish (or rerun with --publish)." |
| 217 | exit 0 |
| 218 | fi |
| 219 | |
| 220 | # --- Publish ----------------------------------------------------------------------- |
| 221 | step "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) |
| 225 | git -C "$site" add appcast.xml |
| 226 | git -C "$site" commit -q -a -m "[bucksley-site] release Mac $version ($build)" |
| 227 | git -C "$site" push origin main |
| 228 | git -C "$root" add project.yml |
| 229 | git -C "$root" commit -q -m "[bucksley] release Mac $version ($build)" |
| 230 | git -C "$root" push origin main |
| 231 | |
| 232 | step "Wait for $site_url" |
| 233 | for 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 |
| 242 | done |
| 243 | echo "The site did not serve build $build within 10 minutes. Check the Cloudflare Pages deployment." >&2 |
| 244 | exit 1 |