fastlane 入門使用
- 2019 年 11 月 8 日
- 筆記

這次以 fastlane 為例,嘗試項目中有什麼事情可以被自動完成。
fastlane 是 Ruby scripts 的集合,安裝方法不多說了見 官網文檔。
fastlane 中有但不限於以下工具集:
- produce 同時在 Apple Developer Portal 和 App Store Connect 中創建新的 iOS apps。
- cert 自動創建和維護 iOS 簽名證書。
- sigh 創建,更新,下載和修復配置文件。
- snapshot 自動在每台設備上獲取 iOS 應用的本地化螢幕截圖。
- frameit 將您的螢幕截圖放入正確的設備框架中。
- gym 構建和打包您的 iOS apps。
- deliver 將截圖,元數據和您的應用上傳到 App Store。
- pem 自動生成並更新推送通知配置文件。
- spaceship 一個 Ruby 庫能夠訪問蘋果開發者中心和應用商店連接 api。
- pilot 自動化 TestFlight 部署並管理測試用戶。
- boarding 邀請 beta 測試人員。
- match 使用 Git 同步整個團隊的證書和配置文件。
- scan 運行 app 測試。
實驗環境:Xcode 10.1、Swift 4.2、fastlane 2.116.1、$99 開發者賬戶
項目添加 fastlane
在項目根目錄下:
fastlane init
在出現 What would you like to use fastlane for?
是選擇 4 Manual setup
,在根據提示回車確認。
之後項目文件中將新增:
Gemfile
這個文件包含了 fastlane gem 的依賴fastlane
這個文件夾包含Appfile
存儲 app identifier,Apple ID 以及 fastlane 設置 app 所需的任何其他標識資訊Fastfile
管理 lanes 創建的可被調用的 actions
創建 App
打開 Fastfile
將裡面所有內容替換為:
default_platform(:ios) platform :ios do # 1 lane 的描述 desc "Create app on Apple Developer and App Store Connect sites" # 2 create_app 是這個 lane 的名字 lane :create_app do # 3 使用 produce action 將 app 添加到 Developer Portal 和 App Store Connect produce end end
打開 Appfile
將 apple_id
前的 # 移除,將 [[APPLE_ID]]
替換為真實 Apple ID,這樣 fastlane 將不會反覆提示你輸入。
如果 App Store Connect 和 Apple Developer Portal 可以將 apple_id
替換為:
apple_dev_portal_id("[[APPLE_DEVELOPER_ACCOUNT_USERNAME]]") itunes_connect_id("[[APP_STORE_CONNECT_ACCOUNT_USERNAME]]")
打開命令行在項目目錄輸入:
fastlane create_app
根據提示輸入密碼,之後將提示你輸入 bundle ID,以反 host 格式創建 ID,之後是 App name 最長 30 個字元。
完成後登陸 Apple Developer Center 和 App Store Connect,app 在二者已經都被創建了。
再次打開 Appfile 將 app_identifier
填寫剛才創建的 bundle ID。
生成交付文件
bundle exec fastlane deliver
根據提醒暫不使用 Swift 代替 Ruby,因為 fastlane.swift
現在為 beta。
完成後 fastlane 文件夾中新增:
metadata
這個文件夾存放了 app 大部分的 metadata 元數據Deliverfile
保存這剩餘小部分的元數據screenshots
將保存 app 截圖

metadata
文件夾中文件內容就是提交給 App Store Connect,多語言 app 可以手動創建對應的語言文件夾。
更多 deliver
參數見 這裡
自動截圖
多種設備和多語言 app 的截圖是繁雜的。
fastlane snapshot init
fastlane 文件夾中新增:Snapfile
,將裡面的所有內容替換為一下:
# 1 - 想截圖的設備列表 devices([ "iPhone 8 Plus", "iPhone SE" ]) # 2 - 支援的語言列表 languages([ 'en-US', 'fr-FR' ]) # 3 - 包含 UI Tests 的 scheme 名字 scheme("mZone Poker UITests") # 4 - 截圖存儲位置 output_directory "./fastlane/screenshots" # 5 - 是否清理之前的截圖 clear_previous_screenshots(true)
保存關閉,然後命令中執行:
fastlane snapshot init

