Fastlane 自动打包

985 阅读5分钟

前言

  • 可以自动执行繁琐的任务,例如生成屏幕截图,处理配置文件和发布应用程序。每天我们可能会打包很多次,传统的打包这时候就显得比较浪费时间,因此我们可以采用自动打包方式来提升我们的效率

官网地址:docs.fastlane.tools/plugins/ava…

文档链接:

插件:docs.fastlane.tools/plugins/ava…
命令:docs.fastlane.tools/actions/

下面我们就来介绍一下自动打包

安装Fastlane

先安装Ruby环境,参考这篇文章Mac配置Ruby环境和安装CocoaPods

1.使用Homebrew安装

brew install fastlane

2.使用Gem安装

sudo gem install fastlane

如果出现如下情况

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.

解决方案:

sudo gem install -n /usr/local/bin fastlane

卸载Fastlane

sudo gem unstall fastlane

更新Fastlane

这里需要看你是通过什么方式安装

1.通过gem方式安装

sudo gem install fastlane

2.通过Homebrew方式安装

brew upgrade fastlane

查看Fastlane位置

先找到Fastlane的位置

which fastlane

位置如下:

/usr/local/bin/fastlane

查看Fastlane版本号

fastlane --version

结果如下:

fastlane installation at path:
/Library/Ruby/Gems/2.6.0/gems/fastlane-2.180.1/bin/fastlane
-----------------------------
[✔] 🚀 
fastlane 2.180.1

安装插件

蒲公英插件

fastlane add_plugin pgyer

配置Fastlane

1.cd 到指定目录

2.下载证书到你的钥匙串,这里需要开发者证书和发布证书

3.初始化fastlane

fastlane init

这里有4种选择供选择,

  • Automate screenshots: 自动截屏
  • Automate beta distribution to TestFlight: 自动发布beta版本到testFlight
  • Automate App Store distribution: 自动发布到App Store
  • Manual setup: 手动设置
[✔] 🚀 
[✔] Looking for iOS and Android projects in current directory...
[16:59:49]: Created new folder './fastlane'.
[16:59:49]: Detected an iOS/macOS project in the current directory: 'KJTestCategoryDemo.xcworkspace'
[16:59:49]: -----------------------------
[16:59:49]: --- Welcome to fastlane 🚀 ---
[16:59:49]: -----------------------------
[16:59:49]: fastlane can help you with all kinds of automation for your mobile app
[16:59:49]: We recommend automating one task first, and then gradually automating more over time
[16:59:49]: 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

输入1

[17:00:43]: -------------------------------------------------------
[17:00:43]: --- Setting up fastlane to automate iOS screenshots ---
[17:00:43]: -------------------------------------------------------
[17:00:43]: fastlane uses UI Tests to automate generating localized screenshots of your iOS app
[17:00:43]: fastlane will now create 2 helper files that are needed to get the setup running
[17:00:43]: For more information on how this works and best practices, check out
[17:00:43]: 	https://docs.fastlane.tools/getting-started/ios/screenshots/

输入2

[17:00:49]: Parsing your local Xcode project to find the available schemes and the app identifier
[17:00:49]: Resolving Swift Package Manager dependencies...
[17:00:49]: $ xcodebuild -resolvePackageDependencies -workspace KJTestCategoryDemo.xcworkspace -scheme KJTestCategoryDemo
[17:00:50]: ▸ Command line invocation:
[17:00:50]: ▸     /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -resolvePackageDependencies -workspace KJTestCategoryDemo.xcworkspace -scheme KJTestCategoryDemo
[17:00:51]: ▸ resolved source packages: 
[17:00:51]: $ xcodebuild -showBuildSettings -workspace KJTestCategoryDemo.xcworkspace -scheme KJTestCategoryDemo
✅  Successfully created SnapshotHelper.swift './fastlane/SnapshotHelper.swift'
✅  Successfully created new Snapfile at './fastlane/Snapfile'
-------------------------------------------------------
Open your Xcode project and make sure to do the following:
1) Add a new UI Test target to your project
2) Add the ./fastlane/SnapshotHelper.swift to your UI Test target
   You can move the file anywhere you want
3) Call `setupSnapshot(app)` when launching your app

  let app = XCUIApplication()
  setupSnapshot(app)
  app.launch()

4) Add `snapshot("0Launch")` to wherever you want to trigger screenshots
5) Add a new Xcode scheme for the newly created UITest target
6) Add a Check to enable the `Shared` box of the newly created scheme

More information: https://docs.fastlane.tools/getting-started/ios/screenshots/
[17:00:53]: If you want more details on how to setup automatic screenshots, check out
[17:00:53]: 	https://docs.fastlane.tools/getting-started/ios/screenshots/#setting-up-snapshot

