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