release-ios.sh

main
appstore-kit/release-ios.sh history · blame · raw

125 lines · 5326 bytes · executable

2 symbols in this file
  1#!/bin/sh
  2# Archives an iOS or macOS app and uploads it to App Store Connect, signed automatically with the team's
  3# distribution certificate through the Apple ID signed in to Xcode. The upload shows in TestFlight
  4# once processed and can then be picked for a submission.
  5#
  6# Sourced by each app's scripts/release-ios.sh, which sets these first:
  7#   name            the app's name, for messages
  8#   project         the .xcodeproj, relative to the repository root
  9#   scheme          the scheme to archive
 10#   spec            optional: XcodeGen's project.yml. The project is generated from it first, and
 11#                   the version is read from it rather than from the project file.
 12#   build_number    commits (default): the commit count, passed to xcodebuild;
 13#                   committed: CURRENT_PROJECT_VERSION as committed
 14#   platform        ios (default) or macos
 15#   team            the team ID (default ZCNAX3VL9D)
 16#   allow_untracked 1 lets untracked files through the clean-tree check (default 0)
 17# and may define preflight(), run with $version, $build and $publish set, before archiving.
 18#
 19# PUBLISH=0 archives and exports the .ipa or .pkg to .build/<platform>-release without uploading. Wrappers set
 20# PUBLISH from their own variable, such as MOONRISE_PUBLISH.
 21set -eu
 22
 23cd "$(git -C "$(dirname "$0")" rev-parse --show-toplevel)"
 24publish="${PUBLISH:-1}"
 25team="${team:-ZCNAX3VL9D}"
 26build_number="${build_number:-commits}"
 27spec="${spec:-}"
 28platform="${platform:-ios}"
 29
 30fail() {
 31    echo "release-ios: $1" >&2
 32    exit 1
 33}
 34
 35# One value across all targets and configurations, or the archive would disagree with itself
 36# depending on which target you read.
 37setting() {
 38    if [ -n "$spec" ]; then
 39        values="$(sed -n "s/.*$1: \"*\([^\"]*\)\"*$/\1/p" "$spec" | sort -u)"
 40    else
 41        values="$(sed -n "s/.*$1 = \(.*\);/\1/p" "$project/project.pbxproj" | sort -u)"
 42    fi
 43    [ -n "$values" ] || fail "no $1 in ${spec:-$project}"
 44    [ "$(printf '%s\n' "$values" | wc -l | tr -d ' ')" = 1 ] || fail "$1 differs across targets: $(printf '%s' "$values" | tr '\n' ' ')"
 45    printf '%s' "$values"
 46}
 47
 48if [ "$publish" != 0 ]; then
 49    if [ "${allow_untracked:-0}" = 1 ]; then untracked=no; else untracked=normal; fi
 50    [ -z "$(git status --porcelain --untracked-files=$untracked)" ] || fail "the working tree has changes"
 51    [ "$(git branch --show-current)" = main ] || fail "release from main"
 52fi
 53
 54version="$(setting MARKETING_VERSION)"
 55case "$build_number" in
 56    commits) build="$(git rev-list --count HEAD)" ;;
 57    committed) build="$(setting CURRENT_PROJECT_VERSION)" ;;
 58    *) fail "build_number is commits or committed, not $build_number" ;;
 59esac
 60
 61case "$platform" in
 62    ios) destination='generic/platform=iOS' info=Info.plist ;;
 63    macos) destination='generic/platform=macOS' info=Contents/Info.plist ;;
 64    *) fail "platform is ios or macos, not $platform" ;;
 65esac
 66
 67if command -v preflight >/dev/null; then preflight; fi
 68
 69if [ -n "$spec" ]; then
 70    command -v xcodegen >/dev/null || fail "install XcodeGen: brew install xcodegen"
 71    (cd "$(dirname "$spec")" && xcodegen generate --quiet)
 72fi
 73
 74echo "==> $name $version ($build), $platform"
 75out=".build/$platform-release"
 76rm -rf "${out:?}"
 77mkdir -p "$out"
 78archive="$out/$scheme.xcarchive"
 79if [ -z "$spec" ]; then
 80    # xcodebuild rewrites the project file in its own key order; the copy puts it back as it was.
 81    cp "$project/project.pbxproj" "$out/project.pbxproj"
 82    trap 'cp "$out/project.pbxproj" "$project/project.pbxproj"' EXIT
 83fi
 84xcodebuild archive -project "$project" -scheme "$scheme" -configuration Release \
 85    -destination "$destination" -archivePath "$archive" -derivedDataPath "$out/DerivedData" \
 86    -allowProvisioningUpdates -quiet MARKETING_VERSION="$version" CURRENT_PROJECT_VERSION="$build"
 87
 88# The project file is not the product: check what was built.
 89set -- "$archive"/Products/Applications/*.app
 90plist="$1/$info"
 91built_version="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$plist")"
 92built_build="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleVersion' "$plist")"
 93[ "$built_version" = "$version" ] || fail "archive is $built_version, expected $version"
 94[ "$built_build" = "$build" ] || fail "archive is build $built_build, expected $build"
 95
 96if [ "$publish" = 0 ]; then method=export; else method=upload; fi
 97cat > "$out/ExportOptions.plist" <<PLIST
 98<?xml version="1.0" encoding="UTF-8"?>
 99<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
100<plist version="1.0">
101<dict>
102    <key>method</key>
103    <string>app-store-connect</string>
104    <key>destination</key>
105    <string>$method</string>
106    <key>teamID</key>
107    <string>$team</string>
108    <key>signingStyle</key>
109    <string>automatic</string>
110    <key>manageAppVersionAndBuildNumber</key>
111    <false/>
112    <key>uploadSymbols</key>
113    <true/>
114</dict>
115</plist>
116PLIST
117echo "==> $method"
118xcodebuild -exportArchive -archivePath "$archive" -exportOptionsPlist "$out/ExportOptions.plist" \
119    -exportPath "$out/export" -allowProvisioningUpdates -quiet
120
121if [ "$publish" = 0 ]; then
122    echo "Exported $(ls "$out"/export/*.ipa "$out"/export/*.pkg 2>/dev/null): $name $version ($build), not uploaded"
123else
124    echo "Uploaded $name $version ($build) to App Store Connect; it shows in TestFlight once processed"
125fi