fastlane + iOS 自動打包

fastlane_text.png

概述

APP自動化打包常見的主流工具有Jenkinsfastlane。Jenkins功能強大,但是需要的配置也比較多,團隊較大的可以優(yōu)先考慮,fastlane是用Ruby語言編寫的一套自動化工具集,比較輕便,配置簡單,使用起來也很方便

fastlane安裝

  • 第一步
    安裝Ruby開發(fā)環(huán)境
ruby -v
  • 第二步
    安裝Xcode命令行工具
xcode-select --install
  • 第三步
    安裝fastlane
sudo gem install -n /usr/local/bin fastlane

fastlane 配置

進入到你的iOS項目目錄下,執(zhí)行fastlane初始化命令

fastlane init

命令執(zhí)行完畢后,fastlane 給我們提示讓我們選擇需要執(zhí)行哪種操作

[?] ?? 
[?] Looking for iOS and Android projects in current directory...
[16:54:04]: Created new folder './fastlane'.
[16:54:04]: Detected an iOS/macOS project in the current directory: 'xxxx.xcworkspace'
[16:54:04]: -----------------------------
[16:54:04]: --- Welcome to fastlane ?? ---
[16:54:04]: -----------------------------
[16:54:04]: fastlane can help you with all kinds of automation for your mobile app
[16:54:04]: We recommend automating one task first, and then gradually automating more over time
[16:54:04]: What would you like to use fastlane for?
1. ??  Automate screenshots
2. ????  Automate beta distribution to TestFlight
3. ??  Automate App Store distribution
4. ??  Manual setup - manually setup your project to automate your tasks
?  
  • 第一種獲取App StoreApp預(yù)覽照片。
  • 第二種打包上傳至TestFlight工具上。
  • 第三種打包上傳到App Store。
  • 第四種自定義打包方式。
    這里我們選擇第3種 或者第4種,執(zhí)行后,會提示我們輸入 appleID 開發(fā)者賬號、密碼驗證碼等操作,然后一直到結(jié)束。

我們會在項目的根目錄看到一個fastlane的文件夾,里面包含AppfileFastfile 文件,外部還有GemfileGemfile.lock 等新增文件;

Appfile: 存儲有關(guān)開發(fā)者賬號相關(guān)信息
Fastfile: 核心文件,用于命令行調(diào)用和處理具體的流程,lane相對于一個action方法或函數(shù) 
Gemfile 類似于cocopods 的Podfile文件

Appfile 文件配置如下:

基本上通過終端輸入蘋果開發(fā)者賬號密碼等一系列操作后,這個文件的配置基本不需要修改;

app_identifier("com.****.****") # The bundle identifier of your app
apple_id("********@qq.com") # Your Apple email address

itc_team_id("*******") # App Store Connect Team ID
team_id("*******") # Developer Portal Team ID

# For more information about the Appfile, see:
#     https://docs.fastlane.tools/advanced/#appfile

Fastlane + pgyer 配置

因為這里需要用到一個第三方的應(yīng)用分發(fā)平臺蒲公英,所以需要通過fastlane添加其它插件。

fastlane add_plugin pgyer
fastlane add_plugin versioning

這里先配置一個 Release版本的打包,然后通過 pgyer 上傳的配置

 定義打包平臺
default_platform :ios

   # 參數(shù)定義
   app_scheme = "[scheme]"    #這里雙引號內(nèi)填寫自己的參數(shù) ([]只是提示)    
   app_workspace = "[project.xcworkspace]"
   app_identifier = "[app_identifier]"
   # 打包日期拼寫版本號
   dateTime = Time.new.strftime("%Y%m%d")
   version = get_version_number #獲取版本號
   buildNumber = get_build_number  # 獲取build號
   fileName = "#{version}_#{buildNumber}_#{dateTime}.ipa"

   platform :ios do
      before_all do
      git_pull
      last_git_commit
      sh "rm -f ./Podfile.lock"
      cocoapods(use_bundle_exec: false)
   end

   # 以下是Release版本的配置
   lane :release_pgyer do |options|
    
   sigh(app_identifier: app_identifier) #項目的bundle identifler    
   build_app(
      workspace: app_workspace,
      configuration: "Release",
      scheme: app_scheme,   
      silent: true, #在構(gòu)建時隱藏不必要輸出的信息
      clean: true, #在構(gòu)建前是否clean工程
      include_bitcode:false,  #是否開啟bitecode
      output_directory:"./packets" , #ipa的輸出路徑
      output_name:fileName,  #ipa的文件名(上面??定義)
      export_method: "app-store",  #打包類型
      export_options: {
         # 描述文件匹配哪個 identifier 和 profile文件名
         provisioningProfiles: { "com.*****.live" => "recoder_release_profile"}
         }
      )

      pgyer(api_key: "30ef1623**********07fbd79") # 開始上傳蒲公英及相關(guān)配置
   end

end

蒲公英API 參數(shù)的獲取地址 https://www.pgyer.com/account/api

image.png

執(zhí)行命令小試牛刀
fastlane release_pgyer

執(zhí)行結(jié)果如下:

21:42:44]: Upload success. Visit this URL to see: https://www.pgyer.com/v9GR

+------+----------------------+-------------+
|             fastlane summary              |
+------+----------------------+-------------+
| Step | Action               | Time (in s) |
+------+----------------------+-------------+
| 1    | default_platform     | 0           |
| 2    | get_version_number   | 0           |
| 3    | get_build_number     | 0           |
| 4    | last_git_commit      | 0           |
| 5    | rm -f ./Podfile.lock | 0           |
| 6    | cocoapods            | 1           |
| 7    | sigh                 | 5           |
| 8    | build_app            | 47          |
| 9    | pgyer                | 8           |
+------+----------------------+-------------+