输入3,输入Apple账号和密码

[17:02:24]: --------------------------------
[17:02:24]: --- Login with your Apple ID ---
[17:02:24]: --------------------------------
[17:02:24]: To use App Store Connect and Apple Developer Portal features as part of fastlane,
[17:02:24]: we will ask you for your Apple ID username and password
[17:02:24]: This is necessary for certain fastlane features, for example:
[17:02:24]: 
[17:02:24]: - Create and manage your provisioning profiles on the Developer Portal
[17:02:24]: - Upload and manage TestFlight and App Store builds on App Store Connect
[17:02:24]: - Manage your App Store Connect app metadata and screenshots
[17:02:24]: 
[17:02:24]: Your Apple ID credentials will only be stored in your Keychain, on your local machine
[17:02:24]: For more information, check out
[17:02:24]: 	https://github.com/fastlane/fastlane/tree/master/credentials_manager
[17:02:24]: 
[17:02:24]: Please enter your Apple ID developer credentials
[17:02:24]: Apple ID Username:

这时我们会发现我们的目录下面多了一个下面的文件夹,

image.png

配置Fastfile文件内容

  • 如下配置信息仅供参考,请切换为属于你的专属信息
fastlane_version "2.207.0"

default_platform :ios

platform :ios do

  desc "以 development 方式打包并上传到蒲公英"
  lane :test_beta do

    puts "以 development 方式打包"
    gym(
      # 指定打包所使用的输出方式 (可选: app-store, package, ad-hoc, enterprise, development)
      export_method: "development",
      # 指定项目的 scheme 名称
      scheme: "xxx",
      # 指定输出的文件夹地址
      output_directory: "./archive/test_beta/" + Time.new.strftime("%Y-%m-%d-%H:%M:%S"),
    )

    puts "上传 ipa 包到蒲公英"
    pgyer(
      # 蒲公英 API KEY
      api_key: "xxx",
      # 蒲公英 USER KEY
      user_key: "xxx"
    )

  end

  desc "以 ad-hoc 方式打包并上传到蒲公英"
  lane :beta do
    puts "自动生成 Provisioning Profiles 文件"
    sigh(
      # 指定输出的文件夹地址
      output_path: "./archive/sign",
      # 是否为 AdHoc 证书(设为 false 或不写默认为 AppStore 证书)
      adhoc: true
    )

    puts "以 ad-hoc 方式打包"
    gym(
      # 指定打包所使用的输出方式 (可选: app-store, package, ad-hoc, enterprise, development)
      export_method: "ad-hoc",
      # 指定项目的 scheme 名称
      scheme: "xxx",
      # 指定输出的文件夹地址
      output_directory: "./archive/beta/" + Time.new.strftime("%Y-%m-%d-%H:%M:%S"),
      # 指定打包方式 (可选: Release, Debug)
      configuration: "Release"
    )
    puts "上传 ipa 包到蒲公英"
    pgyer(
      # 蒲公英 API KEY
      api_key: "xxx",
      # 蒲公英 USER KEY
      user_key: "xxx"
    )

  end

  desc "以 app-store 方式打包并上传到 iTunes Connect"
  lane :release do

    puts "自动生成 Provisioning Profiles 文件"
    sigh(
      # 指定输出的文件夹地址
      output_path: "./archive/sign"
    )

    puts "以 app-store 方式打包"
    gym(
      # 指定打包所使用的输出方式 (可选: app-store, package, ad-hoc, enterprise, development)
      export_method: "app-store",
      # 指定项目的 scheme 名称
      scheme: "xxx",
      # 指定输出的文件夹地址
      output_directory: "./archive/release/" + Time.new.strftime("%Y-%m-%d-%H:%M:%S"),
      # 指定打包方式 (可选: Release, Debug)
      configuration: "Release"
    )

    puts "上传 ipa 包到 iTunes Connect"
    deliver(
      # 跳过截图上传
      skip_screenshots: true,
      # 跳过元数据上传
      skip_metadata: true,
      # 跳过审核直接上传
      force: true
    )

  end

end
  • 按上述模式上传至蒲公英
fastlane test_beta

问题总结

缺少插件,解决方案:
image.png 2.1 安装bundle install
2.2 安装插件bundle exec fastlane add_plugin fir

成功如下:
image.png


正常发布流程, image.png

提示缺乏发布证书, image.png

接下来就是等待打包过程,成功如下: image.png

接下来就是上传到苹果平台,这里会提示你登录苹果账号: image.png

发布成功界面: image.png 总耗时,12分钟