commit 08d8e9842a7d29600ee6b733f3103baad681c849
parent 3286b548f26c1e3bbe46be672db24d617366c4d7
Author: Michael Camilleri <[email protected]>
Date: Wed, 4 Nov 2026 09:03:27 +0900
Sign device builds with a temporary keychain
Device builds were signed with the identity in the login keychain, so
the build failed with errSecInternalComponent whenever codesign could
not use that keychain's key, such as when it was locked.
This commit has deploy-device.sh import .asc/dev.p12 into a temporary
keychain that is unlocked for the build, as publish-ios.sh already does.
The setup lives in Scripts/signing-keychain.sh which restores the user's
keychain search list and deletes the temporary keychain on exit. A run
with --skip-build signs nothing and skips it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat
| M | Scripts/deploy-device.sh | | | 21 | ++++++++++++++++++++- |
| A | Scripts/signing-keychain.sh | | | 88 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 108 insertions(+), 1 deletion(-)
diff --git a/Scripts/deploy-device.sh b/Scripts/deploy-device.sh
@@ -7,9 +7,13 @@
# The device is a name, UDID or identifier as `xcrun devicectl list devices`
# shows it; without one, the first available paired iPhone is used. The
# iPhone must have been paired with Xcode, with Developer Mode on.
+#
+# Signing uses a temporary keychain holding .asc/dev.p12, not the login
+# keychain, so a locked login keychain or a missed prompt can't fail it.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."
+source Scripts/signing-keychain.sh
SKIP_BUILD=false
LAUNCH=true
@@ -28,7 +32,11 @@ APP="${DERIVED_DATA}/Build/Products/Debug-iphoneos/Reportage.app"
# Resolves the device to its UDID, which both xcodebuild and devicectl accept.
DEVICES_JSON="$(mktemp)"
-trap 'rm -f "$DEVICES_JSON"' EXIT
+cleanup() {
+ rm -f "$DEVICES_JSON"
+ restore_signing_keychain
+}
+trap cleanup EXIT
xcrun devicectl list devices --json-output "$DEVICES_JSON" >/dev/null
read -r UDID NAME < <(python3 - "$DEVICES_JSON" "$DEVICE" <<'EOF'
import json, sys
@@ -57,6 +65,17 @@ if [[ -z "${UDID:-}" ]]; then
fi
if [[ "$SKIP_BUILD" == false ]]; then
+ SECRETS=".asc/secrets.sh"
+ if [[ ! -f "$SECRETS" ]]; then
+ echo "Error: $SECRETS not found. Copy .asc/ from another app; it holds team-wide signing files." >&2
+ exit 1
+ fi
+ source "$SECRETS"
+ TMP_KEYCHAIN="$(pwd)/.asc/build.keychain-db"
+ signing_keychain_begin
+ signing_keychain_import ".asc/dev.p12" "$DEV_P12_PASS"
+ signing_keychain_activate
+
echo "==> Building for ${NAME}"
xcodebuild build \
-scheme "Reportage" \
diff --git a/Scripts/signing-keychain.sh b/Scripts/signing-keychain.sh
@@ -0,0 +1,88 @@
+#!/bin/bash
+# Shared helper for scripts that need signing identities available to a headless
+# build. Source this file, set TMP_KEYCHAIN, then:
+#
+# cleanup() { restore_signing_keychain; }
+# trap cleanup EXIT
+# signing_keychain_begin
+# signing_keychain_import "$DEV_P12" "$DEV_P12_PASS"
+# signing_keychain_activate
+#
+# Install the trap BEFORE calling signing_keychain_begin: any step below can die
+# under `set -e`, and cleanup is written to tolerate a partially built setup.
+#
+# Requires TMP_KEYCHAIN_PASS from .asc/secrets.sh.
+
+SIGNING_KEYCHAIN_ORIGINAL_LIST=()
+SIGNING_KEYCHAIN_ORIGINAL_DEFAULT=""
+SIGNING_KEYCHAIN_ACTIVE=false
+
+# `security` prints each keychain path quoted and indented; strip both.
+_signing_keychain_unquote() {
+ local line="$1"
+ line="${line#"${line%%[![:space:]]*}"}"
+ line="${line%\"}"
+ line="${line#\"}"
+ printf '%s' "$line"
+}
+
+signing_keychain_begin() {
+ : "${TMP_KEYCHAIN:?TMP_KEYCHAIN must be set before signing_keychain_begin}"
+ : "${TMP_KEYCHAIN_PASS:?TMP_KEYCHAIN_PASS must be set (see .asc/secrets.sh)}"
+
+ # Capture the caller's exact keychain configuration before touching anything,
+ # so cleanup restores their real prior state rather than assuming login-only.
+ while IFS= read -r line; do
+ [[ -n "$line" ]] && SIGNING_KEYCHAIN_ORIGINAL_LIST+=("$(_signing_keychain_unquote "$line")")
+ done < <(security list-keychains -d user)
+
+ SIGNING_KEYCHAIN_ORIGINAL_DEFAULT="$(_signing_keychain_unquote \
+ "$(security default-keychain -d user 2>/dev/null || true)")"
+
+ if [[ ${#SIGNING_KEYCHAIN_ORIGINAL_LIST[@]} -eq 0 ]]; then
+ echo "Error: could not read the current keychain search list; refusing to modify it." >&2
+ return 1
+ fi
+
+ # Mark active before creating anything, so a failure part-way through setup
+ # still gets cleaned up.
+ SIGNING_KEYCHAIN_ACTIVE=true
+
+ echo "==> Setting up temporary keychain..."
+ security delete-keychain "$TMP_KEYCHAIN" 2>/dev/null || true
+ security create-keychain -p "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN"
+ security unlock-keychain -p "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN"
+ security set-keychain-settings -lut 21600 "$TMP_KEYCHAIN"
+}
+
+signing_keychain_import() {
+ local p12="$1"
+ local pass="$2"
+ if [[ ! -f "$p12" ]]; then
+ echo "Error: signing identity not found at $p12" >&2
+ return 1
+ fi
+ security import "$p12" -k "$TMP_KEYCHAIN" -P "$pass" \
+ -T /usr/bin/codesign -T /usr/bin/security -T /usr/bin/productbuild
+}
+
+signing_keychain_activate() {
+ security set-key-partition-list -S apple-tool:,apple:,codesign:,productbuild: \
+ -s -k "$TMP_KEYCHAIN_PASS" "$TMP_KEYCHAIN" >/dev/null
+ # Prepend rather than replace: resetting the list to a hardcoded
+ # login.keychain-db silently discards any other keychains the user had.
+ security list-keychains -d user -s "$TMP_KEYCHAIN" "${SIGNING_KEYCHAIN_ORIGINAL_LIST[@]}"
+}
+
+restore_signing_keychain() {
+ $SIGNING_KEYCHAIN_ACTIVE || return 0
+ echo "==> Restoring keychain search list..."
+ if [[ ${#SIGNING_KEYCHAIN_ORIGINAL_LIST[@]} -gt 0 ]]; then
+ security list-keychains -d user -s "${SIGNING_KEYCHAIN_ORIGINAL_LIST[@]}" || true
+ fi
+ if [[ -n "$SIGNING_KEYCHAIN_ORIGINAL_DEFAULT" ]]; then
+ security default-keychain -d user -s "$SIGNING_KEYCHAIN_ORIGINAL_DEFAULT" || true
+ fi
+ security delete-keychain "$TMP_KEYCHAIN" 2>/dev/null || true
+ SIGNING_KEYCHAIN_ACTIVE=false
+}