mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 19:07:41 +00:00
Play Console's automatic protection injects com.pairip's license check into every APK Play generates, including the universal APK we republish on GitHub. On phones where Google Play is disabled or unreachable (mainland China) the check shows "Something went wrong ... Check that Google Play is enabled" and closes the app. download_signed_apk now fails before the GitHub release when the APK's dex carries com/pairip/licensecheck, and the README says to keep automatic protection off. Co-authored-by: Claude <noreply@anthropic.com>
328 lines
12 KiB
Ruby
328 lines
12 KiB
Ruby
# Manual cert/profile signing avoids automatic cloud-signing export errors and its Admin-key requirement.
|
||
# Credentials come from CI environment variables (.github/workflows/mobile-ios-release.yml).
|
||
|
||
require "base64"
|
||
require "json"
|
||
require_relative "ios_release_version"
|
||
|
||
default_platform(:ios)
|
||
|
||
MOBILE_ROOT = File.expand_path("..", __dir__)
|
||
# Fastlane runs from mobile/fastlane regardless of the caller’s working directory.
|
||
APP_CONFIG_PATH = File.join(MOBILE_ROOT, "app.json")
|
||
WORKSPACE = File.join(MOBILE_ROOT, "ios", "Orca.xcworkspace")
|
||
BUILD_OUTPUT_DIRECTORY = File.join(MOBILE_ROOT, "build")
|
||
SCHEME = "Orca"
|
||
BUNDLE_ID = "com.stably.orca.mobile"
|
||
TESTFLIGHT_GROUPS = ["peeps"].freeze
|
||
DEFAULT_TESTFLIGHT_CHANGELOG = "Latest Orca Mobile updates and fixes.".freeze
|
||
TESTFLIGHT_PROCESSING_TIMEOUT_SECONDS = 20 * 60
|
||
# Google's Play app signing key for dev.orca.app. Play and GitHub installs both carry it, so each updates the other.
|
||
PLAY_APP_SIGNING_CERT_SHA256 = "A0:A6:67:69:42:B2:DE:E1:F3:5F:B0:F6:FC:A0:AE:63:B5:65:4E:42:6C:A3:1A:DA:4C:F9:BE:09:2B:D7:DA:07".freeze
|
||
PLAY_APK_DOWNLOAD_ATTEMPTS = 20
|
||
PLAY_APK_DOWNLOAD_RETRY_SECONDS = 30
|
||
|
||
# Only terminal App Store states close TestFlight trains; in-review/processing states still accept builds.
|
||
# API 3.3 replaces appStoreState with appVersionState, renames ready-for-sale and drops removed states;
|
||
# read both vocabularies so mixed spaceship/API versions cannot silently bypass the guard.
|
||
CLOSED_APP_STORE_STATES = %w[
|
||
READY_FOR_SALE
|
||
READY_FOR_DISTRIBUTION
|
||
PENDING_DEVELOPER_RELEASE
|
||
PENDING_APPLE_RELEASE
|
||
REPLACED_WITH_NEW_VERSION
|
||
REMOVED_FROM_SALE
|
||
DEVELOPER_REMOVED_FROM_SALE
|
||
].freeze
|
||
|
||
def app_store_connect_api_key_from_env
|
||
app_store_connect_api_key(
|
||
key_id: ENV.fetch("ASC_KEY_ID"),
|
||
issuer_id: ENV.fetch("ASC_ISSUER_ID"),
|
||
key_content: ENV.fetch("ASC_API_KEY_P8"),
|
||
is_key_content_base64: true,
|
||
in_house: false,
|
||
)
|
||
end
|
||
|
||
def load_mobile_app_config
|
||
JSON.parse(File.read(APP_CONFIG_PATH))
|
||
end
|
||
|
||
def android_package
|
||
load_mobile_app_config.fetch("expo").fetch("android").fetch("package")
|
||
end
|
||
|
||
# The V3.0 signer is the one Android checks for updates; V3.2 (minSdk 37 PQC) and the source stamp also print digests.
|
||
def verify_play_signed_apk(apk)
|
||
apksigner = Dir[File.join(ENV.fetch("ANDROID_HOME"), "build-tools", "*", "apksigner")].max_by do |path|
|
||
Gem::Version.new(File.basename(File.dirname(path)))
|
||
end
|
||
UI.user_error!("apksigner not found under ANDROID_HOME") unless apksigner
|
||
output = sh(apksigner, "verify", "--print-certs", apk, log: false)
|
||
digests = output.scan(/^V3\.0 Signer: certificate SHA-256 digest: (\h+)$/).flatten.uniq
|
||
expected = PLAY_APP_SIGNING_CERT_SHA256.delete(":").downcase
|
||
unless digests == [expected]
|
||
UI.user_error!("#{apk} V3.0 signer #{digests.inspect} is not Google's Play app signing key #{expected}")
|
||
end
|
||
UI.success("#{apk} is signed with Google's Play app signing key")
|
||
if ENV["GITHUB_OUTPUT"]
|
||
File.write(ENV["GITHUB_OUTPUT"], "cert_sha256=#{PLAY_APP_SIGNING_CERT_SHA256}\n", mode: "a")
|
||
end
|
||
end
|
||
|
||
# Play Console's automatic protection injects com.pairip's license check into every APK Play generates.
|
||
# It closes the app wherever Play is disabled or unreachable (mainland China), and the GitHub APK is
|
||
# meant for exactly those phones (#42973), so refuse to publish a protected APK.
|
||
PLAY_LICENSE_CHECK_CLASS = "Lcom/pairip/licensecheck/".b.freeze
|
||
|
||
def reject_play_license_check(apk)
|
||
require "zip"
|
||
protected_dex = Zip::File.open(apk) do |zip|
|
||
zip.glob("classes*.dex").find { |entry| entry.get_input_stream.read.include?(PLAY_LICENSE_CHECK_CLASS) }
|
||
end
|
||
return unless protected_dex
|
||
UI.user_error!(
|
||
"#{apk} carries Play's automatic-protection license check (#{protected_dex.name}), which closes the app " \
|
||
"when Google Play is disabled or unreachable. Turn off automatic protection in Play Console " \
|
||
"(Test and release > App integrity), then release a new versionCode.",
|
||
)
|
||
end
|
||
|
||
def write_mobile_app_config(config)
|
||
File.write(APP_CONFIG_PATH, "#{JSON.pretty_generate(config)}\n")
|
||
end
|
||
|
||
def current_mobile_version(config)
|
||
config.fetch("expo").fetch("version")
|
||
end
|
||
|
||
def testflight_changelog
|
||
changelog = ENV.fetch("TESTFLIGHT_CHANGELOG", "").strip
|
||
changelog.empty? ? DEFAULT_TESTFLIGHT_CHANGELOG : changelog
|
||
end
|
||
|
||
# Older spaceship versions lack app_version_state; either field can prove the train closed.
|
||
def closed_state?(app_store_version)
|
||
states = [app_store_version.app_store_state]
|
||
states << app_store_version.app_version_state if app_store_version.respond_to?(:app_version_state)
|
||
|
||
states.compact.any? { |state| CLOSED_APP_STORE_STATES.include?(state) }
|
||
end
|
||
|
||
# Fetch once across all versions: TestFlight-only versions have no App Store record, yet a higher
|
||
# approved version closes them too (90062). Nil app/API errors preserve the prior upload behavior.
|
||
def highest_closed_app_store_version(app)
|
||
return nil unless app
|
||
|
||
closed = app
|
||
.get_app_store_versions
|
||
.select { |app_store_version| closed_state?(app_store_version) }
|
||
.map(&:version_string)
|
||
|
||
IosReleaseVersion.max_version(closed)
|
||
rescue StandardError => error
|
||
# Surface lookup errors because assuming open disables the 90186 guard.
|
||
UI.error("Could not determine closed App Store versions (#{error.message}); assuming open and proceeding.")
|
||
nil
|
||
end
|
||
|
||
platform :ios do
|
||
desc "Resolve the iOS release version and next TestFlight build number"
|
||
lane :prepare_release_version do |options|
|
||
api_key = app_store_connect_api_key_from_env
|
||
config = load_mobile_app_config
|
||
|
||
app =
|
||
begin
|
||
Spaceship::ConnectAPI::App.find(BUNDLE_ID)
|
||
rescue StandardError => error
|
||
UI.error("Could not look up App Store app #{BUNDLE_ID} (#{error.message}); skipping closed-train check.")
|
||
nil
|
||
end
|
||
highest_closed = highest_closed_app_store_version(app)
|
||
UI.message("Highest closed App Store version: #{highest_closed || 'none'}")
|
||
|
||
version =
|
||
begin
|
||
IosReleaseVersion.resolve(
|
||
requested: options[:version],
|
||
bump_patch: options[:bump_patch],
|
||
current_version: current_mobile_version(config),
|
||
train_closed: ->(candidate) { IosReleaseVersion.closed_train?(candidate, highest_closed) },
|
||
)
|
||
rescue ArgumentError => error
|
||
UI.user_error!(error.message)
|
||
end
|
||
|
||
# Patch bumps skip closed trains because workflow-only releases can outpace Git; explicit/current versions fail fast.
|
||
if IosReleaseVersion.closed_train?(version, highest_closed)
|
||
retry_guidance =
|
||
if IosReleaseVersion.truthy?(options[:bump_patch])
|
||
"Use a higher release_version or land a \"Prepare mobile <version>\" commit."
|
||
else
|
||
"Re-dispatch with bump_patch_version: true (or a higher release_version), " \
|
||
"or land a \"Prepare mobile <version>\" commit."
|
||
end
|
||
UI.user_error!(
|
||
"iOS version #{version} is not higher than #{highest_closed}, which is already " \
|
||
"submitted/released on the App Store, so Apple will reject the upload. #{retry_guidance}",
|
||
)
|
||
end
|
||
|
||
latest_build_number = latest_testflight_build_number(
|
||
api_key: api_key,
|
||
app_identifier: BUNDLE_ID,
|
||
version: version,
|
||
initial_build_number: 0,
|
||
)
|
||
build_number = latest_build_number.to_i + 1
|
||
|
||
# Expo copies app.json into the native project and About-screen runtime manifest.
|
||
config.fetch("expo")["version"] = version
|
||
config.fetch("expo").fetch("ios")["buildNumber"] = build_number.to_s
|
||
write_mobile_app_config(config)
|
||
|
||
UI.message("Prepared Orca Mobile iOS #{version} (#{build_number})")
|
||
end
|
||
|
||
desc "Build, sign, and upload Orca Mobile to App Store Connect / TestFlight"
|
||
lane :build_and_upload do
|
||
api_key = app_store_connect_api_key_from_env
|
||
|
||
team_id = ENV.fetch("APPLE_TEAM_ID")
|
||
|
||
# Fetch/install an explicit API-key profile for manual archive/export.
|
||
get_provisioning_profile(
|
||
api_key: api_key,
|
||
app_identifier: BUNDLE_ID,
|
||
force: true,
|
||
)
|
||
# This fastlane exposes the chosen profile through SIGH_NAME, not SIGH_PROFILE_MAPPING.
|
||
profile_name = lane_context[SharedValues::SIGH_NAME]
|
||
|
||
# Archive needs explicit manual signing/team/profile; omitting DEVELOPMENT_TEAM fails signing.
|
||
build_app(
|
||
workspace: WORKSPACE,
|
||
scheme: SCHEME,
|
||
configuration: "Release",
|
||
export_method: "app-store",
|
||
xcargs: "DEVELOPMENT_TEAM=#{team_id} " \
|
||
"CODE_SIGN_STYLE=Manual " \
|
||
"CODE_SIGN_IDENTITY='Apple Distribution' " \
|
||
"PROVISIONING_PROFILE_SPECIFIER='#{profile_name}'",
|
||
export_options: {
|
||
teamID: team_id,
|
||
signingStyle: "manual",
|
||
provisioningProfiles: {
|
||
BUNDLE_ID => profile_name,
|
||
},
|
||
},
|
||
output_directory: BUILD_OUTPUT_DIRECTORY,
|
||
output_name: "Orca.ipa",
|
||
clean: true,
|
||
)
|
||
|
||
upload_to_testflight(
|
||
api_key: api_key,
|
||
# Separate distribution retries processing timeouts against the same binary.
|
||
skip_waiting_for_build_processing: true,
|
||
distribute_external: false,
|
||
)
|
||
end
|
||
|
||
desc "Wait for and distribute an uploaded build to external TestFlight testers"
|
||
lane :distribute_testflight do |options|
|
||
version = options[:version].to_s.strip
|
||
build_number = options[:build_number].to_s.strip
|
||
UI.user_error!("version is required") if version.empty?
|
||
UI.user_error!("build_number is required") if build_number.empty?
|
||
|
||
api_key = app_store_connect_api_key_from_env
|
||
app = Spaceship::ConnectAPI::App.find(BUNDLE_ID)
|
||
UI.user_error!("Could not find App Store Connect app #{BUNDLE_ID}") unless app
|
||
|
||
available_groups = app.get_beta_groups.map(&:name)
|
||
missing_groups = TESTFLIGHT_GROUPS - available_groups
|
||
unless missing_groups.empty?
|
||
UI.user_error!(
|
||
"Missing TestFlight group(s): #{missing_groups.join(', ')}. " \
|
||
"Create them in App Store Connect or update TESTFLIGHT_GROUPS.",
|
||
)
|
||
end
|
||
|
||
upload_to_testflight(
|
||
api_key: api_key,
|
||
app_identifier: BUNDLE_ID,
|
||
# Without an IPA to inspect, fastlane would prompt interactively for platform and fail CI.
|
||
app_platform: "ios",
|
||
app_version: version,
|
||
build_number: build_number,
|
||
distribute_only: true,
|
||
skip_waiting_for_build_processing: false,
|
||
wait_processing_timeout_duration: TESTFLIGHT_PROCESSING_TIMEOUT_SECONDS,
|
||
# Expire the superseded build in this train only; default beta review otherwise blocks submission
|
||
# ("Another build is in review", 2026-08-05/08-06).
|
||
reject_build_waiting_for_review: true,
|
||
distribute_external: true,
|
||
groups: TESTFLIGHT_GROUPS,
|
||
notify_external_testers: true,
|
||
changelog: testflight_changelog,
|
||
)
|
||
end
|
||
|
||
desc "Build, upload, and distribute Orca Mobile to external TestFlight testers"
|
||
lane :release do
|
||
build_and_upload
|
||
|
||
config = load_mobile_app_config
|
||
distribute_testflight(
|
||
version: current_mobile_version(config),
|
||
build_number: config.fetch("expo").fetch("ios").fetch("buildNumber"),
|
||
)
|
||
end
|
||
end
|
||
|
||
platform :android do
|
||
desc "Upload a release-signed AAB to Google Play's internal testing track"
|
||
lane :upload_internal do |options|
|
||
upload_to_play_store(
|
||
package_name: android_package,
|
||
json_key: ENV.fetch("ORCA_PLAY_JSON_KEY_PATH"),
|
||
track: "internal",
|
||
aab: options.fetch(:aab),
|
||
# Checks the bundle against Play and discards the edit, so nothing is released.
|
||
validate_only: options[:validate_only].to_s == "true",
|
||
skip_upload_apk: true,
|
||
skip_upload_metadata: true,
|
||
skip_upload_changelogs: true,
|
||
skip_upload_images: true,
|
||
skip_upload_screenshots: true,
|
||
)
|
||
end
|
||
|
||
desc "Download the universal APK Google Play signed for a version code and check its signer"
|
||
lane :download_signed_apk do |options|
|
||
destination = options.fetch(:destination)
|
||
attempts = 0
|
||
begin
|
||
attempts += 1
|
||
download_universal_apk_from_google_play(
|
||
package_name: android_package,
|
||
json_key: ENV.fetch("ORCA_PLAY_JSON_KEY_PATH"),
|
||
version_code: Integer(options.fetch(:version_code)),
|
||
destination: destination,
|
||
certificate_sha256_hash: PLAY_APP_SIGNING_CERT_SHA256,
|
||
)
|
||
rescue StandardError => e
|
||
# Play generates the APK a little after the upload commits.
|
||
raise if attempts >= PLAY_APK_DOWNLOAD_ATTEMPTS
|
||
UI.important("Signed APK not ready (attempt #{attempts}/#{PLAY_APK_DOWNLOAD_ATTEMPTS}): #{e.message}")
|
||
sleep(PLAY_APK_DOWNLOAD_RETRY_SECONDS)
|
||
retry
|
||
end
|
||
verify_play_signed_apk(destination)
|
||
reject_play_license_check(destination)
|
||
end
|
||
end
|