使用 Fastlane 實現 iOS 跟 Android 自動打包指令碼
對於一個iOS APP的釋出上線,一般來說都需要經歷:編譯打包
-> 截圖
-> 填寫一些說明文字
-> 上傳ipa到itunes connect
-> 提交供稽核
。每次都要進行這麼多“繁瑣”的步驟,對於某些步驟可能一次還不能執行成功需要等著介面提示上傳錯誤然後手動重新再來一次(想想都覺得可怕)。
在日常開發中,打包也是最後上線不可缺少的環節,如果需要生成ipa檔案通常需要在Xcode裡點選Product
-> Archive
,然後在彈出來的Organizer
中選擇匯出什麼型別(ad hoc
/enterprise
)的包。對於大專案來說動輒編譯十分鐘以上的來說,一天打幾個包就差不多過去了。
為了解決這些問題,ofollow,noindex">Felix Krause
大神寫了一個工具集fastlane
。fastlane 是一套使用Ruby寫的自動化工具集,用於 iOS 和 Android 的自動化打包、釋出等工作。
前言
最近用shell打包ipa發現終端總是提示:
shell error: exportArchive: "***.app" requires a provisioning profile.
把配置檔案刪除重新下載也一樣,所以索性重新換用另一種指令碼工具來打包ipa,發現這個還是挺好用的
介紹
-
Fastlane是一套使用Ruby寫的自動化工具集,用於 iOS 和 Android 的自動化打包、釋出等工作,可以節省大量的時間。
-
Github:github.com/fastlane/fa…
官網:fastlane.tools
文件:docs.fastlane.tools
配置環境
-
首先要安裝正確的 Ruby 版本。在終端視窗中用下列命令來確認:
ruby -v
-
如果沒有安裝,則輸入命令安裝 gym:
sudo gem install gym
-
確保Xcode命令列工具安裝最新版本,使用如下命令進行安裝:
xcode-select --install
-
以上依賴配置好之後就可以通過 rubygem 進行安裝 fastlane:
sudo gem install fastlane
-
完成安裝
fastlane實戰
初始化
-
開啟終端,cd到你的工程目錄,然後執行 fastlane init 命令開始初始化
-
在執行的過程中會要求填寫一些專案的資料,如 Apple ID 等,fastlane 會自動檢測當前目錄下專案的 App Name 和 App Identifier,可以選擇自行輸入這些資訊。初始化完成會在當前目錄下面生成一個fastlane的資料夾。
-
最重要的兩個檔案就是 Appfile 和 Fastfile,主要的說明如下
-
Appfile裡面存放了App的基本資訊包括 app_identifier、apple_id、team_id 等,如果在 init 的時候輸入了正確的 apple_id 和密碼會自動獲取 team_id。
-
Fastfile 是最重要的一個檔案,在這個裡面可以編寫和定製我們的自動化指令碼,所有的流程控制功能都寫在這個檔案裡面。
fastfile 檔案
Fastfile 管理你所建立的 lane ,瞭解詳情。它的格式是這樣的:
desc "企業版" lane :inHouse do gym(scheme: "XXX", export_method:"enterprise", output_directory "./build", # 打包後的 ipa 檔案存放的目錄 output_name "XXX"# ipa 檔名 ) end 複製程式碼
我的用法
fastfile 檔案裡主要修改四個地方內容
platform :ios do desc "ad_Hoc 版本" lane :beta do gym(scheme: “專案名稱”, export_method:"app-store",output_directory: "./build",)
我一般用gym語法操作
gym(scheme: scheme_name, clean: true, export_method:'appstore', configuration: configuration, output_directory: output_directory, output_name: output_name) 複製程式碼
結果
這次我只是用來打包測試,fastlane裡的原始碼:
# This file contains the fastlane.tools configuration # You can find the documentation at https://docs.fastlane.tools # For a list of all available actions, check out`` # #https://docs.fastlane.tools/actions # # For a list of all available plugins, check out # #https://docs.fastlane.tools/plugins/available-plugins # # Uncomment the line if you want fastlane to automatically update itself # update_fastlane # 指定打包所使用的輸出方式,目前支援 app-store, package, ad-hoc, enterprise, development, 和developer-id default_platform(:ios) platform :ios do desc "ad_Hoc 版本" lane :beta do gym(scheme: "***", export_method:"ad-hoc", output_directory: "./build",#檔案路徑 ) end end 複製程式碼
接下來我們開始進階教程,將打包好的 ipa 上傳到 fir 在Fastfile檔案裡寫入:
# This is the minimum version number required. # Update this, if you use features of a newer version # 指定打包所使用的輸出方式,目前支援 app-store, package, ad-hoc, enterprise, development, 和developer-id default_platform :ios platform :ios do desc "開始打包-內測版--開發證書 - dev" #內測版--開發證書 lane :adhoc do #開始打包 puts "開始打包-內測版--開發證書 - dev" gym( export_method:"ad-hoc", output_directory:"/Users/weiyuxiang/Desktop/Order/build",# 打包後的 ipa 檔案存放的目錄 ) #使用fir-cli上傳ipa sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T fir的token" end desc "開始打包--企業公測版--hoc" lane :inhoc do gym( export_method:"app-store", output_directory:"/Users/weiyuxiang/Desktop/Order/build", ) #使用fir-cli上傳ipa sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -Tfir的token" end end 複製程式碼
在測試的時候用
fastlane adhoc
上架則用
fastlane inhoc
到這裡一般沒有問題,但是樓主我還會遇到一個問題,最後用fir上傳的時候會報這些錯誤:
/Users/weiyuxiang/.rvm/rubies/ruby-2.2.4/lib/ruby/gems/2.2.0/gems/fastlane-2.95.0/fastlane_core/lib/fastlane_core/ui/interface.rb:153:in `shell_error!': [!] Exit status of command 'fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T a5bd6574b7292220d5c4b44b6' was 1 instead of 0. (FastlaneCore::Interface::FastlaneShellError) /Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:458:in `block in replace_bin_path': can't find executable fir for gem fir-cli. fir-cli is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception) from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:478:in `block in replace_bin_path' from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/fir:22:in `<main>' from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `eval' from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `<main>' 複製程式碼
按照錯誤提示,在 Gemfile 檔案里加一句:gem "fir"
即可
Fastlane 能做的事情還有很多,大家可以去好好看看Fastlane文件,研究一些高階的用法吧!