cyfuer

Jun 07, 2017

iOS自动打包方案:Fastlane

 

安装fastlane

  • fastlane是基于ruby的,所以我们需要安装下ruby环境,这里ruby已经安装好了:
# 输出ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20] ruby -v
 
  • 然后检查 Xcode 命令行工具是否安装。在终端窗口中输入命令:
# 如果已经安装会提示command line tools are already installed, use "Software Update" to install updates. xcode-select --install
 
  • 通过gem安装fastlane
sudo gem install fastlane
 
这里遇到一个小插曲,安装好的fastlane无法运行,运行fastlane -v 报错:
Rworld@cyfuer ~/D/剑/i/woshiqiuxing> fastlane -v feature-downloadVideoB Traceback (most recent call last): 2: from /usr/local/bin/fastlane:23:in `<main>' 1: from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path' /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem fastlane (>= 0.a) with executable fastlane (Gem::GemNotFoundException)
找了很多办法,最后卸载fastlane,重新通过以下命令安装就可以了
gem install fastlane
 
执行fastlane -v 检测是否安装成功:
Rworld@cyfuer /u/l/bin> fastlane -v master? fastlane installation at path: /Users/Rworld/.rvm/gems/ruby-2.1.5/gems/fastlane-2.180.0/bin/fastlane ----------------------------- [✔] 🚀 fastlane 2.180.0
 

使用fastlane

  • 进入到要用fastlane自动打包的工程根目录,执行以下命令初始化fastlane
    • fastlane init feature-downloadVideoB
      输出如下:
      [✔] 🚀 [14:51:47]: Sending anonymous analytics information [14:51:47]: Learn more at https://docs.fastlane.tools/#metrics [14:51:47]: No personal or sensitive data is sent. [14:51:47]: You can disable this by adding `opt_out_usage` at the top of your Fastfile [✔] Looking for iOS and Android projects in current directory... [14:51:47]: Created new folder './fastlane'. [14:51:47]: Detected an iOS/macOS project in the current directory: 'woshiqiuxing.xcworkspace' [14:51:48]: ----------------------------- [14:51:48]: --- Welcome to fastlane 🚀 --- [14:51:48]: ----------------------------- [14:51:48]: fastlane can help you with all kinds of automation for your mobile app [14:51:48]: We recommend automating one task first, and then gradually automating more over time [14:51:48]: 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 ?
       
      最后面罗列出了使用fastlane的功能:
      1、代表app市场图片素材;
      2、发布到TestFlight进行测试;
      3、发布到App Store;
      4、自定义
       
  • 选择4自定义打包过程,等待初始化完成之后,工程目录就多了一个fastlane 目录,其内容如下:
    • notion image
      可能会遇到bundle update 命令卡很久(网络被墙了)。解决办法是关闭当前终端进程。然后在项目根目录执行以下命令:
      bundle update fastlane
       
  • 这里选择用蒲公英平台发包,所以还需要添加一个蒲公英测试平台的插件,在终端执行以下命令安装蒲公英插件:
    • fastlane add_plugin pgyer
      安装完成后fastlane 目录会多一个Pluginfile文件,如下图所示:
      notion image
      Appfile用来编辑设置app_identifier,apple_id和team_id的信息;
      Pluginfile是存放蒲公英测试平台的默认配置;
      Fastfile管理你所创建的 lane;
       
      appfile内容:
      app_identifier "com.smilodontech.iamkicker" apple_id "3281687865@qq.com" team_id "MZKRDDFYVS" for_lane :beta_pg do app_identifier("com.smilodontechbeta.iamkicker") team_id "MZKRDDFYVS" end
       
      fastfile内容:
      default_platform(:ios) platform :ios do desc "使用方法 `fastlane release_pg configuration:[Debug_Beta | Debug]`" private_lane :release_pg do |options| configuration = options[:configuration] date = Time.new.strftime("%Y%m%d-%H%M") gym( scheme: "woshiqiuxing", configuration: "#{configuration}", output_directory: "../build/#{configuration}/#{date}", output_name: "#{configuration}-#{date}.ipa", clean: true, silent: true, include_symbols: true, export_method:'development', ) pgyer(api_key: "8627de1da2f0adf52c6ea6b916195571", user_key: "d5f1a41563ea466600457e1bd53f3089", # update_description: get_update_description(), # password: "123456", # install_type: "2" ) end desc "发布测试版至蒲公英" lane :beta_pg do release_pg(configuration: 'Debug_Beta') end desc "发布正式版至蒲公英" lane :pg do release_pg(configuration: 'Debug') end end
       
      fastfile的参考格式一般如下所示:
      #---------- begin ----------- default_platform :ios platform :ios do desc "发布 蒲公英" lane :beta_pgy do gym(scheme: "Test", export_method: "ad-hoc", silent: true, # 隐藏没有必要的信息 clean: true # 在构建前先clean ) pgyer(api_key: "b*******************************4", user_key: "e********************************7", update_description: get_update_description(), # password: "123456", # install_type: "2" ) end desc "发布 到 苹果TestFlight" lane :beta_apple do gym(scheme: "Test"], silent: true, # 隐藏没有必要的信息 clean: true # 在构建前先clean ) pilot #管理TestFlight测试用户,上传二进制文件 end desc "发布苹果商店" lane :release_apple do gym(scheme: "Test"], silent: true, # 隐藏没有必要的信息 clean: true # 在构建前先clean ) deliver #上传截图、元数据、App到iTunesConnect end #---------- end ------------- # You can define as many lanes as you want after_all do |lane| # This block is called, only if the executed lane was successful # slack( # message: "Successfully deployed new App Update." # ) end error do |lane, exception| # slack( # message: exception.message, # success: false # ) end end
       
      其中一个lane就是一个任务,里面是一个个的action组成的工作流。利用目前支持的工具可以做所有包含自动化和可持续化构建的每个环节,例如:
    • scan 自动化测试工具,很好的封装了 Unit Test
    • sigh 针对于 iOS 项目开发证书和 Provision file 的下载工具
    • match 同步团队每个人的证书和 Provision file 的超赞工具
    • gym 针对于 iOS 编译打包生成 ipa 文件
    • deliver 用于上传应用的二进制代码,应用截屏和元数据到 App Store
    • snapshot 可以自动化iOS应用在每个设备上的本地化截屏过程
    •  
  • 在工程根目录执行lane命令
    • fastlane <lane name>
       
      如果安装了mac版的蒲公英客户端的朋友,在自动打包完成之后,客户端会自动拦截ipa的动作(前提是保证你的账户和fastfile里面对于蒲公英的配置api_key 和user_key的配置保持一致即可)然后将ipa上传即可。
       

参考链接

 

Copyright © 2025 cyfuer

logo