創建 Test Target
在 Xcode 選擇 File -> New -> Target 選擇 iOS UI Testing Bundle 點擊 Next。

Product Name 輸入上面 scheme
填寫的名字 MZone Poker UITests 點擊 Finish。
之後將 fastlane 文件夾中的 SnapshotHelper.swift 拖到 mZone Poker UITests 中。
之後將 mZone_Poker_UITests.swift 中的 setUp() 和 tearDown() 移除,替換 testExample():
// 1 設置截屏和啟動 app let app = XCUIApplication() setupSnapshot(app) app.launch() // 2 點擊 Chip Count 輸入框(在 Storyboard 中的 accessibility identifier 設置 "chip count")然後輸入 10 let chipCountTextField = app.textFields["chip count"] chipCountTextField.tap() chipCountTextField.typeText("10") // 3 點擊 Big Blind 輸入框 然後輸入 100 let bigBlindTextField = app.textFields["big blind"] bigBlindTextField.tap() bigBlindTextField.typeText("100") // 4 截圖 snapshot("01UserEntries") // 5 點擊 what should i do 再進行截圖 app.buttons["what should i do"].tap() snapshot("02Suggestion")
之後創建 mZone Poker UITests scheme,點擊 run stop 右邊的按鈕選擇 Manage Schemes...

選擇 Edit Schemes...
勾選 Test
和 Run

打開 Fastfile 添加:
desc "Take screenshots" lane :screenshot do snapshot end
命令行執行:
bundle exec fastlane screenshot
完成後將自動打開:

創建 IPA 文件
首先要確保已經 target
設置 bundle identifier
和 signing identity
。
命令行執行:
fastlane gym init
打開 Gymfile
替換為以下內容:
# 1 指定 scheme scheme("mZone Poker") # 2 指定存放 .ipa 文件夾 output_directory("./fastlane/builds") # 3 從構建中排除 bitcode。Bitcode 允許 Apple 優化你的 app,但是現在排除它以提高構建速度。 include_bitcode(false) # 4 從構建中排除符號。包含符號允許 Apple 訪問應用程式的調試資訊,但是現在排除它以提高構建速度。 include_symbols(false) # 5 允許 Xcode 使用自動配置。 export_xcargs("-allowProvisioningUpdates")
打開 Fastfile
添加:
desc "Create ipa" lane :build do # 1 允許 Xcode 自動配置 enable_automatic_code_signing # 2 構建號自增(App Store Connect 要求構建號不能重複) increment_build_number # 3 創建簽名的 .ipa 文件 gym end
保存後命令行執行:
bundle exec fastlane build

上傳到 App Store Connect
使用 deliver
將截圖、元數據、.ipa 文件上傳到 App Store Connect。
替換 Deliverfile
內容:
# 1 價格為 0 則是免費應用 price_tier(0) # 2 回答 Apple 在手動提交審核時會向您呈現的問題 submission_information({ export_compliance_encryption_updated: false, export_compliance_uses_encryption: false, content_rights_contains_third_party_content: false, add_id_info_uses_idfa: false }) # 3 提供應用評級配置位置 app_rating_config_path("./fastlane/metadata/app_store_rating_config.json") # 4 提供.ipa文件位置 ipa("./fastlane/builds/mZone Poker.ipa」) # 5 將s ubmit_for_review 設置為 true 以自動提交應用以供審核 submit_for_review(true) # 6 必須在應用審核接受後手動發布應用 automatic_release(false)
在 Fastfile
中添加:
desc "Upload to App Store" lane :upload do deliver end
然後命令行執行:
bundle exec fastlane upload
完成後,登錄 App Store Connect。螢幕截圖,元數據和構建應該在那裡,等待審查。
將命令放在一起
打開 Fastfile
添加:
desc "Create app, take screenshots, build and upload to App Store" lane :do_everything do create_app screenshot build upload end
在 Deliverfile
添加:
force(true)
執行:
bundle exec fastlane do_everything
Reference:
– EOF –