iOS Fastlane --自动化打包上传到蒲公英、fir

参考: iOS使用fastlane实现自动化打包

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 到项目根目录中
fastlane init 之后会出现如下图的4个选项:

image.png

  • 1)自动截取APP的屏幕
  • 2)发布bate版用于TestFlight
  • 3)发布到appStore
  • 4)自定义任务

肯定是选择4,选择完成后项目目录中多出了这些文件


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 这时build会根据之前的build+1 #########
###### fastlane iosDebug  ######

###如果需要上传到蒲公英,请运行前先安装插件安装蒲公英的fastlane add_plugin pgyer ###


default_platform(:iOS)   # 设置默认的平台为iOS

############# 需要根据项目自定义设置 ############
APP_NAME = "AiDouVideo"             #输出包时app的名字
APP_SCHEME = "AiDouVideo"   #这个app打开manage schemes下选中的名称
APP_IPA_OUTPUT_DIRECTORY = "/Users/zhousong/Desktop/IPA"    # 打包所保存的文件目录,可以不设置
# 配置蒲公英的apiKey 和 userKey 需要设置 
APP_PGYER_API_KEY = "xxx"
APP_PGYER_USER_KEY = "xxx"
# 配置fir的token
APP_FIR_API_TOKEN = "xx"


######## 不用设置的 #####################
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")  # 打包的时间
# APP_INFO_PLIST_PATH = './AiDouVideo/AiDouVideo-Info.plist'
APP_ENV_PREFIX = ""   # 打包完成后的包文件名字的前缀 区别release和debug



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

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


    #增加build号  只能是整数和浮点数
    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


#统一的打包方法
def generate_ipa(exportMethod,configuration,options)

    # 设置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项目
      silent: true,      # 隐藏没有必要的信息
      scheme: "#{APP_SCHEME}",
      workspace: "#{APP_WORKSPACE}", 
      configuration: "#{configuration}",  # 环境
      export_method: "#{exportMethod}",   # app-store、ad-hoc、development、enterprise
      output_directory: "#{APP_IPA_OUTPUT_DIRECTORY}", #ipa的存放目录
      output_name: "#{app_ipa_name}", # 输出ipa的文件名
      # 生成的ipa文件是否包含symbols,这个文件是内存标记文件,用来定位错误信息的,有了这个安装包大小会变大
      include_symbols: true,
      # 生成的ipa文件是否包含bitcode,在本身项目中也可以配置
      include_bitcode: false,
      # keychain授权 Xcode9不允许访问钥匙串密码,所以我们需要手动开权限
      export_xcargs: "-allowProvisioningUpdates"
      )

end

platform :iOS do
  # before_all就是先于所有lane执行的任务
  before_all do
       # 根据安装路径指定要使用的Xcode 
       xcode_select "/Applications/Xcode.app"
       # 超时失败,默认的timeout是10秒
       ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "120"
       # Appfile设置了可以忽略下面的证书设置
       # 这个action很重要cert就是下载和安装匹配的Provision Profile文件,不用你去管那些证书不匹配的事情了
       # cert(
          # Appfile设置了这边就可以不用了
          # username: "[email protected]",
          # team_id: "xxxxxxx",
          # output_path:"#{APP_CERT_OUTPUT_DIRECTORY}"
        # )

       #这一步就是签名了
       # sigh(
         # Appfile设置了这边就可以不用了 
         # app_identifier: ENV["APP_IDENTIFIER"],
         # team_id: ENV['TEAM_ID'],

         # 更新开发证书而不是生产证书
         # development: ???  
         # 因为是根据BundleID下载,导致adhoc和appstore会优先appstore,导致最后导出报错,如果是adhoc包请设置为true
         # adhoc: true, 
         # 设置所生成描述文件的名称,必须包含文件类型后缀.mobileprovision 可以不设置
         # filename: "MyTest.mobileprovision",

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

  # lane  自定义的任务

#debug包 上传到蒲公英平台
#option类似一个字典 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: "发布成功!", message: "已成功上传到蒲公英平台, 赶快联系测试人员开始测试吧!", 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: "发布成功!", message: "已成功上传到fir平台, 赶快联系测试人员开始测试吧!", open: "https://fir.im/apps")
    end
  end
# release发布包
lane :  do |options|
  APP_ENV_PREFIX = "appstore_"
  generate_ipa("app-store","Release",options)
  # 上传至app-store
  deliver(
    force: true,              #是否跳过HTML验证报告,默认false
    skip_metadata: true,      #是否跳过上传metadata,默认false
    skip_screenshots: true    #是否跳过上传屏幕截图,默认false
   )
   notification(title: "发布成功!", message: "已成功发布到appstore, 请查验!", open: "https://itunesconnect.apple.com")
end

# testFlight包
lane :iosTestFlight do |options|
  APP_ENV_PREFIX = "adhoc_"
  generate_ipa("ad-hoc","Release",options)
  # 管理TestFlight的测试用户,上传二进制文件
  pilot
  notification(title: "发布成功!", message: "已成功发布到appstore, 请查验!", open: "https://itunesconnect.apple.com")
end



  # 当lane执行完成之后进行哪些操作
  after_all do |lane|
  #发送一个桌面通知
  #notification(title: "execute success", subtitle: "执行成功!", message: "lane已经执行成功了")
  end

  error do |lane, exception|
  puts("#{exception.message}")
  notification(title: "执行#{lane}发生异常!", message: "发生异常, 详情请查看控制台!")
  end

end

注意: APP_PGYER_API_KEYAPP_PGYER_USER_KEYAPP_FIR_API_TOKEN分别要去蒲公英和fir的官网上申请的。
在进行打包前我们还要安装蒲公英和fir的插件
fastlane add_plugin pgyerfastlane add_plugin firimfastlane add_plugin fir_cli

6. 执行任务

Fastfile 配置文件中我们可以看到有四个任务,分别是:iosDebug_pgyiosDebug_firiosTestFlightiosRelease ,每个任务里都有不同的配置,如果想要自行配置可在文件中进行修改,我们只需要在项目根目录下执行 fastline iosDebug_pgyfastlane iosDebug_fir..等对应的任务名即可。这样就成功了

image.png

7. 遇到的问题

当我在打Release包的时候,遇到了下面这些错误

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 
[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)

看到这些报错我以为是文件导入方式不对,但改了之后也是报错,于是我仔细看了第一句错误Couldn't find specified configuration 'Release',我点开了Edit Shceme,然后我发现Build Configuration 选项中只有Debug没有Release ,在下面添加了Release 模式,重新运行即可打包成功。

image.png

你可能感兴趣的:(iOS Fastlane --自动化打包上传到蒲公英、fir)