#!/bin/sh # Archives an iOS or macOS app and uploads it to App Store Connect, signed automatically with the team's # distribution certificate through the Apple ID signed in to Xcode. The upload shows in TestFlight # once processed and can then be picked for a submission. # # Sourced by each app's scripts/release-ios.sh, which sets these first: # name the app's name, for messages # project the .xcodeproj, relative to the repository root # scheme the scheme to archive # spec optional: XcodeGen's project.yml. The project is generated from it first, and # the version is read from it rather than from the project file. # build_number commits (default): the commit count, passed to xcodebuild; # committed: CURRENT_PROJECT_VERSION as committed # platform ios (default) or macos # team the team ID (default ZCNAX3VL9D) # allow_untracked 1 lets untracked files through the clean-tree check (default 0) # and may define preflight(), run with $version, $build and $publish set, before archiving. # # PUBLISH=0 archives and exports the .ipa or .pkg to .build/-release without uploading. Wrappers set # PUBLISH from their own variable, such as MOONRISE_PUBLISH. set -eu cd "$(git -C "$(dirname "$0")" rev-parse --show-toplevel)" publish="${PUBLISH:-1}" team="${team:-ZCNAX3VL9D}" build_number="${build_number:-commits}" spec="${spec:-}" platform="${platform:-ios}" fail() { echo "release-ios: $1" >&2 exit 1 } # One value across all targets and configurations, or the archive would disagree with itself # depending on which target you read. setting() { if [ -n "$spec" ]; then values="$(sed -n "s/.*$1: \"*\([^\"]*\)\"*$/\1/p" "$spec" | sort -u)" else values="$(sed -n "s/.*$1 = \(.*\);/\1/p" "$project/project.pbxproj" | sort -u)" fi [ -n "$values" ] || fail "no $1 in ${spec:-$project}" [ "$(printf '%s\n' "$values" | wc -l | tr -d ' ')" = 1 ] || fail "$1 differs across targets: $(printf '%s' "$values" | tr '\n' ' ')" printf '%s' "$values" } if [ "$publish" != 0 ]; then if [ "${allow_untracked:-0}" = 1 ]; then untracked=no; else untracked=normal; fi [ -z "$(git status --porcelain --untracked-files=$untracked)" ] || fail "the working tree has changes" [ "$(git branch --show-current)" = main ] || fail "release from main" fi version="$(setting MARKETING_VERSION)" case "$build_number" in commits) build="$(git rev-list --count HEAD)" ;; committed) build="$(setting CURRENT_PROJECT_VERSION)" ;; *) fail "build_number is commits or committed, not $build_number" ;; esac case "$platform" in ios) destination='generic/platform=iOS' info=Info.plist ;; macos) destination='generic/platform=macOS' info=Contents/Info.plist ;; *) fail "platform is ios or macos, not $platform" ;; esac if command -v preflight >/dev/null; then preflight; fi if [ -n "$spec" ]; then command -v xcodegen >/dev/null || fail "install XcodeGen: brew install xcodegen" (cd "$(dirname "$spec")" && xcodegen generate --quiet) fi echo "==> $name $version ($build), $platform" out=".build/$platform-release" rm -rf "${out:?}" mkdir -p "$out" archive="$out/$scheme.xcarchive" if [ -z "$spec" ]; then # xcodebuild rewrites the project file in its own key order; the copy puts it back as it was. cp "$project/project.pbxproj" "$out/project.pbxproj" trap 'cp "$out/project.pbxproj" "$project/project.pbxproj"' EXIT fi xcodebuild archive -project "$project" -scheme "$scheme" -configuration Release \ -destination "$destination" -archivePath "$archive" -derivedDataPath "$out/DerivedData" \ -allowProvisioningUpdates -quiet MARKETING_VERSION="$version" CURRENT_PROJECT_VERSION="$build" # The project file is not the product: check what was built. set -- "$archive"/Products/Applications/*.app plist="$1/$info" built_version="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$plist")" built_build="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleVersion' "$plist")" [ "$built_version" = "$version" ] || fail "archive is $built_version, expected $version" [ "$built_build" = "$build" ] || fail "archive is build $built_build, expected $build" if [ "$publish" = 0 ]; then method=export; else method=upload; fi cat > "$out/ExportOptions.plist" < method app-store-connect destination $method teamID $team signingStyle automatic manageAppVersionAndBuildNumber uploadSymbols PLIST echo "==> $method" xcodebuild -exportArchive -archivePath "$archive" -exportOptionsPlist "$out/ExportOptions.plist" \ -exportPath "$out/export" -allowProvisioningUpdates -quiet if [ "$publish" = 0 ]; then echo "Exported $(ls "$out"/export/*.ipa "$out"/export/*.pkg 2>/dev/null): $name $version ($build), not uploaded" else echo "Uploaded $name $version ($build) to App Store Connect; it shows in TestFlight once processed" fi