如何通过Fastlane获取Apple开发者门户证书信息及到期提醒
用Fastlane获取Apple证书到期信息并实现到期通知方案
Fastlane完全可以实现你的需求,它底层通过spaceship库与Apple开发者门户交互,能直接获取所有证书的到期信息。以下是具体实现方案:
一、Fastlane实现步骤
1. 准备工作
确保你已经配置好Fastlane环境,并且通过fastlane spaceauth完成Apple开发者账号的登录验证(避免后续lane运行时需要手动输入验证码)。
2. 编写Fastlane Lane
在项目的Fastfile中添加以下lane:
lane :check_cert_expiry do # 连接到Apple开发者门户 Spaceship::Portal.login("你的Apple ID邮箱") Spaceship::Portal.select_team # 获取所有证书(包含开发、发布等所有类型) certificates = Spaceship::Portal.certificate.all certificates.each do |cert| expiry_date = cert.expires.strftime("%Y-%m-%d") days_remaining = (cert.expires - Date.today).to_i # 打印证书基础信息(可选,用于调试) puts "证书名称: #{cert.name}" puts "证书类型: #{cert.type_display_name}" puts "到期日期: #{expiry_date}" puts "剩余有效天数: #{days_remaining}天" puts "---" # 当剩余天数≤30时发送邮件通知 if days_remaining <= 30 mail( to: ["收件人邮箱1", "收件人邮箱2"], subject: "⚠️ Apple证书即将到期: #{cert.name}", body: "【证书到期提醒】\n证书名称:#{cert.name}\n证书类型:#{cert.type_display_name}\n到期日期:#{expiry_date}\n剩余有效天数:#{days_remaining}天\n请及时更新证书,避免影响项目打包发布。", # 配置SMTP信息,也可以通过环境变量设置 smtp: { address: "smtp.example.com", port: 587, user_name: "发件人邮箱", password: "发件人邮箱密码/授权码", authentication: "plain", enable_starttls_auto: true } ) end end end
3. 运行与集成
- 直接运行:在终端执行
fastlane check_cert_expiry即可触发检查和通知。 - CI/CD集成:可以把这个lane加到定时任务(比如Jenkins定时构建、GitHub Actions的schedule触发器),实现自动定期检查。
二、替代方案:直接调用Apple Developer API
如果你不想依赖Fastlane,也可以直接调用Apple官方的开发者API来获取证书信息,步骤如下:
1. 准备API密钥
在Apple开发者门户的「Certificates, Identifiers & Profiles」→「API Keys」中创建一个API密钥,确保勾选「Certificates」的读取权限,下载.p8格式的私钥文件,并记录Key ID和Issuer ID。
2. 编写脚本示例(Python)
import requests import jwt import datetime import smtplib from email.mime.text import MIMEText # 配置API密钥信息 KEY_ID = "你的Key ID" ISSUER_ID = "你的Issuer ID" PRIVATE_KEY_PATH = "你的.p8私钥文件路径" # 生成JWT令牌(用于API身份验证) def generate_auth_token(): with open(PRIVATE_KEY_PATH, "r") as f: private_key = f.read() payload = { "iss": ISSUER_ID, "exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=20), "aud": "appstoreconnect-v1" } return jwt.encode(payload, private_key, algorithm="ES256", headers={"kid": KEY_ID}) # 获取所有证书信息 def get_all_certificates(): token = generate_auth_token() api_url = "https://api.appstoreconnect.apple.com/v1/certificates" headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json" } response = requests.get(api_url, headers=headers) if response.status_code == 200: return response.json() else: print(f"API请求失败,状态码: {response.status_code}") return None # 发送邮件通知 def send_expiry_alert(cert_name, expiry_date, days_remaining): msg_content = f"【Apple证书到期提醒】\n证书名称:{cert_name}\n到期日期:{expiry_date}\n剩余有效天数:{days_remaining}天\n请及时更新证书,避免影响项目打包发布。" msg = MIMEText(msg_content, "plain", "utf-8") msg["Subject"] = f"⚠️ Apple证书即将到期: {cert_name}" msg["From"] = "发件人邮箱" msg["To"] = "收件人邮箱" # 配置SMTP服务器 with smtplib.SMTP("smtp.example.com", 587) as server: server.starttls() server.login("发件人邮箱", "发件人邮箱密码/授权码") server.send_message(msg) # 主逻辑 if __name__ == "__main__": certs_data = get_all_certificates() if not certs_data: exit() today = datetime.date.today() for cert in certs_data["data"]: attrs = cert["attributes"] cert_name = attrs["name"] expiry_date_str = attrs["expirationDate"] expiry_date = datetime.datetime.strptime(expiry_date_str, "%Y-%m-%dT%H:%M:%SZ").date() days_remaining = (expiry_date - today).days print(f"证书名称: {cert_name}") print(f"到期日期: {expiry_date}") print(f"剩余天数: {days_remaining}天") print("---") if days_remaining <= 30: send_expiry_alert(cert_name, expiry_date, days_remaining)
总结
- Fastlane方案更适合已经在使用Fastlane做打包发布的项目,集成成本低,无需额外配置API密钥。
- Apple Developer API方案更灵活,不依赖第三方工具,适合需要自定义逻辑的场景。
内容的提问来源于stack exchange,提问作者Aditya pratap singh
相关产品推荐
相关产品推荐

