iOS Fastlane --自動化打包上傳到蒲公英、fir

參考: iOS使用fastlane實(shí)現(xiàn)自動化打包

1. 安裝 homebrew

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

  • 查看homebrew版本 brew -v
  • 更新 brew update
  • 本地軟件庫列表 brew ls

2. 檢查 ruby版本,以及是否安裝 Xcode 命令行工具

ruby -v 查看是否安裝ruby

xcode-select --install 安裝Xcode工具

3. 安裝 fastlane

sudo gem install fastlane

之后 cd 到項(xiàng)目根目錄中
fastlane init 之后會出現(xiàn)如下圖的4個(gè)選項(xiàng):

image.png

  • 1)自動截取APP的屏幕
  • 2)發(fā)布bate版用于TestFlight
  • 3)發(fā)布到appStore
  • 4)自定義任務(wù)

肯定是選擇4,選擇完成后項(xiàng)目目錄中多出了這些文件


image.png

4. 文件配置

  • 配置 Appfile (這里填app的bundleId)
  app_identifier("[[xxx]]") # The bundle identifier of your app
  • 配置 Fastfile
```  
# 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



################## READ  FIRST ##################

######## 使用方法1 自定義version和build #########
###### fastlane iosDebug version:1.0.1  build:1 ######

######## 使用方法2 不寫version和build 這時(shí)build會根據(jù)之前的build+1 #########
###### fastlane iosDebug  ######

###如果需要上傳到蒲公英,請運(yùn)行前先安裝插件安裝蒲公英的fastlane add_plugin pgyer ###


default_platform(:iOS)   # 設(shè)置默認(rèn)的平臺為iOS

############# 需要根據(jù)項(xiàng)目自定義設(shè)置 ############
APP_NAME = "AiDouVideo"             #輸出包時(shí)app的名字
APP_SCHEME = "AiDouVideo"   #這個(gè)app打開manage schemes下選中的名稱
APP_IPA_OUTPUT_DIRECTORY = "/Users/zhousong/Desktop/IPA"    # 打包所保存的文件目錄,可以不設(shè)置
# 配置蒲公英的apiKey 和 userKey 需要設(shè)置 
APP_PGYER_API_KEY = "xxx"
APP_PGYER_USER_KEY = "xxx"
# 配置fir的token
APP_FIR_API_TOKEN = "xx"


######## 不用設(shè)置的 #####################
APP_XCODEPROJ = "#{APP_NAME}.xcodeproj"      # app的xcodeproj
APP_WORKSPACE = "#{APP_NAME}.xcworkspace"    # app的xcworkspace
APP_IPA_TIME = Time.now.strftime("%Y-%m-%d_%H:%M")  # 打包的時(shí)間
# APP_INFO_PLIST_PATH = './AiDouVideo/AiDouVideo-Info.plist'
APP_ENV_PREFIX = ""   # 打包完成后的包文件名字的前綴 區(qū)別release和debug



# 版本 build number++
def prepare_version(options)

    #增加version版本號
    if options[:version]
      increment_version_number(
        version_number: options[:version],
        xcodeproj: "#{APP_XCODEPROJ}",
      )
    else
      # 可以不設(shè)置
    end


    #增加build號  只能是整數(shù)和浮點(diǎn)數(shù)
    if options[:build]
      increment_build_number(
        build_number: options[:build],
        xcodeproj: "#{APP_XCODEPROJ}",
      )
    else
      last_build = get_build_number(xcodeproj: "#{APP_XCODEPROJ}")
      now_build = last_build.to_i + 1
      increment_build_number(
        build_number: now_build,
        xcodeproj: "#{APP_XCODEPROJ}",
      )
    end
end


#統(tǒng)一的打包方法
def generate_ipa(exportMethod,configuration,options)

    # 設(shè)置version和build
    prepare_version(options)  
    # 得到最新的version
    app_version = get_version_number(target: "#{APP_SCHEME}")     
    # 最新的build
    app_build = get_build_number(xcodeproj: "#{APP_XCODEPROJ}")         
    # app包名
    app_ipa_name = "#{APP_NAME}_" "#{APP_ENV_PREFIX}" + "#{APP_IPA_TIME}_" + "#{app_version}_" + "#{app_build}"

    #打包
    gym(
      clean: true,       # 打包前clean項(xiàng)目
      silent: true,      # 隱藏沒有必要的信息
      scheme: "#{APP_SCHEME}",
      workspace: "#{APP_WORKSPACE}", 
      configuration: "#{configuration}",  # 環(huán)境
      export_method: "#{exportMethod}",   # app-store、ad-hoc、development、enterprise
      output_directory: "#{APP_IPA_OUTPUT_DIRECTORY}", #ipa的存放目錄
      output_name: "#{app_ipa_name}", # 輸出ipa的文件名
      # 生成的ipa文件是否包含symbols,這個(gè)文件是內(nèi)存標(biāo)記文件,用來定位錯(cuò)誤信息的,有了這個(gè)安裝包大小會變大
      include_symbols: true,
      # 生成的ipa文件是否包含bitcode,在本身項(xiàng)目中也可以配置
      include_bitcode: false,
      # keychain授權(quán) Xcode9不允許訪問鑰匙串密碼,所以我們需要手動開權(quán)限
      export_xcargs: "-allowProvisioningUpdates"
      )

end

platform :iOS do
  # before_all就是先于所有l(wèi)ane執(zhí)行的任務(wù)
  before_all do
       # 根據(jù)安裝路徑指定要使用的Xcode 
       xcode_select "/Applications/Xcode.app"
       # 超時(shí)失敗,默認(rèn)的timeout是10秒
       ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "120"
       # Appfile設(shè)置了可以忽略下面的證書設(shè)置
       # 這個(gè)action很重要cert就是下載和安裝匹配的Provision Profile文件,不用你去管那些證書不匹配的事情了
       # cert(
          # Appfile設(shè)置了這邊就可以不用了
          # username: "xxx@xx.com",
          # team_id: "xxxxxxx",
          # output_path:"#{APP_CERT_OUTPUT_DIRECTORY}"
        # )

       #這一步就是簽名了
       # sigh(
         # Appfile設(shè)置了這邊就可以不用了 
         # app_identifier: ENV["APP_IDENTIFIER"],
         # team_id: ENV['TEAM_ID'],

         # 更新開發(fā)證書而不是生產(chǎn)證書
         # development: ???  
         # 因?yàn)槭歉鶕?jù)BundleID下載,導(dǎo)致adhoc和appstore會優(yōu)先appstore,導(dǎo)致最后導(dǎo)出報(bào)錯(cuò),如果是adhoc包請?jiān)O(shè)置為true
         # adhoc: true, 
         # 設(shè)置所生成描述文件的名稱,必須包含文件類型后綴.mobileprovision 可以不設(shè)置
         # filename: "MyTest.mobileprovision",

        #  force:true,
        #  provisioning_name: 'MyTest AppStore',
        #  ignore_profiles_with_different_name: true,
        #  output_path: "build/sign",
        # )
  end

  # lane  自定義的任務(wù)

#debug包 上傳到蒲公英平臺
#option類似一個(gè)字典 option[:version] 取其中value值version
lane :iosDebug_pgy do |options|
  APP_ENV_PREFIX = "ad-hoc"
  generate_ipa("ad-hoc","Release",options)

  #上傳至蒲公英 在這之前請安裝插件 fastlane add_plugin pgyer
  if APP_PGYER_API_KEY.length > 0 && APP_PGYER_USER_KEY.length > 0
    pgyer(
      api_key: "#{APP_PGYER_API_KEY}",
      user_key: "#{APP_PGYER_USER_KEY}",
    )
  notification(title: "發(fā)布成功!", message: "已成功上傳到蒲公英平臺, 趕快聯(lián)系測試人員開始測試吧!", open: "https://www.pgyer.com/")
  end
end
 #debug包 上傳到fir
  lane :iosRelease_fir do |options|
    APP_ENV_PREFIX = "ad-hoc"
    generate_ipa("ad-hoc","Release",options)

    #上傳至fir 在這之前請安裝插件 fastlane add_plugin firim
    if APP_FIR_API_TOKEN.length > 0 
      firim(
        firim_api_token: "#{APP_FIR_API_TOKEN}", 
      )
    notification(title: "發(fā)布成功!", message: "已成功上傳到fir平臺, 趕快聯(lián)系測試人員開始測試吧!", open: "https://fir.im/apps")
    end
  end
# release發(fā)布包
lane :  do |options|
  APP_ENV_PREFIX = "appstore_"
  generate_ipa("app-store","Release",options)
  # 上傳至app-store
  deliver(
    force: true,              #是否跳過HTML驗(yàn)證報(bào)告,默認(rèn)false
    skip_metadata: true,      #是否跳過上傳metadata,默認(rèn)false
    skip_screenshots: true    #是否跳過上傳屏幕截圖,默認(rèn)false
   )
   notification(title: "發(fā)布成功!", message: "已成功發(fā)布到appstore, 請查驗(yàn)!", open: "https://itunesconnect.apple.com")
end

# testFlight包
lane :iosTestFlight do |options|
  APP_ENV_PREFIX = "adhoc_"
  generate_ipa("ad-hoc","Release",options)
  # 管理TestFlight的測試用戶,上傳二進(jìn)制文件
  pilot
  notification(title: "發(fā)布成功!", message: "已成功發(fā)布到appstore, 請查驗(yàn)!", open: "https://itunesconnect.apple.com")
end



  # 當(dāng)lane執(zhí)行完成之后進(jìn)行哪些操作
  after_all do |lane|
  #發(fā)送一個(gè)桌面通知
  #notification(title: "execute success", subtitle: "執(zhí)行成功!", message: "lane已經(jīng)執(zhí)行成功了")
  end

  error do |lane, exception|
  puts("#{exception.message}")
  notification(title: "執(zhí)行#{lane}發(fā)生異常!", message: "發(fā)生異常, 詳情請查看控制臺!")
  end

end

注意: APP_PGYER_API_KEY、 APP_PGYER_USER_KEY、 APP_FIR_API_TOKEN分別要去蒲公英和fir的官網(wǎng)上申請的。
在進(jìn)行打包前我們還要安裝蒲公英和fir的插件
fastlane add_plugin pgyer、 fastlane add_plugin firim 、fastlane add_plugin fir_cli

6. 執(zhí)行任務(wù)

Fastfile 配置文件中我們可以看到有四個(gè)任務(wù),分別是:iosDebug_pgyiosDebug_fir 、iosTestFlightiosRelease ,每個(gè)任務(wù)里都有不同的配置,如果想要自行配置可在文件中進(jìn)行修改,我們只需要在項(xiàng)目根目錄下執(zhí)行 fastline iosDebug_pgy 、fastlane iosDebug_fir..等對應(yīng)的任務(wù)名即可。這樣就成功了

image.png

7. 遇到的問題

當(dāng)我在打Release包的時(shí)候,遇到了下面這些錯(cuò)誤

Couldn't find specified configuration 'Release'
[22:29:19]: ? ?  /Users/zhousong/Desktop/DOEX/交易所/AiDouVideo/ThirdPartyFramework/WSocketManager/WebSocketManager.h:3:9: 'SocketRocket/SocketRocket.h' file not found
[22:29:19]: ? #import <SocketRocket/SocketRocket.h>
[22:29:19]: ?         ^
[22:29:19]: ? ?  error: failed to emit precompiled header '/Users/zhousong/Library/Developer/Xcode/DerivedData/AiDouVideo-bxifstjvaotselgzfgzbhliuqvfq/Build/Intermediates.noindex/ArchiveIntermediates/AiDouVideo/PrecompiledHeaders/AiDouVideo-Bridging-Header-swift_1OUSDN8PU827W-clang_1WFO3U4XD3B2D.pch' for bridging header /Users/zhousong/Desktop/DOEX/交易所/AiDouVideo/ConfigurationFiles/Tools/Tool/Iban/AiDouVideo-Bridging-Header.h
[22:29:20]: ? ** ARCHIVE FAILED **
[22:29:20]: ? The following build commands failed:
[22:29:20]: ?   PrecompileSwiftBridgingHeader normal arm64
[22:29:20]: ?   CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
[22:29:20]: ? (2 failures)

看到這些報(bào)錯(cuò)我以為是文件導(dǎo)入方式不對,但改了之后也是報(bào)錯(cuò),于是我仔細(xì)看了第一句錯(cuò)誤Couldn't find specified configuration 'Release',我點(diǎn)開了Edit Shceme,然后我發(fā)現(xiàn)Build Configuration 選項(xiàng)中只有Debug沒有Release ,在下面添加了Release 模式,重新運(yùn)行即可打包成功。

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

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