#!/bin/bash # Build an Ad Hoc IPA and copy to the shared distribution server. # Usage: ./build-and-deploy.sh [branch] # branch defaults to current branch if not specified set -e SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SERVER_BUILDS_DIR="/Volumes/Next2/hermes-home/1ai/builds" BUILD_DIR="$SCRIPT_DIR/.build" ARCHIVE_PATH="$BUILD_DIR/Marks.xcarchive" EXPORT_PATH="$BUILD_DIR/export" REPO_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" SCHEME="${SCHEME:-Marks}" CONFIGURATION="${CONFIGURATION:-Debug}" EXPORT_METHOD="${EXPORT_METHOD:-development}" mkdir -p "$SERVER_BUILDS_DIR" mkdir -p "$BUILD_DIR" cd "$REPO_DIR" CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) if [ -n "$CI" ]; then COMMIT=$(git rev-parse --short HEAD) echo "📌 CI mode — building commit: $COMMIT (ref: $CURRENT_BRANCH)" elif [ -n "$1" ] && [ "$1" != "$CURRENT_BRANCH" ]; then echo "🔀 Switching from $CURRENT_BRANCH to $1..." git checkout "$1" echo "⬇️ Pulling latest $1..." git pull origin "$1" COMMIT=$(git rev-parse --short HEAD) echo "📌 Building commit: $COMMIT on $1" else COMMIT=$(git rev-parse --short HEAD) if git ls-remote --exit-code origin "$CURRENT_BRANCH" &>/dev/null; then echo "⬇️ Pulling latest $CURRENT_BRANCH..." git pull origin "$CURRENT_BRANCH" else echo "⚠️ No remote for $CURRENT_BRANCH — building local HEAD" fi echo "📌 Building commit: $COMMIT on $CURRENT_BRANCH" fi echo "⚙️ Generating Xcode project..." xcodegen generate echo "🔨 Building $SCHEME ($CONFIGURATION)..." xcodebuild archive \ -scheme "$SCHEME" \ -configuration "$CONFIGURATION" \ -archivePath "$ARCHIVE_PATH" \ -destination 'generic/platform=iOS' \ 2>&1 | tee "$BUILD_DIR/archive.log" | tail -20 cat > "$BUILD_DIR/ExportOptions.plist" << EOF method ${EXPORT_METHOD} signingStyle automatic teamID AE5DZKJHGN compileBitcode stripSwiftSymbols EOF echo "📦 Exporting IPA..." xcodebuild -exportArchive \ -archivePath "$ARCHIVE_PATH" \ -exportPath "$EXPORT_PATH" \ -exportOptionsPlist "$BUILD_DIR/ExportOptions.plist" \ 2>&1 | tee "$BUILD_DIR/export.log" | tail -20 IPA_FILE=$(find "$EXPORT_PATH" -name "*.ipa" | head -1) if [ -n "$IPA_FILE" ]; then TIMESTAMP=$(date +%Y%m%d-%H%M%S) APP_PROPS="$ARCHIVE_PATH/Info.plist" VERSION=$(/usr/libexec/PlistBuddy -c "Print :ApplicationProperties:CFBundleShortVersionString" "$APP_PROPS") BUILD=$(/usr/libexec/PlistBuddy -c "Print :ApplicationProperties:CFBundleVersion" "$APP_PROPS") BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-$(git rev-parse --abbrev-ref HEAD)}}" BRANCH=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9._-]/-/g') DEST_NAME="Marks-${VERSION}-${BUILD}-${BRANCH}-${TIMESTAMP}.ipa" cp "$IPA_FILE" "$SERVER_BUILDS_DIR/$DEST_NAME" echo "" echo "✅ Build complete!" echo " IPA: $SERVER_BUILDS_DIR/$DEST_NAME" echo " Server: https://krishnas-mac-studio.tail37d544.ts.net:8443" else echo "❌ No IPA file found in export" exit 1 fi