iOS 用fastlane进行团队证书管理
团队开发中,经常会遇到新成员加入或者证书及配置文件的变更,使用fastlane进行统一管理配置,能很好解决证书冲突等问题
文章的最终的效果==>使用一个终端命令配置好一个项目所需要的所有证书及配置文件
关于fastlane的基本介绍及使用可以看这里:fastlane的基本使用(自动化打包发布)
在使用fastlane管理证书前,要先注册一个私有的仓库,若有私有服务器则放在服务器上即可
下面以码云作为管理仓库
安装及基本使用
若没有安装fastlane,则先执行
sudo gem install fastlane
进行安装
在项目目录下,执行fastlane init
初始化项目后,
执行fastlane match init
创建match文件,并在在Git Repo处输入私有仓库的地址
![](https://img.haomeiwen.com/i2100810/430627637dba93f7.png)
执行完成后,打开Matchfile文件
git_url("https://gitee.com/xxxx/xxxxxxx.git") //此处为刚输入的证书存放仓库
type("development") # 默认match所同步的类型,可不管
app_identifier("bundle Id") #bundleId,若同工程下有多个,则用["bundleId1","bundleId2"]
username("user@fastlane.tools") #苹果开发者账号
# For all available options run `fastlane match --help`
# Remove the # in the beginning of the line to enable the other options
在工程目录下执行fastlane match development/adhoc/appstore
来上传/获取对应证书及配置文件
![](https://img.haomeiwen.com/i2100810/e6fc0851a300cb75.png)
首次执行时,会要求输入一个密码,用来对证书进行加密,后续其他机器获取证书时使用该密码进行解密,输入密码后继续按照终端提示进行下一步操作,注意,此时会自动在Apple Developer中生成新的证书及配置文件来进行使用
![](https://img.haomeiwen.com/i2100810/b53914bc71c749f9.png)
完成后,git仓库就会生成对应的certs及profiles文件夹来存放证书和配置文件
![](https://img.haomeiwen.com/i2100810/60ba8b2b69b69556.png)
当有新成员加入时,则执行
fastlane match development/adhoc/appstore --readonly
,并输入对应的加密密码来获取
证书复用问题
实际开发过程中,项目的证书及配置文件都是已经建立好的,下面介绍如何使用已有的证书和配置文件来进行团队证书管理
创建ruby.rb文件,将以下代码复制进去,替换掉注释部分
require 'spaceship'
Spaceship.login('xxxxxx@xxx.com') #输入对应的苹果账号
Spaceship.select_team
Spaceship.certificate.all.each do |cert|
cert_type = Spaceship::Portal::Certificate::CERTIFICATE_TYPE_IDS[cert.type_display_id].to_s.split("::")[-1]
puts "Cert id: #{cert.id}, name: #{cert.name}, expires: #{cert.expires.strftime("%Y-%m-%d")}, type: #{cert_type}"
end
终端执行ruby ruby.rb
查找出现在已有的证书,并记录下等下要用到的Cert id
![](https://img.haomeiwen.com/i2100810/eef4e9c1625d7bfc.png)
在git仓库创建certs及profiles文件夹,如下图所示,区分好对应的类型
![](https://img.haomeiwen.com/i2100810/c3742e29b4a0c34e.png)
接着从Apple Developer中下载现有的证书及mobileprovision文件,将证书导入到钥匙中,并生成p12文件
- 执行
openssl pkcs12 -nocerts -nodes -out key.pem -in {证书}.p12
生成.pem文件 - 执行
openssl aes-256-cbc -k {密码} -in key.pem -out {cert_id}.p12 -a
生成加密后的p12 - 执行
openssl aes-256-cbc -k {密码} -in {证书}.cer -out {cert_id}.cer -a
生成加密的证书
其中cert_id为前面执行ruby文件所找到的证书id
将加密后的证书及P12放入git仓库的certs目录对应的类型下,此时再执行fastlane match development/adhoc/appstore
即会从git仓库中获取现有的证书和配置,这样就达到了整个开发团队保持同样的证书和配置
mobileprovision同理从Apple Developer上下载后,用同样方式加密(取名为{Development/ADHoc/AppStore/InHouse}_bundleId.mobileprovision)放入git仓库的profiles对应目录下,例如
openssl aes-256-cbc -k vanke -in xxxx.mobileprovision -out Development_yyyy -a
最后的最后
上传完所有的证书及配置文件后,可在Fastfile文件中创建一个lane专门用来加载所有的证书和配置文件
platform :ios do
...
#证书 替换bundle id
lane :cer do
match(type: "development", app_identifier: "bundle id", force_for_new_devices: true, readonly: true)
match(type: "adhoc", app_identifier: "bundle id", force_for_new_devices: true, readonly: true)
match(type: "enterprise", app_identifier: "bundle id", readonly: true)
end
...
当项目有新成员加入时,执行fastlane cer
即可同步证书,搭配fastlane打包上传使用口感更加哦~
常见问题
- 其他成员git时提示账号密码错误或者git请求时间过长
更改Matchfile文件,git_url处的地址带上账号和密码,例如账号123456@qq.com,密码111111,则地址为git_url("https://123456@qq.com:111111@gitee.com/user/project.git")
,注意特殊符号和中文要进行url encode