使用fastlane 實現(xiàn)ipa自動打包腳本

對于一個iOS APP的發(fā)布上線,一般來說都需要經(jīng)歷:編譯打包 -> 截圖 -> 填寫一些說明文字 -> 上傳ipa到itunes connect -> 提交供審核。每次都要進(jìn)行這么多“繁瑣”的步驟,對于某些步驟可能一次還不能執(zhí)行成功需要等著界面提示上傳錯誤然后手動重新再來一次(想想都覺得可怕)。
在日常開發(fā)中,打包也是最后上線不可缺少的環(huán)節(jié),如果需要生成ipa文件通常需要在Xcode里點擊Product -> Archive,然后在彈出來的Organizer中選擇導(dǎo)出什么類型(ad hoc/enterprise)的包。對于大項目來說動輒編譯十分鐘以上的來說,一天打幾個包就差不多過去了。
為了解決這些問題,Felix Krause大神寫了一個工具集fastlane。fastlane是一套使用Ruby寫的自動化工具集,用于iOS和Android的自動化打包、發(fā)布等工作。

前言

最近用shell打包ipa發(fā)現(xiàn)終端總是提示
shell error: exportArchive: "***.app" requires a provisioning profile.
把配置文件刪除重新下載也一樣,所以索性重新?lián)Q用另一種腳本工具來打包ipa,發(fā)現(xiàn)這個還是挺好用的

介紹

配置環(huán)境

  • 首先要安裝正確的 Ruby 版本。在終端窗口中用下列命令來確認(rèn):
    ruby -v

  • 如果沒有安裝,則輸入命令安裝gym:
    sudo gem install gym

  • 確保Xcode命令行工具安裝最新版本,使用如下命令進(jìn)行安裝:
    xcode-select --install

  • 以上依賴配置好之后就可以通過 rubygem 進(jìn)行安裝fastlane:
    sudo gem install fastlane

  • 完成安裝

fastlane實戰(zhàn)

初始化

  • 打開終端,cd到你的工程目錄,然后執(zhí)行fastlane init命令開始初始化

  • 在執(zhí)行的過程中會要求填寫一些項目的資料,如Apple ID等,fastlane會自動檢測當(dāng)前目錄下項目的App Name和App Identifier,可以選擇自行輸入這些信息。初始化完成會在當(dāng)前目錄下面生成一個fastlane的文件夾。

  • 最重要的兩個文件就是Appfile和Fastfile,主要的說明如下

    • Appfile里面存放了App的基本信息包括app_identifier、apple_id、team_id等,如果在init的時候輸入了正確的apple_id和密碼會自動獲取team_id。

    • Fastfile是最重要的一個文件,在這個里面可以編寫和定制我們的自動化腳本,所有的流程控制功能都寫在這個文件里面。

fastfile 文件

Fastfile管理你所創(chuàng)建的 lane ,了解詳情。它的格式是這樣的:
  lane :inHouse do
  gym(scheme: "XXX",
      export_method:"enterprise",
      output_directory "./build", # 打包后的 ipa 文件存放的目錄
      output_name "XXX"  # ipa 文件名
   )
  end

我的用法

fastfile文件里主要修改四個地方內(nèi)容

  1. platform :ios do(安卓,iOS都可以用)

2.desc "ad_Hoc 版本"(對lane的描述,fastlane會自動將desc的內(nèi)容生成說明文檔)

  1. lane :beta do (定義一個lane(任務(wù)),可以理解為一個函數(shù),我們在執(zhí)行的時候使用fastlane lane名稱,比如 cd到項目根目錄,然后 fastlane beta )

  2. 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)

結(jié)果

這次我只是用來打包測試,所以

# 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

接下來我們開始進(jìn)階教程,將打包好的ipa上傳到fir
在Fastfile文件里寫入:

# 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 "開始打包-內(nèi)測版--開發(fā)證書 - dev"
    #內(nèi)測版--開發(fā)證書
 lane :adhoc do 
    #開始打包
    puts "開始打包-內(nèi)測版--開發(fā)證書 - 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 "開始打包  --  企業(yè)公測版--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 -T  fir的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能做的事情還有很多,大家可以去好好看看文檔,研究一些高級的用法吧!

最后

腳本雖好用,但是我們在除了能知道如何使用以外,也應(yīng)該去深入了解其中的一些原理,做工具的主人而不是工具的奴隸

參考文檔:

  1. 使用fastlane gym實現(xiàn)ipa自動打包腳本
  2. iOS中使用Fastlane實現(xiàn)自動化打包和發(fā)布
  3. iOS中fastlane的使用
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容