iOS 用fastlane進行團隊證書管理
團隊開發中,經常會遇到新成員加入或者證書及配置檔案的變更,此時往往會造成一堆證書/pp檔案失效問題,故我們可以使用fastlane的match進行統一管理配置
我們的目標 ---> 使用一個終端命令配置好一個專案所需要的所有證書及配置檔案,從此媽媽再也不用擔心我證書失效了
關於fastlane的基本介紹及使用可以看這裡: ofollow,noindex">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("[email protected]")#蘋果開發者賬號 # 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('[email protected]') #輸入對應的蘋果賬號 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 openssl aes-256-cbc -k {密碼} -in key.pem -out {cert_id}.p12 -a openssl aes-256-cbc -k {密碼} -in {證書}.cer -out {cert_id}.cer -a
將加密後的證書及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打包上傳使用口感更加哦~