[21:42:44]: fastlane.tools finished successfully ??

經(jīng)過一堆log日志輸出后,會顯示succes成功的提示;
在項目的 packets/路徑下,能看到我們的ipa包已經(jīng)打包成功了,ipa的命名方式是 版本號+ build號 +日期的格式;

image.png

在蒲公英平臺上可以查看我們的ipa是否上傳成功,這里我們貼一下通過 fastlane + pgyer 的方式上傳成功的截圖;

image.png

Fastlane + TestFlight 配置

除了蒲公英,我們絕大部分時候需要通過 App Connect網(wǎng)站上傳Release版本的ipa,或者通過TestFlight分發(fā)內(nèi)測包,這里我們提供一下 Fastlane + TestFlight 的配置, 當然這里還有一個坑點,就是有的蘋果開發(fā)者賬號開啟了雙重認證,需要額外做一些操作;

配置如下:

# 定義打包平臺
default_platform :ios

   # 參數(shù)定義
   app_scheme = "[****scheme]"   
   app_workspace = "****.xcworkspace"
   app_identifier = "com.****.www"
   # 打包日期拼寫版本號
   dateTime = Time.new.strftime("%Y%m%d")
   version = get_version_number #獲取版本號
   buildNumber = get_build_number  # 獲取build號
   fileName = "#{version}_#{buildNumber}_#{dateTime}.ipa"


   platform :ios do
      before_all do
      git_pull
      last_git_commit
      sh "rm -f ./Podfile.lock"
      cocoapods(use_bundle_exec: false)
   end
   
   # 提交一個新的Beta版本
   lane :beta do |options|
      
      sigh(app_identifier: app_identifier) #項目的bundle identifler    
      build_app(
         workspace: app_workspace,
         configuration: "Release",
         scheme: app_scheme,   
         silent: true,  #在構(gòu)建時隱藏不必要輸出的信息
         clean: true,  #在構(gòu)建前是否clean工程
         include_bitcode:false,  #是否開啟bitecode
         output_directory:"./packets" ,  #ipa的輸出路徑
         output_name:fileName,  #ipa的文件名(上面??定義)
         export_method: "app-store",  #打包方式
         export_options: {
             # 描述文件匹配哪個 identifier 和 profile文件名 
            provisioningProfiles: { "com.****.www" => "描述文件名稱"}
         }
      ) 
      
      #蘋果賬號鑒權(quán)
      api_key = app_store_connect_api_key(
        key_id: "C5P****KHG",
        issuer_id: "8a88fec8*************5ce96430a21",
        key_filepath: "./certificate/AuthKey_C5PG*****HG.p8",
        duration: 1200, # optional (maximum 1200)
        in_house: false # optional but may be required if using match/sigh
      )
   
      #上傳到TestFlight
      upload_to_testflight(
         api_key: api_key,
         skip_waiting_for_build_processing: true,
         ipa: "./packets/#{fileName}",
         skip_submission:true
      )
   end

執(zhí)行命令:

fastlane beta
執(zhí)行結(jié)果:

在終端看到 執(zhí)行success后,能在蘋果開發(fā)者后臺 https://appstoreconnect.apple.com/ 網(wǎng)站看到 我們剛才自動打包并上傳的ipa;并且在TestFlight內(nèi)測的用戶會收到相應(yīng)的通知;

image.png

蘋果賬號雙重認證問題

關(guān)于蘋果賬號的雙重認證問題,需要我們登陸Developer后臺中生成一個API密鑰,在App Store Connect -> 用戶與訪問 中進行配置;并把xxxxxxxx.p8 文件保存下來(xxxxxxxx.p8文件只能下載一次)

image.png

Question1 工程版本號 和build number 導致的問題

[17:33:41]: Before being able to increment and read the version number from your Xcode project, you first need to setup your project properly. Please follow the guide at https://developer.apple.com/library/content/qa/qa1827/_index.html

(CURRENT_PROJECT_VERSION)表示使用build Setting中的Version的(MARKETING_VERSION):表示使用build Setting中的Build的值;

配置如下:


image.png

Question2

+---------------------+-----------------------------------------------------+
|                               Lane Context                                |
+---------------------+-----------------------------------------------------+
| DEFAULT_PLATFORM    | ios                                                 |
| VERSION_NUMBER      | 23                                                  |
| BUILD_NUMBER        | 23                                                  |
| PLATFORM_NAME       | ios                                                 |
| LANE_NAME           | ios debug_pgyer                                     |
| CERT_FILE_PATH      | /Users/pengchao/Desktop/zheshi/ios_recoder/25KJ9FK  |
|                     | Q2Q.cer                                             |
| CERT_CERTIFICATE_ID | 25KJ9FKQ2Q                                          |
+---------------------+-----------------------------------------------------+
[18:03:59]: Could not find a matching code signing identity for type 'Development'. It is recommended to use match to manage code signing for you, more information on https://codesigning.guide. If you don't want to do so, you can also use cert to generate a new one: https://fastlane.tools/cert

解決辦法:

總結(jié)+后續(xù)

關(guān)于一些不清楚的參數(shù)可以參考 fastlane 官網(wǎng)的文檔,項目中首先保證自己這臺Mac 的配置 是可以正常打包的,否則很容易遇到問題;

參考文章

https://docs.fastlane.tools
iOS Fastlane 使用文檔
Fastlane的集成和使用 包括 ios/mac 項目示例
fastlane自動化打包iOS APP

最后編輯于
?著作權(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)容