fastlane 如何用 cert 和 sigh 自动生成 iOS 代码签名证书与 Provisioning Profile
【免费下载链接】fastlane🚀 The easiest way to automate building and releasing your iOS and Android apps项目地址: https://gitcode.com/GitHub_Trending/fa/fastlane
当你要把 iOS 应用的构建放进 CI 或自动化脚本时,代码签名证书和 Provisioning Profile 是绕不开的两个文件:证书过期、Profile 失效都会让打包直接失败。fastlane 提供cert和sigh两个动作来分别解决这两件事——cert负责创建并安装代码签名证书,sigh负责创建、续期、下载并修复 Provisioning Profile。本文说明如何用这两个动作完成“证书 + Profile”的自动化生成,以及每一步如何确认结果。
适用前提:
- 拥有 Apple Developer Portal(开发者门户)账号,证书创建需要 Team Admin 权限才能创建 Distribution 证书;
- 建议运行环境为 macOS:
cert会在 macOS 上把证书导入 Keychain 并做本地安装校验,非 macOS 系统上这两步会被跳过,keychain_path、keychain_password等参数也不支持; - 登录方式二选一:Apple ID 账号密码(fastlane 的 CredentialsManager 负责管理),或 App Store Connect API Key。
准备:配置 Appfile
cert和sigh都优先从Appfile读取应用的 bundle identifier 与 Apple ID,无需每次都传参数。仓库中的 Appfile 模板(AppfileTemplate)展示了最小配置:
app_identifier("com.example.app") # 你的 App bundle identifier apple_id("you@example.com") # Apple Developer Portal 用户名如果在多个团队之间切换,还可以配置 team_id / team_name(对应cert、sigh的team_id、team_name参数)。确认某个动作支持哪些参数和环境变量,可以直接运行:
fastlane action cert fastlane action sigh第一步:用 cert 生成并安装签名证书
最基本的命令是:
fastlane certsigh/cert的文档明确指出:在 fastlane 中,cert是get_certificates的别名,sigh是get_provisioning_profile的别名(见 get_certificates 文档、get_provisioning_profile 文档)。
cert的执行逻辑(由 Runner 实现 决定)是:
- 先检查本地机器上是否已安装可用的签名证书,已存在则直接复用;
- 只有在需要新建证书时,才会创建新的私钥、创建证书签名请求(CSR)、向 Apple 请求生成证书,下载并安装到本地 Keychain。
如果 Appfile 中没写apple_id,可以在命令行直接传入 Apple ID:
fastlane cert -u cert@example.com常用参数(均可通过fastlane action cert查看,也支持对应环境变量,如CERT_DEVELOPMENT、CERT_OUTPUT_PATH):
development: true:创建 Development 证书,而不是默认的 Distribution 证书;generate_apple_certs:创建 Xcode 11 及以上使用的 Apple Development / Apple Distribution 证书,macOS + Xcode 11+ 环境下默认为true;type::指定特殊证书类型,取值mac_installer_distribution、developer_id_installer、developer_id_application、developer_id_kext;output_path(-o):证书和私钥文件的保存目录,默认.;keychain_path、keychain_password:自定义 Keychain 及其密码(仅 macOS)。
执行后如何确认成功:
- 在 macOS 上,终端会输出 “Verifying the certificate is properly installed locally...”,成功后显示
Successfully installed certificate <证书ID>;如果在 Keychain 中找不到新证书会直接报错退出; - 证书文件被写入
output_path(默认为当前目录),同时在 lane 中产生两个共享值:CERT_FILE_PATH(证书文件路径)和CERT_CERTIFICATE_ID(证书 ID); - 非 macOS 环境会提示 “Skipping verifying certificates...”,只生成证书文件而不做本地安装。
一个需要知道的边界:cert永远不会撤销你已有的证书。当证书数量达到 Apple 的上限时,cert会抛出异常,需要你先在开发者门户手动撤销旧证书才能腾出名额。
第二步:用 sigh 生成 Provisioning Profile
sigh默认针对 App Store 类型执行“创建、修复、下载”Profile 的完整流程:
fastlane sigh指定 bundle identifier 和 Apple ID 的完整写法:
fastlane sigh -a com.example.app -u you@example.com按目标用途选择 Profile 类型(三类参数互斥,不能同时开启):
fastlane sigh # 默认 App Store fastlane sigh --adhoc # Ad Hoc fastlane sigh --development # Development与cert配合时最关键的几个参数:
-o(output_path):Profile 保存目录,默认当前目录。例如fastlane sigh -o "~/Certificates/";force(-f):无论现有 Profile 状态如何都重新生成,得到最长有效期的 Profile,并且会把所有可用设备加进 Profile;-q(filename):指定生成的 Profile 文件名,必须以.mobileprovision结尾,例如fastlane sigh -a com.example.app -u you@example.com -q "myProfile.mobileprovision";--skip_install:只生成 Profile 文件,不安装到本地;-c(cert_owner_name):指定新 Profile 使用哪张证书,例如fastlane sigh -c "SunApps GmbH"。
关于证书选择有一条明确的联动规则:如果同一个 fastlane lane 中先运行了cert,sigh会自动使用cert刚生成的签名证书(cert会把证书 ID 写入SIGH_CERTIFICATE_ID供sigh使用),不需要再用-c手动指定。
执行成功后,Profile 文件(.mobileprovision)落在output_path指定的目录,lane 中可以得到SIGH_PROFILE_PATH(本次 Profile 的绝对路径)、SIGH_UUID、SIGH_NAME等共享值,供后续动作(如构建、上传)直接使用。
组合到 Fastfile:一条 lane 完成证书和 Profile
cert和sigh官方推荐的用法是在同一个 lane 中串联。在Fastfile中写入:
lane :beta do cert sigh(force: true) end然后用fastlane beta触发。这里force: true的作用是每次都重新生成 Provisioning Profile,保证sigh始终使用本地机器上刚由cert安装的那张签名证书。
已知限制与注意事项
- 文档明确建议:对于大多数项目,更推荐用 fastlane 的
match方案统一生成和维护证书与 Profile;直接组合cert+sigh适合想完全掌控签名流程、并熟悉代码签名的场景; cert无法从 Apple 开发者门户下载“已有证书的私钥”——私钥永远只存在于当初创建证书的机器上,这也是sigh依赖本地 Keychain 中证书的前提;sigh使用环境变量SIGH_CERTIFICATE(或-c)指定证书时,也可以传证书名称或过期日期;- 如果
sigh提示找不到 App Identifier,说明开发者门户里还没有这个 bundle id,需要先创建应用标识,文档指出的对应动作是produce; sigh不会触碰 Xcode 自己管理的 Profile,它只管理自己生成和下载的那一套。
完成fastlane beta(或分别运行fastlane cert和fastlane sigh)后,你的工作目录中应能看到新生成的.cer证书文件与.mobileprovisionProfile 文件,终端出现 “Successfully installed certificate” 即为整条链路跑通的标志。
【免费下载链接】fastlane🚀 The easiest way to automate building and releasing your iOS and Android apps项目地址: https://gitcode.com/GitHub_Trending/fa/fastlane
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考