Branchwell

Jobley / app · jobley.ai / scripts / release-mac.sh

Raw 11.7 KB on a698b1d1990e3dd0f04a257534b232ac0b1cf6c7
1#!/bin/zsh
2# Build, sign, notarize and publish a Jobley 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 jobley-site main, pushes, and waits for jobley.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="${JOBLEY_SITE_DIR:-$root/../jobley-site}"
28[[ -e "$site/.git" ]] || { echo "No site repo at $site. Clone protomota/jobley-site there or set JOBLEY_SITE_DIR." >&2; exit 1; }
29team="22HV69L32T"
30bundle_id="com.protomota.jobley"
31site_url="https://jobley.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="${JOBLEY_RELEASE_DIR:-/Volumes/Expand/Projects/Jobley/release}"
38if [[ -z "${JOBLEY_RELEASE_DIR:-}" && ! -d /Volumes/Expand ]]; then
39 echo "The Expand drive is not mounted. Mount it or set JOBLEY_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: JOBLEY_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# The Developer ID provisioning profile named below (checked in under signing/) signs the release.
89profile_name="Jobley Developer ID"
90profile_dir="$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles"
91has_profile() {
92 local file
93 for file in "$profile_dir"/*.provisionprofile(N) "$HOME/Library/MobileDevice/Provisioning Profiles"/*.provisionprofile(N); do
94 local decoded="$(security cms -D -i "$file" 2>/dev/null)"
95 grep -A1 '<key>Name</key>' <<< "$decoded" | grep -q ">$profile_name<" && return 0
96 done
97 return 1
98}
99if ! has_profile && [[ -f "$root/signing/Jobley_Developer_ID.provisionprofile" ]]; then
100 mkdir -p "$profile_dir"
101 cp "$root/signing/Jobley_Developer_ID.provisionprofile" "$profile_dir/"
102fi
103signing=(CODE_SIGN_STYLE=Manual "CODE_SIGN_IDENTITY=Developer ID Application" "DEVELOPMENT_TEAM=$team")
104export_profiles=""
105if has_profile; then
106 signing+=("JOBLEY_RELEASE_PROFILE=$profile_name")
107 export_profiles="<key>provisioningProfiles</key><dict><key>$bundle_id</key><string>$profile_name</string></dict>"
108else
109 echo "No \"$profile_name\" provisioning profile is installed or in signing/; stopping." >&2
110 exit 1
111fi
112
113step "Generate the Xcode project"
114( cd "$root" && xcodegen generate --quiet )
115
116step "Archive Jobley $version ($build)"
117archive="$out/Jobley.xcarchive"
118xcodebuild archive -project "$root/Jobley.xcodeproj" -scheme JobleyMac -configuration Release \
119 -destination 'generic/platform=macOS' -archivePath "$archive" -derivedDataPath "$derived" \
120 "${signing[@]}" MARKETING_VERSION="$version" CURRENT_PROJECT_VERSION="$build" > "$out/archive.log" 2>&1 ||
121 { grep -E "error:|warning: .*provision" "$out/archive.log" | tail -20 >&2; exit 1; }
122
123step "Export with Developer ID"
124cat > "$out/ExportOptions.plist" <<PLIST
125<?xml version="1.0" encoding="UTF-8"?>
126<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
127<plist version="1.0"><dict>
128 <key>method</key><string>developer-id</string>
129 <key>teamID</key><string>$team</string>
130 <key>signingStyle</key><string>manual</string>
131 <key>signingCertificate</key><string>Developer ID Application</string>
132 $export_profiles
133</dict></plist>
134PLIST
135xcodebuild -exportArchive -archivePath "$archive" -exportPath "$out/export" \
136 -exportOptionsPlist "$out/ExportOptions.plist" \
137 > "$out/export.log" 2>&1 || { grep -v IDEDistribution "$out/export.log" | tail -20 >&2; exit 1; }
138app="$out/export/Jobley.app"
139codesign --verify --deep --strict --verbose=2 "$app"
140# Capture each signature first: with pipefail, grep -q closing the pipe early looks like a failure.
141signature="$(codesign -dv --verbose=4 "$app" 2>&1)"
142grep -q "Authority=Developer ID Application: Protomota, LLC ($team)" <<< "$signature" ||
143 { echo "The exported app is not signed with the Protomota Developer ID." >&2; exit 1; }
144entitlements="$(codesign -d --entitlements - --xml "$app" 2>/dev/null)"
145grep -q "com.apple.security.network.client" <<< "$entitlements" ||
146 { echo "The exported app cannot reach the update feed (no network entitlement)." >&2; exit 1; }
147for helper in Autoupdate Updater.app XPCServices/Installer.xpc XPCServices/Downloader.xpc; do
148 signature="$(codesign -dv "$app/Contents/Frameworks/Sparkle.framework/Versions/B/$helper" 2>&1)"
149 grep -q "TeamIdentifier=$team" <<< "$signature" ||
150 { echo "Sparkle's $helper is not signed with the Developer ID." >&2; exit 1; }
151done
152
153step "Notarize the app"
154ditto -c -k --keepParent "$app" "$out/Jobley.zip"
155notarize "$out/Jobley.zip"
156xcrun stapler staple "$app"
157spctl --assess --type execute --verbose=2 "$app"
158
159# --- Installer ---------------------------------------------------------------------
160step "Build the installer"
161dmg="$out/Jobley-$version.dmg"
162# The shared Protomota Finder layout (installer/), built with dmgbuild. The packaging tools live in
163# their own Python environment, made on first use.
164packaging_python="${JOBLEY_PACKAGING_PYTHON:-$work/installer-tools/bin/python}"
165if ! "$packaging_python" -c 'import dmgbuild, ds_store, mac_alias' 2>/dev/null; then
166 python3 -m venv "$work/installer-tools"
167 "$work/installer-tools/bin/pip" install --quiet -r "$root/installer/requirements.txt"
168 packaging_python="$work/installer-tools/bin/python"
169fi
170"$packaging_python" "$root/installer/package.py" "$app" "$dmg" > "$out/dmg.log" 2>&1 || { tail -20 "$out/dmg.log" >&2; exit 1; }
171# Sign the installer with the same certificate Xcode used for the app.
172codesign -d --extract-certificates="$secrets/cert" "$app"
173identity="$(openssl x509 -inform DER -in "$secrets/cert0" -noout -fingerprint -sha1 | sed 's/.*=//; s/://g')"
174codesign --force --sign "$identity" --timestamp "$dmg"
175notarize "$dmg"
176xcrun stapler staple "$dmg"
177spctl --assess --type open --context context:primary-signature --verbose=2 "$dmg"
178
179# --- Update feed -------------------------------------------------------------------
180step "Sign the update and write the feed"
181if [[ -n "${JOBLEY_SPARKLE_EDDSA_PRIVATE_KEY:-}" ]]; then
182 ( umask 077; print -rn -- "$JOBLEY_SPARKLE_EDDSA_PRIVATE_KEY" > "$update_key" )
183else
184 "$sparkle/generate_keys" --account "$bundle_id" -x "$update_key" > /dev/null
185fi
186feed="$out/feed"; mkdir "$feed"; cp "$dmg" "$feed/"
187print -r -- "<p>${notes}</p>" > "$feed/Jobley-$version.html"
188"$sparkle/generate_appcast" --ed-key-file "$update_key" --download-url-prefix "$site_url/downloads/" \
189 --link "$site_url" --embed-release-notes -o "$feed/appcast.xml" "$feed"
190grep -q "sparkle:version>$build<" "$feed/appcast.xml" && grep -q 'sparkle:edSignature=' "$feed/appcast.xml" ||
191 { echo "The update feed is missing this build or its signature." >&2; exit 1; }
192
193step "Stage the site files"
194mkdir -p "$site/downloads"
195cp "$dmg" "$site/downloads/"
196cp "$feed/appcast.xml" "$site/appcast.xml"
197sha="$(shasum -a 256 "$dmg" | cut -d' ' -f1)"
198bytes="$(stat -f %z "$dmg")"
199# Older installers stay in downloads/ so the appcast's earlier entries and old links keep working.
200"$site/tools/set-release.sh" "$version" "$build" "$sha" "$bytes"
201sed -i '' -e "s/^\\( MARKETING_VERSION: \\).*/\\1\"$version\"/" \
202 -e "s/^\\( CURRENT_PROJECT_VERSION: \\).*/\\1\"$build\"/" "$config"
203
204echo "\nJobley $version ($build) is signed, notarized and staged."
205echo "Installer: $dmg"
206echo "SHA-256: $sha"
207
208if (( ! publish )); then
209 echo "Review $site, then commit and push both repos to main to publish (or rerun with --publish)."
210 exit 0
211fi
212
213# --- Publish -----------------------------------------------------------------------
214step "Publish to main"
215# Installers live in the R2 bucket jobley-downloads (jobley.ai/downloads/* streams from it); upload before the feed goes live.
216(cd "$site" && npx -y wrangler r2 object put "jobley-downloads/Jobley-$version.dmg" --file "downloads/Jobley-$version.dmg" \
217 --remote --content-type application/x-apple-diskimage)
218git -C "$site" add appcast.xml
219git -C "$site" commit -q -a -m "[jobley-site] release Mac $version ($build)"
220git -C "$site" push origin main
221git -C "$root" add project.yml
222git -C "$root" commit -q -m "[jobley] release Mac $version ($build)"
223git -C "$root" push origin main
224
225step "Wait for $site_url"
226for attempt in {1..40}; do
227 live_feed="$(curl -fsS "$site_url/appcast.xml?build=$build" 2>/dev/null || true)"
228 if grep -q "sparkle:version>$build<" <<< "$live_feed"; then
229 live_sha="$(curl -fsSL "$site_url/downloads/Jobley-$version.dmg" | shasum -a 256 | cut -d' ' -f1)"
230 [[ "$live_sha" == "$sha" ]] || { echo "The live download does not match the signed installer." >&2; exit 1; }
231 echo "Live: $site_url/download offers Jobley $version ($build), byte-identical to the signed installer."
232 exit 0
233 fi
234 sleep 15
235done
236echo "The site did not serve build $build within 10 minutes. Check the Cloudflare Pages deployment." >&2
237exit 1