Skip to content

Commit 428323c

Browse files
ci: consolidate workflows and build Apple apps on GitHub (#345)
* ci(apple): build and upload on GitHub macOS runners * ci(apple): migrate main-branch TestFlight distribution * ci(apple): preserve automatic TestFlight uploads * ci(apple): document signing access and preserve upload errors * ci: consolidate app workflows into four entry points * ci(release): apply Play track only to Android publication * fix(ci): preserve macOS sandbox entitlements during export * fix(ci): request XML when checking signed entitlements
1 parent 9362965 commit 428323c

17 files changed

Lines changed: 1077 additions & 1176 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Apple build and upload
2+
description: Archive on a GitHub macOS runner and upload to App Store Connect.
3+
inputs:
4+
platform:
5+
description: ios or macos
6+
required: true
7+
app-dir:
8+
description: Release source Flutter application directory
9+
required: true
10+
outputs:
11+
build_number:
12+
description: Exact CFBundleVersion uploaded to Apple
13+
value: ${{ steps.archive.outputs.build_number }}
14+
runs:
15+
using: composite
16+
steps:
17+
- name: Select Xcode
18+
shell: bash
19+
run: |
20+
sudo xcode-select -s /Applications/Xcode_26.2.app/Contents/Developer
21+
xcodebuild -version
22+
23+
- name: Read Flutter version
24+
id: flutter
25+
shell: bash
26+
env:
27+
APP_DIR: ${{ inputs.app-dir }}
28+
run: echo "version=$(cat "$APP_DIR/.flutter_version")" >> "$GITHUB_OUTPUT"
29+
30+
- name: Set up Flutter
31+
uses: subosito/flutter-action@v2
32+
with:
33+
channel: stable
34+
flutter-version: ${{ steps.flutter.outputs.version }}
35+
cache: true
36+
37+
- name: Install dependencies
38+
shell: bash
39+
working-directory: ${{ inputs.app-dir }}
40+
run: flutter pub get
41+
42+
- name: Archive, sign and upload
43+
id: archive
44+
shell: bash
45+
env:
46+
APPLE_PLATFORM: ${{ inputs.platform }}
47+
APP_DIR: ${{ inputs.app-dir }}
48+
ACTION_PATH: ${{ github.action_path }}
49+
run: bash "$ACTION_PATH/../../scripts/apple_archive.sh" "$APPLE_PLATFORM" "$APP_DIR" "$RUNNER_TEMP/apple-build"
50+
51+
- name: Preserve build diagnostics
52+
if: ${{ !cancelled() }}
53+
uses: actions/upload-artifact@v6
54+
with:
55+
name: apple-${{ inputs.platform }}-diagnostics
56+
path: |
57+
${{ runner.temp }}/apple-build/*.log
58+
${{ runner.temp }}/apple-build/*.xcresult
59+
if-no-files-found: ignore
60+
retention-days: 7

‎.github/scripts/app_store_connect_release.rb‎

Lines changed: 69 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
require "timeout"
1111
require "uri"
1212

13+
$stdout.sync = true
14+
1315
class AppStoreConnectError < StandardError; end
1416

1517
class AppStoreConnectClient
@@ -151,9 +153,10 @@ def run
151153
validate_release_notes!
152154
return puts("Release inputs are valid") if @options[:validate_only]
153155

154-
build_run = start_xcode_cloud_run
155-
wait_for_xcode_cloud_run(build_run.fetch("id"))
156-
build = wait_for_processed_build(build_run.fetch("id"))
156+
build = wait_for_processed_build
157+
return puts("Uploaded build is ready; App Review submission was not requested") if @options[:process_only]
158+
return distribute_testflight(build) unless Array(@options[:testflight_groups]).empty?
159+
157160
version = find_or_create_version
158161

159162
if submitted?(version)
@@ -187,71 +190,12 @@ def validate_note!(path)
187190
raise AppStoreConnectError, "Release notes exceed Apple's 4000-character limit: #{path}"
188191
end
189192

190-
def start_xcode_cloud_run
191-
source_reference = wait_for_source_reference
192-
puts "Starting Xcode Cloud workflow #{@options[:workflow_id]} at #{@options[:tag]}"
193-
@client.post(
194-
"/v1/ciBuildRuns",
195-
data: {
196-
type: "ciBuildRuns",
197-
attributes: {},
198-
relationships: {
199-
workflow: { data: { type: "ciWorkflows", id: @options[:workflow_id] } },
200-
sourceBranchOrTag: { data: { type: "scmGitReferences", id: source_reference.fetch("id") } }
201-
}
202-
}
203-
).fetch("data")
204-
end
205-
206-
def wait_for_source_reference
207-
canonical_name = "refs/tags/#{@options[:tag]}"
208-
loop do
209-
repository = @client.get("/v1/ciWorkflows/#{@options[:workflow_id]}/repository").fetch("data")
210-
response = @client.get(
211-
"/v1/scmRepositories/#{repository.fetch('id')}/gitReferences",
212-
"fields[scmGitReferences]" => "name,canonicalName,isDeleted,kind",
213-
"limit" => 200
214-
)
215-
reference = Array(response["data"]).find do |candidate|
216-
candidate.dig("attributes", "canonicalName") == canonical_name &&
217-
!candidate.dig("attributes", "isDeleted")
218-
end
219-
return reference if reference
220-
221-
puts "Xcode Cloud has not indexed #{@options[:tag]} yet"
222-
wait_or_timeout!
223-
end
224-
end
225-
226-
def wait_for_xcode_cloud_run(build_run_id)
227-
puts "Waiting for Xcode Cloud build #{build_run_id}"
228-
loop do
229-
run = @client.get("/v1/ciBuildRuns/#{build_run_id}").fetch("data")
230-
commit_sha = run.dig("attributes", "sourceCommit", "commitSha").to_s
231-
if !commit_sha.empty? && commit_sha != @options[:git_sha]
232-
raise AppStoreConnectError,
233-
"Xcode Cloud build source commit #{commit_sha} does not match release commit #{@options[:git_sha]}"
234-
end
235-
236-
progress = run.dig("attributes", "executionProgress")
237-
if progress == "COMPLETE"
238-
status = run.dig("attributes", "completionStatus")
239-
raise AppStoreConnectError, "Xcode Cloud build finished with #{status}" unless status == "SUCCEEDED"
240-
241-
puts "Xcode Cloud build ##{run.dig('attributes', 'number')} succeeded"
242-
return run
243-
end
244-
245-
puts "Xcode Cloud build ##{run.dig('attributes', 'number')} is #{progress}"
246-
wait_or_timeout!
247-
end
248-
end
249-
250-
def wait_for_processed_build(build_run_id)
251-
puts "Waiting for App Store Connect to process the Xcode Cloud archive"
193+
def wait_for_processed_build
194+
puts "Waiting for App Store Connect to process #{@options[:platform]} #{@options[:version]} (#{@options[:build_number]})"
252195
loop do
253196
response = @client.get(
254-
"/v1/ciBuildRuns/#{build_run_id}/builds",
197+
"/v1/builds",
198+
"filter[version]" => @options[:build_number],
255199
"filter[app]" => @options[:app_id],
256200
"filter[preReleaseVersion.version]" => @options[:version],
257201
"filter[preReleaseVersion.platform]" => @options[:platform],
@@ -271,19 +215,66 @@ def wait_for_processed_build(build_run_id)
271215
audience = build.dig("attributes", "buildAudienceType")
272216
unless audience == "APP_STORE_ELIGIBLE"
273217
raise AppStoreConnectError,
274-
"Build #{build.dig('attributes', 'version')} is #{audience}; configure the Xcode Cloud " \
218+
"Build #{build.dig('attributes', 'version')} is #{audience}; export the " \
275219
"archive for TestFlight and App Store distribution"
276220
end
277221

278222
puts "Build #{build.dig('attributes', 'version')} is valid and App Store eligible"
279223
return build
280224
end
281225

282-
puts builds.empty? ? "No uploaded build is associated with the run yet" : "The build is still processing"
226+
puts builds.empty? ? "The uploaded build is not visible yet" : "The build is still processing"
283227
wait_or_timeout!
284228
end
285229
end
286230

231+
# Keep the same explicit tester groups as the retired main-branch workflows.
232+
# Beta App Review is separate from public App Store review.
233+
def distribute_testflight(build)
234+
groups = @client.get("/v1/apps/#{@options[:app_id]}/betaGroups", "limit" => 200).fetch("data")
235+
selected = @options[:testflight_groups].map do |name|
236+
matches = groups.select { |group| group.dig("attributes", "name") == name }
237+
raise AppStoreConnectError, "Expected exactly one TestFlight group named #{name}" unless matches.length == 1
238+
239+
matches.first
240+
end
241+
build_id = build.fetch("id")
242+
notes = File.read(File.join(@options[:release_notes_dir], "default.txt"), encoding: "UTF-8").strip
243+
localizations = @client.get("/v1/builds/#{build_id}/betaBuildLocalizations").fetch("data")
244+
if localizations.empty?
245+
@client.post("/v1/betaBuildLocalizations", data: {
246+
type: "betaBuildLocalizations", attributes: { locale: "en-US", whatsNew: notes },
247+
relationships: { build: { data: { type: "builds", id: build_id } } }
248+
})
249+
else
250+
localizations.each do |localization|
251+
@client.patch("/v1/betaBuildLocalizations/#{localization.fetch('id')}", data: {
252+
type: "betaBuildLocalizations", id: localization.fetch("id"), attributes: { whatsNew: notes }
253+
})
254+
end
255+
end
256+
257+
if selected.any? { |group| !group.dig("attributes", "isInternalGroup") }
258+
detail = @client.get("/v1/builds/#{build_id}/buildBetaDetail").fetch("data")
259+
@client.patch("/v1/buildBetaDetails/#{detail.fetch('id')}", data: {
260+
type: "buildBetaDetails", id: detail.fetch("id"), attributes: { autoNotifyEnabled: true }
261+
})
262+
state = detail.dig("attributes", "externalBuildState")
263+
if state == "READY_FOR_BETA_SUBMISSION"
264+
@client.post("/v1/betaAppReviewSubmissions", data: {
265+
type: "betaAppReviewSubmissions",
266+
relationships: { build: { data: { type: "builds", id: build_id } } }
267+
})
268+
elsif !%w[WAITING_FOR_BETA_REVIEW IN_BETA_REVIEW BETA_APPROVED READY_FOR_BETA_TESTING IN_BETA_TESTING].include?(state)
269+
raise AppStoreConnectError, "TestFlight build cannot be distributed: #{state}"
270+
end
271+
end
272+
@client.post("/v1/builds/#{build_id}/relationships/betaGroups", data: selected.map do |group|
273+
{ type: "betaGroups", id: group.fetch("id") }
274+
end)
275+
puts "Assigned #{@options[:platform]} build #{@options[:build_number]} to #{@options[:testflight_groups].join(', ')}"
276+
end
277+
287278
def find_or_create_version
288279
response = @client.get(
289280
"/v1/apps/#{@options[:app_id]}/appStoreVersions",
@@ -491,29 +482,30 @@ def wait_or_timeout!
491482
end
492483
end
493484

494-
begin
485+
def run_release_cli
495486
options = {
496-
timeout: 7200,
487+
timeout: 2700,
497488
release_type: "AFTER_APPROVAL",
498-
validate_only: false
489+
validate_only: false,
490+
testflight_groups: []
499491
}
500492

501493
OptionParser.new do |parser|
502494
parser.banner = "Usage: app_store_connect_release.rb [options]"
503-
parser.on("--workflow-id ID") { |value| options[:workflow_id] = value }
495+
parser.on("--build-number NUMBER") { |value| options[:build_number] = value }
504496
parser.on("--app-id ID") { |value| options[:app_id] = value }
505497
parser.on("--platform PLATFORM") { |value| options[:platform] = value }
506498
parser.on("--version VERSION") { |value| options[:version] = value }
507-
parser.on("--tag TAG") { |value| options[:tag] = value }
508-
parser.on("--git-sha SHA") { |value| options[:git_sha] = value }
509499
parser.on("--release-notes-dir PATH") { |value| options[:release_notes_dir] = value }
510500
parser.on("--release-type TYPE") { |value| options[:release_type] = value }
511501
parser.on("--timeout SECONDS", Integer) { |value| options[:timeout] = value }
512502
parser.on("--validate-only") { options[:validate_only] = true }
503+
parser.on("--process-only") { options[:process_only] = true }
504+
parser.on("--testflight-group NAME") { |value| options[:testflight_groups] << value }
513505
end.parse!
514506

515507
required = %i[platform version release_notes_dir]
516-
required += %i[workflow_id app_id tag git_sha] unless options[:validate_only]
508+
required += %i[app_id build_number] unless options[:validate_only]
517509
missing = required.select { |key| options[key].nil? || options[key].empty? }
518510
raise AppStoreConnectError, "Missing options: #{missing.join(', ')}" unless missing.empty?
519511
unless %w[IOS MAC_OS].include?(options[:platform])
@@ -538,3 +530,5 @@ def wait_or_timeout!
538530
warn "::error::#{error.message}"
539531
exit 1
540532
end
533+
534+
run_release_cli if $PROGRAM_NAME == __FILE__

‎.github/scripts/apple_archive.sh‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
# Archive on GitHub, then let Xcode sign and upload using the existing Apple API key.
3+
set -euo pipefail
4+
5+
platform="$1"
6+
app_dir="$2"
7+
output_dir="$3"
8+
case "$platform" in
9+
ios) destination='generic/platform=iOS' ;;
10+
macos) destination='generic/platform=macOS' ;;
11+
*) echo "Unsupported Apple platform: $platform" >&2; exit 1 ;;
12+
esac
13+
14+
mkdir -p "$output_dir"
15+
output_dir=$(cd "$output_dir" && pwd)
16+
cd "$app_dir"
17+
# Three numeric components, each within Apple's digit limits. Reruns get a new
18+
# increasing build number, including when a different workflow releases the tag.
19+
build_number=$(date -u '+%y%m.%d%H.%M%S' | awk -F. '{printf "%d.%d.%d\n", $1, $2, $3}')
20+
printf 'build_number=%s\n' "$build_number" >> "$GITHUB_OUTPUT"
21+
printf 'Building %s from %s as %s\n' "$platform" "$(git rev-parse HEAD)" "$build_number"
22+
23+
config_args=("$platform" --release --config-only --build-number="$build_number")
24+
if [[ "$platform" == ios ]]; then config_args+=(--no-codesign); fi
25+
flutter build "${config_args[@]}"
26+
(cd "$platform" && pod install)
27+
28+
signing_args=(CODE_SIGNING_ALLOWED=NO)
29+
if [[ "$platform" == macos ]]; then
30+
# Xcode's export preserves the archive's signed entitlements. An unsigned Mac
31+
# archive loses App Sandbox at export, so embed the project entitlements in
32+
# an ad-hoc signature first; export replaces it with Apple's distribution one.
33+
signing_args=(CODE_SIGNING_ALLOWED=YES CODE_SIGN_IDENTITY=- CODE_SIGN_STYLE=Manual)
34+
fi
35+
xcodebuild archive -hideShellScriptEnvironment \
36+
-workspace "$platform/Runner.xcworkspace" -scheme Runner \
37+
-configuration Release -destination "$destination" \
38+
-archivePath "$output_dir/Runner.xcarchive" \
39+
-resultBundlePath "$output_dir/archive.xcresult" \
40+
"${signing_args[@]}" \
41+
2>&1 | tee "$output_dir/archive.log"
42+
43+
if [[ "$platform" == macos ]]; then
44+
app_path=$(/usr/libexec/PlistBuddy -c 'Print :ApplicationProperties:ApplicationPath' "$output_dir/Runner.xcarchive/Info.plist")
45+
codesign -d --entitlements - --xml "$output_dir/Runner.xcarchive/Products/$app_path" \
46+
> "$output_dir/archive-entitlements.plist"
47+
python3 - "$platform/Runner/Release.entitlements" "$output_dir/archive-entitlements.plist" <<'PY'
48+
import plistlib
49+
import sys
50+
51+
with open(sys.argv[1], 'rb') as source, open(sys.argv[2], 'rb') as archive:
52+
expected, actual = plistlib.load(source), plistlib.load(archive)
53+
if actual.get('com.apple.security.app-sandbox') is not True:
54+
raise SystemExit('macOS archive is missing App Sandbox')
55+
for key, value in expected.items():
56+
if actual.get(key) != value:
57+
raise SystemExit(f'macOS archive entitlement was not preserved: {key}')
58+
print('macOS release entitlements verified before upload')
59+
PY
60+
fi
61+
62+
# Only the ephemeral runner sees this file. It is never included in artifacts.
63+
key_dir=$(mktemp -d "$RUNNER_TEMP/apple-api.XXXXXX")
64+
trap 'rm -rf "$key_dir"' EXIT
65+
export APPLE_API_KEY_PATH="$key_dir/AuthKey.p8"
66+
ruby -rbase64 -e '
67+
key = ENV.fetch("APP_STORE_CONNECT_PRIVATE_KEY").gsub("\\n", "\n").strip
68+
key = Base64.strict_decode64(key) unless key.include?("BEGIN")
69+
File.write(ENV.fetch("APPLE_API_KEY_PATH"), key, perm: 0600)
70+
'
71+
cat > "$output_dir/ExportOptions.plist" <<'PLIST'
72+
<?xml version="1.0" encoding="UTF-8"?>
73+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
74+
<plist version="1.0"><dict>
75+
<key>method</key><string>app-store-connect</string>
76+
<key>destination</key><string>upload</string>
77+
<key>signingStyle</key><string>automatic</string>
78+
<key>teamID</key><string>6DCQ69GP5G</string>
79+
<key>manageAppVersionAndBuildNumber</key><false/>
80+
<key>uploadSymbols</key><true/>
81+
</dict></plist>
82+
PLIST
83+
xcodebuild -exportArchive \
84+
-archivePath "$output_dir/Runner.xcarchive" \
85+
-exportOptionsPlist "$output_dir/ExportOptions.plist" \
86+
-exportPath "$output_dir/export" \
87+
-allowProvisioningUpdates \
88+
-authenticationKeyPath "$APPLE_API_KEY_PATH" \
89+
-authenticationKeyID "$APP_STORE_CONNECT_KEY_ID" \
90+
-authenticationKeyIssuerID "$APP_STORE_CONNECT_ISSUER_ID" \
91+
2>&1 | tee "$output_dir/upload.log"

0 commit comments

Comments
 (0)