Files
tra-app/devops/iso/新的/upload_to_appstore.py
T
2025-11-10 10:00:46 +08:00

144 lines
5.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
import os
import subprocess
import sys
import tempfile
# 颜色定义
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
NC = '\033[0m' # 无颜色
def run_command(command, capture_output=False):
"""执行系统命令并返回结果"""
try:
result = subprocess.run(
command,
shell=True,
check=True,
stdout=subprocess.PIPE if capture_output else None,
stderr=subprocess.STDOUT if capture_output else None,
text=True
)
return result
except subprocess.CalledProcessError as e:
if capture_output:
return e # 返回错误信息供后续分析
print(f"{RED}❌ 命令执行失败: {e.output}{NC}")
sys.exit(e.returncode)
def main():
# 检查参数
if len(sys.argv) < 2:
print(f"{RED}错误:参数不足!{NC}")
print(f"使用方法:{YELLOW}{sys.argv[0]} <target_branch>{NC}")
print(f"示例:{YELLOW}{sys.argv[0]} sit{NC}")
sys.exit(1)
# 解析参数(第一个参数为target_branch
target_branch = sys.argv[1]
# ==============================================
# 配置变量 - 根据实际情况修改以下参数
# ==============================================
API_KEY = "ACRB9XFL9G" # App Store Connect API 密钥ID
API_ISSUER = "69a6de7f-3378-47e3-e053-5b8c7c11a4d1" # API 密钥对应的Issuer ID
APP_TYPE = "ios" # 应用类型(固定为ios
# 关键修改:根据target_branch拼接IPA路径
build_dir = f"{os.path.expanduser('~')}/Desktop/lx_uniapp/build" # 构建输出目录
ipa_dir_name = f"{target_branch}_ipa_production" # 带分支名的IPA目录
IPA_PATH = os.path.join(build_dir, ipa_dir_name, "HBuilder.ipa") # 固定IPA文件名
# 显示参数信息
print(f"{YELLOW}===== 上传配置信息 ====={NC}")
print(f"▶ 目标分支: {YELLOW}{target_branch}{NC}")
print(f"▶ IPA文件路径: {YELLOW}{IPA_PATH}{NC}\n")
# ==============================================
# 验证IPA文件是否存在
# ==============================================
if not os.path.isfile(IPA_PATH):
print(f"{RED}❌ 错误:未找到IPA文件!{NC}")
print(f" 查找路径:{IPA_PATH}")
print(f" 请确认该分支已成功构建IPA文件")
sys.exit(1)
# ==============================================
# 1. 验证IPA文件合法性
# ==============================================
print(f"{YELLOW}📋 开始验证IPA文件...{NC}")
print(f" 文件路径:{IPA_PATH}")
# 创建临时文件保存输出
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as tmp_file:
tmp_filename = tmp_file.name
# 执行验证命令
validate_cmd = (
f"xcrun altool --validate-app "
f"--type '{APP_TYPE}' "
f"--file '{IPA_PATH}' "
f"--apiKey '{API_KEY}' "
f"--apiIssuer '{API_ISSUER}' > '{tmp_filename}' 2>&1"
)
# 执行命令但不立即退出(需要处理特定错误)
result = run_command(validate_cmd, capture_output=True)
# 读取并显示验证输出
with open(tmp_filename, 'r') as f:
validate_output = f.read()
print(validate_output)
os.unlink(tmp_filename) # 清理临时文件
# 处理验证结果
if isinstance(result, subprocess.CalledProcessError):
# 检查是否是版本号重复错误
if "The bundle version must be higher than the previously uploaded version" in validate_output:
# 提取之前的版本号
previous_version = None
if "previousBundleVersion = " in validate_output:
previous_version = validate_output.split("previousBundleVersion = ")[1].split()[0]
elif "version: " in validate_output:
previous_version = validate_output.split("version: ")[1].split("")[0]
print(f"{RED}❌ 错误:版本号重复{NC}")
if previous_version:
print(f"{RED} 已上传的最新版本号为: {previous_version}{NC}")
try:
print(f"{RED} 请将版本号修改为 {int(previous_version) + 1} 或更高后重试{NC}")
except ValueError:
print(f"{RED} 请将版本号修改为更高版本后重试{NC}")
else:
print(f"{RED} 请将版本号修改为更高版本后重试{NC}")
sys.exit(result.returncode)
else:
print(f"{RED}❌ IPA文件验证失败,请查看上方错误信息{NC}")
sys.exit(result.returncode)
else:
print(f"{GREEN}✅ IPA文件验证成功,符合上传要求{NC}")
# ==============================================
# 2. 上传IPA文件到App Store Connect
# ==============================================
print(f"\n{YELLOW}🚀 开始上传IPA文件...{NC}")
print(f" 文件路径:{IPA_PATH}")
upload_cmd = (
f"xcrun altool --upload-app "
f"--type '{APP_TYPE}' "
f"--file '{IPA_PATH}' "
f"--apiKey '{API_KEY}' "
f"--apiIssuer '{API_ISSUER}'"
)
run_command(upload_cmd)
# 上传成功提示
print(f"\n{GREEN}🎉 上传成功!可在App Store Connect中查看构建版本{NC}")
print(f" 提示:构建版本处理需要几分钟,请耐心等待")
if __name__ == "__main__":
main()