Branchwell

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

Raw 5.2 KB on 66c544d8e0357c5df9f3df1c7ec9fc4a47431c36
1#!/bin/zsh
2# Build Bucksley for iPhone and upload it to TestFlight.
3#
4# scripts/release-ios.sh [--build N]
5#
6# Uses the version in project.yml. The build number defaults to one more than the newest
7# build App Store Connect has for that version. See RELEASING.md for what it expects.
8set -euo pipefail
9
10build=""
11while (( $# )); do
12 case "$1" in
13 --build) build="$2"; shift 2 ;;
14 *) sed -n '2,7p' "$0" | sed 's/^# \{0,1\}//'; exit 2 ;;
15 esac
16done
17
18root="${0:A:h:h}"
19team="22HV69L32T"
20bundle_id="com.protomota.bucksley"
21profile_name="Bucksley App Store"
22version="$(sed -n 's/^ MARKETING_VERSION: "\(.*\)"$/\1/p' "$root/project.yml" | head -1)"
23
24work="${BUCKSLEY_RELEASE_DIR:-/Volumes/Expand/Projects/Bucksley/release}"
25if [[ -z "${BUCKSLEY_RELEASE_DIR:-}" && ! -d /Volumes/Expand ]]; then
26 echo "The Expand drive is not mounted. Mount it or set BUCKSLEY_RELEASE_DIR." >&2; exit 1
27fi
28secrets="$(mktemp -d)"; chmod 700 "$secrets"
29trap 'rm -rf "$secrets"' EXIT
30step() { print -P "\n%B== $1%b"; }
31
32# --- Credentials: the team's App Store Connect upload key, from the environment or Bitwarden Secrets
33# Manager. The default prefix is the same upload credential Jotley's release script uses.
34bws_value() {
35 local token="${BWS_ACCESS_TOKEN:-$(launchctl getenv BWS_ACCESS_TOKEN 2>/dev/null || true)}"
36 [[ -n "$token" ]] || return 1
37 BWS_ACCESS_TOKEN="$token" bws secret list --output json 2>/dev/null |
38 /usr/bin/python3 -c 'import json,sys; print(next(s["value"] for s in json.load(sys.stdin) if s["key"]==sys.argv[1]).replace("\\n", "\n"), end="")' "$1"
39}
40if [[ -z "${APPLE_API_KEY_ID:-}" ]]; then
41 prefix="${BUCKSLEY_ASC_SECRET_PREFIX:-INKSPRING_ASC_UPLOAD}"
42 APPLE_API_KEY_ID="$(bws_value "${prefix}_KEY_ID")"
43 APPLE_API_ISSUER="$(bws_value "${prefix}_ISSUER_ID")"
44 ( umask 077; bws_value "${prefix}_PRIVATE_KEY" > "$secrets/AuthKey_$APPLE_API_KEY_ID.p8"; echo >> "$secrets/AuthKey_$APPLE_API_KEY_ID.p8" )
45else
46 ( umask 077; cp "$APPLE_API_KEY_PATH" "$secrets/AuthKey_$APPLE_API_KEY_ID.p8" )
47fi
48export APPLE_API_KEY_ID APPLE_API_ISSUER
49export APPLE_API_KEY_FILE="$secrets/AuthKey_$APPLE_API_KEY_ID.p8"
50api() { /usr/bin/python3 "$root/scripts/asc.py" "$@"; }
51app_id="$(api app-id "$bundle_id")" # the App Store Connect record for com.protomota.bucksley
52
53if [[ -z "$build" ]]; then
54 build=$(( $(api latest-build "$app_id" "$version") + 1 ))
55fi
56out="$work/ios-$version-$build"
57mkdir -p "$out"
58
59# --- Signing: the App Store profile, installed from signing/ when it is missing.
60profile_dir="$HOME/Library/Developer/Xcode/UserData/Provisioning Profiles"
61if [[ -f "$root/signing/Bucksley_App_Store.mobileprovision" ]]; then
62 mkdir -p "$profile_dir"
63 cp "$root/signing/Bucksley_App_Store.mobileprovision" "$profile_dir/Bucksley_App_Store.mobileprovision"
64fi
65
66if [[ -f "$out/export/Bucksley.ipa" ]]; then
67 echo "Using the build already exported in $out."
68 ipa="$out/export/Bucksley.ipa"
69else
70 step "Generate the Xcode project"
71 ( cd "$root" && xcodegen generate --quiet )
72
73 step "Archive Bucksley $version ($build) for iPhone"
74 archive="$out/Bucksley.xcarchive"
75 xcodebuild archive -project "$root/Bucksley.xcodeproj" -scheme BucksleyPhone -configuration Release \
76 -destination 'generic/platform=iOS' -archivePath "$archive" -derivedDataPath "$work/derived-ios-release" \
77 CODE_SIGN_STYLE=Manual "CODE_SIGN_IDENTITY=Apple Distribution" "DEVELOPMENT_TEAM=$team" \
78 "BUCKSLEY_IOS_RELEASE_PROFILE=$profile_name" \
79 MARKETING_VERSION="$version" CURRENT_PROJECT_VERSION="$build" > "$out/archive.log" 2>&1 ||
80 { grep -E "error:" "$out/archive.log" | tail -20 >&2; exit 1; }
81
82 step "Export for App Store Connect"
83 cat > "$out/ExportOptions.plist" <<PLIST
84<?xml version="1.0" encoding="UTF-8"?>
85<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
86<plist version="1.0"><dict>
87 <key>method</key><string>app-store-connect</string>
88 <key>destination</key><string>export</string>
89 <key>teamID</key><string>$team</string>
90 <key>signingStyle</key><string>manual</string>
91 <key>signingCertificate</key><string>Apple Distribution</string>
92 <key>provisioningProfiles</key><dict><key>$bundle_id</key><string>$profile_name</string></dict>
93 <key>manageAppVersionAndBuildNumber</key><false/>
94</dict></plist>
95PLIST
96 xcodebuild -exportArchive -archivePath "$archive" -exportPath "$out/export" \
97 -exportOptionsPlist "$out/ExportOptions.plist" > "$out/export.log" 2>&1 ||
98 { grep -v IDEDistribution "$out/export.log" | tail -20 >&2; exit 1; }
99 ipa="$(print -l "$out"/export/*.ipa | head -1)"
100 [[ -f "$ipa" ]] || { echo "No IPA was exported." >&2; exit 1; }
101fi
102
103step "Upload to App Store Connect"
104API_PRIVATE_KEYS_DIR="$secrets" xcrun altool --upload-app -f "$ipa" -t ios \
105 --apiKey "$APPLE_API_KEY_ID" --apiIssuer "$APPLE_API_ISSUER" > "$out/upload.log" 2>&1 ||
106 { tail -20 "$out/upload.log" >&2; exit 1; }
107tail -3 "$out/upload.log"
108
109step "Wait for Apple to process the build"
110api wait-build "$app_id" "$version" "$build"
111
112step "Make it available to internal testers"
113api internal-group "$app_id" "$version" "$build"
114
115echo "\nBucksley $version ($build) is in TestFlight."