← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Scripts/publish-ios.sh (6844B)

1#!/bin/bash
2set -euo pipefail
3
4REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
5SECRETS="$REPO_ROOT/.asc/secrets.sh"
6
7if [[ ! -f "$SECRETS" ]]; then
8    echo "Error: $SECRETS not found. Copy .asc/ from another app; it holds team-wide signing files."
9    exit 1
10fi
11
12source "$SECRETS"
13
14SCHEME="Reportage"
15# Archive into the Xcode Archives library at a unique per-build path so
16# Organizer indexes every build and TestFlight crashes symbolicate
17# automatically. One timestamp drives both the date dir and the archive name
18# so they can't disagree across a midnight boundary.
19ARCHIVE_STAMP="$(date '+%Y-%m-%d %H.%M.%S')"
20ARCHIVE_DIR="$HOME/Library/Developer/Xcode/Archives/${ARCHIVE_STAMP%% *}"
21ARCHIVE_PATH="$ARCHIVE_DIR/$SCHEME $ARCHIVE_STAMP.xcarchive"
22EXPORT_PATH="/tmp/Reportage-export"
23EXPORT_PLIST="/tmp/Reportage-ExportOptions.plist"
24IPA_PATH="$EXPORT_PATH/Reportage.ipa"
25DEV_P12="$REPO_ROOT/.asc/dev.p12"
26DIST_P12="$REPO_ROOT/.asc/ios-signing/dist-headless.p12"
27TMP_KEYCHAIN="$REPO_ROOT/.asc/build.keychain-db"
28
29CHECK_ONLY=false
30if [[ "${1:-}" == "--check" ]]; then
31    CHECK_ONLY=true
32fi
33
34cd "$REPO_ROOT"
35
36if ! git diff --quiet || ! git diff --cached --quiet; then
37    echo "Error: Git repository is dirty. Commit or stash changes before publishing."
38    exit 1
39fi
40
41# A release tag marks a version as shipped. Uploading under a tagged version
42# means the bump after that release was missed, and the build would be rejected
43# by App Store Connect or land on a version already in the store.
44APP_VERSION="$(awk -F'"' '/CFBundleShortVersionString/ { print $2; exit }' "$REPO_ROOT/project.yml")"
45if [[ -z "$APP_VERSION" ]]; then
46    echo "Error: could not read CFBundleShortVersionString from project.yml."
47    exit 1
48fi
49if git rev-parse -q --verify "refs/tags/v$APP_VERSION" >/dev/null; then
50    echo "Error: v$APP_VERSION is already tagged as released. Bump the version in project.yml and both Info.plist files first."
51    exit 1
52fi
53
54# Capture the caller's exact keychain configuration before touching anything,
55# so cleanup restores their real prior state rather than assuming login-only.
56# `security` prints each path quoted and indented; strip both.
57unquote_keychain_path() {
58    local line="$1"
59    line="${line#"${line%%[![:space:]]*}"}"
60    line="${line%\"}"
61    line="${line#\"}"
62    printf '%s' "$line"
63}
64
65ORIGINAL_KEYCHAINS=()
66while IFS= read -r line; do
67    [[ -n "$line" ]] && ORIGINAL_KEYCHAINS+=("$(unquote_keychain_path "$line")")
68done < <(security list-keychains -d user)
69
70ORIGINAL_DEFAULT_KEYCHAIN="$(unquote_keychain_path "$(security default-keychain -d user 2>/dev/null || true)")"
71
72if [[ ${#ORIGINAL_KEYCHAINS[@]} -eq 0 ]]; then
73    echo "Error: could not read the current keychain search list; refusing to modify it."
74    exit 1
75fi
76
77# Cleanup must tolerate a partially completed setup: it is installed before the
78# temporary keychain exists, and any step below can die under `set -e`.
79cleanup_keychain() {
80    echo "==> Restoring keychain search list..."
81    if [[ ${#ORIGINAL_KEYCHAINS[@]} -gt 0 ]]; then
82        security list-keychains -d user -s "${ORIGINAL_KEYCHAINS[@]}" || true
83    fi
84    if [[ -n "$ORIGINAL_DEFAULT_KEYCHAIN" ]]; then
85        security default-keychain -d user -s "$ORIGINAL_DEFAULT_KEYCHAIN" || true
86    fi
87    security delete-keychain "$TMP_KEYCHAIN" 2>/dev/null || true
88    rm -f "$REPO_ROOT/private_keys/AuthKey_${KEY_ID}.p8"
89    rmdir "$REPO_ROOT/private_keys" 2>/dev/null || true
90}
91trap cleanup_keychain EXIT
92
93echo "==> Setting up temporary keychain..."
94security delete-keychain "$TMP_KEYCHAIN" 2>/dev/null || true
95security create-keychain -p "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN"
96security unlock-keychain -p "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN"
97security set-keychain-settings -lut 21600 "$TMP_KEYCHAIN"
98security import "$DEV_P12" -k "$TMP_KEYCHAIN" -P "$DEV_P12_PASS" \
99    -T /usr/bin/codesign -T /usr/bin/security -T /usr/bin/productbuild
100security import "$DIST_P12" -k "$TMP_KEYCHAIN" -P "$DIST_P12_PASS" \
101    -T /usr/bin/codesign -T /usr/bin/security -T /usr/bin/productbuild
102security set-key-partition-list -S apple-tool:,apple:,codesign:,productbuild: \
103    -s -k "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN"
104security list-keychains -d user -s "$TMP_KEYCHAIN" "${ORIGINAL_KEYCHAINS[@]}"
105
106mkdir -p "$ARCHIVE_DIR"
107echo "==> Archiving $SCHEME to $ARCHIVE_PATH..."
108xcodebuild \
109    -scheme "$SCHEME" \
110    -project Reportage.xcodeproj \
111    -configuration Release \
112    -destination 'generic/platform=iOS' \
113    -archivePath "$ARCHIVE_PATH" \
114    archive
115
116echo "==> Writing export options..."
117cat > "$EXPORT_PLIST" <<PLIST
118<?xml version="1.0" encoding="UTF-8"?>
119<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
120  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
121<plist version="1.0">
122<dict>
123    <key>method</key>
124    <string>app-store-connect</string>
125    <key>signingStyle</key>
126    <string>manual</string>
127    <key>teamID</key>
128    <string>$TEAM_ID</string>
129    <key>signingCertificate</key>
130    <string>iPhone Distribution</string>
131    <key>provisioningProfiles</key>
132    <dict>
133        <key>net.inqk.reportage</key>
134        <string>Reportage iOS Distribution</string>
135        <key>net.inqk.reportage.widgets</key>
136        <string>Reportage Widgets iOS Distribution</string>
137    </dict>
138    <key>destination</key>
139    <string>export</string>
140    <key>stripSwiftSymbols</key>
141    <true/>
142    <key>manageAppVersionAndBuildNumber</key>
143    <false/>
144</dict>
145</plist>
146PLIST
147
148echo "==> Exporting IPA..."
149xcodebuild \
150    -exportArchive \
151    -archivePath "$ARCHIVE_PATH" \
152    -exportPath "$EXPORT_PATH" \
153    -exportOptionsPlist "$EXPORT_PLIST"
154
155echo "==> Checking entitlements in exported IPA..."
156CHECK_DIR="/tmp/Reportage-ipa-check"
157rm -rf "$CHECK_DIR"
158unzip -q "$IPA_PATH" -d "$CHECK_DIR"
159APP_DIR="$CHECK_DIR/Payload/Reportage.app"
160APP_ENTITLEMENTS="$(codesign -d --entitlements - --xml "$APP_DIR" 2>/dev/null)"
161for key in com.apple.developer.healthkit com.apple.developer.weatherkit; do
162    if ! grep -q "<key>$key</key>" <<<"$APP_ENTITLEMENTS"; then
163        echo "Error: exported app is missing the $key entitlement."
164        exit 1
165    fi
166done
167if [[ ! -d "$APP_DIR/PlugIns/ReportageWidgets.appex" ]]; then
168    echo "Error: exported app does not contain ReportageWidgets.appex."
169    exit 1
170fi
171echo "--- iOS app entitlements ---"
172codesign -d --entitlements - "$APP_DIR"
173echo "--- Widgets entitlements ---"
174codesign -d --entitlements - "$APP_DIR/PlugIns/ReportageWidgets.appex"
175rm -rf "$CHECK_DIR"
176
177if $CHECK_ONLY; then
178    echo "==> Check complete. Skipping upload."
179    exit 0
180fi
181
182echo "==> Uploading to App Store Connect..."
183mkdir -p "$REPO_ROOT/private_keys"
184cp "$REPO_ROOT/.asc/AuthKey_${KEY_ID}.p8" "$REPO_ROOT/private_keys/"
185xcrun iTMSTransporter \
186    -m upload \
187    -assetFile "$IPA_PATH" \
188    -apiKey "$KEY_ID" \
189    -apiIssuer "$ISSUER_ID" \
190    -v informational
191
192echo "==> Done!"