前言
在日常的開發工作中給測試同學打包測試是很正常的流程,但是每次都通過Xcode archive來生成測試包然後再匯出,然後在上傳到三方測試網站(蒲公英、fir等),一次兩次感覺沒有什麼。但是一次次重新打包,之後就是全部重複無意義的工作,就會感覺很煩了,於是我想到了用自動化打包來說實現這個工作。
Fastlane是什麼?
Fastlane是一套非常實用而強大的元件,是用Ruby語言編寫的一套自動化工具集和框架,其目的主要是為iOS和Android應用程式自動化測試部署和釋出的最簡單方法。它處理所有繁瑣的任務,如生成螢幕截圖,處理程式碼簽名以及釋出應用程式。
安裝使用過程
1、安裝
使用sudo gem install fastlane -NV或是brew cask install fastlane,這裡使用gem安裝的話需要注意一下淘寶ruby映象已經不在更新,以前用過淘寶映象的小夥伴需要更換為Ruby China 映象。參考這裡,安裝過程可能需要一段時間,請耐心等待。
2、初始化Fastlane
cd到你想要自動化打包的專案下

執行 fastlane init初始化,首先你需要輸入你的開發者賬號,如果你的appleid歸屬多個開發者賬號的話根據需求選擇一個回車繼續,之後回提示輸入密碼並進行雙重認證,根據提示輸入即可。




3、設定相關配置檔案
上面幾步執行順利的話你的專案裡面會多一個fastlane資料夾,如圖所示。


我們在這裡新增一個我們自己需要的lane。
desc "打包到pgy"
lane :test do |options|
gym(
clean:true, #打包前clean專案
export_method: "development", #匯出方式
scheme:"shangshaban", #scheme
configuration: "Debug",#環境
output_directory:"./app",#ipa的存放目錄
output_name:get_build_number(),#輸出ipa的檔名為當前的build號
# Xcode9將不會允許你訪問鑰匙串裡的內容,除非設定allowProvisioningUpdates
export_xcargs:"-allowProvisioningUpdates",
# 隱藏沒有必要的資訊
silent:true,
# 手動配置證書,注意打包方式需在export_options內使用method設定,不可使用export_method
export_options: {
method:"development",
provisioningProfiles: {
"xxxxxx":"xxxxxx",
"xxxxxxx":"xxxxxxx"
},
}
)
pgyer(api_key: "xxxxx", user_key: "xxxx",update_description: options[:desc])
end
複製程式碼
蒲公英api_key、user_key獲取方式。 這裡的provisioningProfiles資料需要特別說明一下獲取方式,在xcode裡面點選product--》archive:






將上面紅框部分對應到provisioningProfiles裡面的值填進去就OK。 然後再終端輸入fastlane add_plugin pgyer安裝蒲公英相關外掛:


4、執行打包並上傳
在專案目錄下面執行fastlane test,發現提示錯誤:


