前言
團隊開發中,經常會遇到新成員加入或者證書及配置文件的變更,此時往往會造成一堆證書/pp文件失效問題,故我們可以使用fastlane的match進行統一管理配置
我們的目標 ---> 使用一個終端命令配置好一個項目所需要的所有證書及配置文件,從此媽媽再也不用擔心我證書失效了
關于fastlane的基本介紹及使用可以看這里:fastlane的基本使用(自動化打包發布)
在使用fastlane管理證書前,要先注冊一個私有的倉庫,若有私有服務器則放在服務器上即可
本編文章以碼云作為管理倉庫
match
match是fastlane的一個功能組件, 能自動從蘋果官方上下載證書和pp文件同步到我們的git倉庫中
安裝及初始化
默認項目已經進行fastlane初始化,打開Matchfile文件
cd到當前項目文件下,并執行
[sudo] gem install match
fastlane match init
執行成功后,會在fastlane文件夾下生成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
刪除舊證書和pp文件
如果當前項目已經存在證書和pp文件,要先在官網上將證書和pp文件全部刪除,也可以執行以下命令來刪除
fastlane match nuke development
fastlane match nuke distribution
生成證書和pp文件
在工程目錄下分別執行
fastlane match development
fastlane match adhoc
fastlane match appstore
首次執行時,會要求輸入一個密碼,用來對證書進行加密,后續其他機器獲取證書時使用該密碼進行解密,輸入密碼后繼續按照終端提示進行下一步操作,注意,此時會自動在Apple Developer中生成新的證書及配置文件來進行使用
完成后,git倉庫就會生成對應的certs及profiles文件夾來存放證書和配置文件
團隊管理
當有新成員加入時,執行
fastlane match development --readonly
fastlane match adhoc --readonly
fastlane match appstore --readonly
并輸入對應的加密密碼來獲取
xcode要取消Automatically manage signing
,并將獲取到的pp文件放到對應的Signing配置中,至此就配置完成啦!
手動上傳證書和profile文件(不推薦)
實際開發過程中,項目的證書及配置文件都是已經建立好的,可能會由于種種原因無法進行刪除重新配置,下面介紹如何使用已有的證書和配置文件來進行團隊證書管理
創建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
在git倉庫創建certs及profiles文件夾,如下圖所示,區分好對應的類型
接著從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 :match_all do
sh "fastlane match development --readonly"
sh "fastlane match adhoc --readonly"
sh "fastlane match appstore --readonly"
end
...
當項目有新成員加入時,執行fastlane match_all
即可同步證書,搭配fastlane打包上傳使用口感更加哦~
常見問題
其他成員git時提示賬號密碼錯誤或者git請求時間過長
可更改Matchfile文件,git_url處的地址帶上賬號和密碼,例如賬號123456@qq.com,密碼111111,則地址為git_url("https://123456@qq.com:111111@gitee.com/user/project.git")
,注意特殊符號和中文要進行url encode如果團隊不是公用同一個開發者賬號進行開發,而是通過添加開發者的方式,development證書會在每次同步是變成新的開發者對應證書,建議development證書不用同步,本地管理就好
參考:
Simplify your life with fastlane match
A new approach to code signing