Fastlane:移动应用CICD最优解之iOS项目Beta版部署

798 阅读5分钟

Beta版本部署

构建项目

fastlane 会使用一个名为build_app的操作来构建您的应用程序,只需将以下内容添加到项目的 Fastfile:

lane :beta do
  build_app(scheme: "MyApp")
end

此外,我们还可以为构建应用指定更多选项

lane :beta do
  build_app(scheme: "MyApp",
            workspace: "Example.xcworkspace",
            include_bitcode: true)
end

运行命令

fastlane beta

如果运行正常,则应该有一个“[ProductName]”。Ipa '文件在当前目录中。要获取 *build_app *的所有可用参数列表,运行' fastlane action build_app '。

代码签名

由于上一步的代码签名,可能出现了错误。我们准备了自己的代码签名指南,它可以帮助您为您的项目设置正确的代码签名方法。

上传

在构建完应用后,你便可以将其上传到你所选择的beta测试服务中。“fastlane”的美妙之处在于,你可以轻松切换beta版本的提供商,甚至可以一次上传多个版本,而无需任何额外的工作。

你所要做的就是在使用build_app构建应用后输入你选择的beta测试提供商的名称:

lane :beta do
  sync_code_signing(type: "appstore")    # see code signing guide for more information
  build_app(scheme: "MyApp")
  upload_to_testflight
  slack(message: "Successfully distributed a new beta build")
end

fastlane 自动传递关于生成的'.ipa '文件从build_app中选择的beta测试提供商。

像要获取操作中所有可用参数的列表,运行命令

fastlane action slack

Beta 测试

TestFlight

我们可以使用 fastlane轻松的上传到 TestFlight (App Store Connect标准测试服务)
只需要在构建完成之后 使用 built-in testflight action.

lane :beta do
  # ...
  build_app
  upload_to_testflight
end

示例用例

lane :beta do
  # ...
  build_app

  # Variant 1: Provide a changelog to your build
  upload_to_testflight(changelog: "Add rocket emoji")

  # Variant 2: Skip the "Waiting for processing" of the binary
  #   While this will speed up your build, it will not distribute
  #   the binary to your tests, nor set a changelog
  upload_to_testflight(skip_waiting_for_build_processing: true)
end

如果我们使用 fastlane init 来在项目中设置的 fastlane, 我们的 Apple ID 就被存储在 fastlane/Appfile文件。 你可以使用 upload_to_testflight(username: "bot@fastlane.tools")重新设置username 。

获取所有选项运行命令:

fastlane action upload_to_testflight

有了fastlane,你还可以自动管理你的测试者,检查其他可用的action。


变更记录

项目的变更记录会改变,所以在Fastfile中存储一个静态的发布记录没有多大意义。它会自动使用git commit中的信息,相关配置如下:

lane :beta do
  sync_code_signing
  build_app

  changelog_from_git_commits # this will generate the changelog based on your last commits
  upload_to_testflight
end

使用' fastlane action changelog_from_git_commits '获取所有可用选项的列表 下面是一些示例

changelog_from_git_commits(
  between: ['7b092b3', 'HEAD'], # Optional, lets you specify a revision/tag range between which to collect commit info
  merge_commit_filtering: 'exclude_merges' # Optional, lets you filter out merge commits
)

提示更新日志

你可以在你的终端中使用prompt 动作自动要求更新日志:

lane :beta do
  # Variant 1: Ask for a one line input
  changelog = prompt(text: "Changelog: ")

  # Variant 2: Ask for a multi-line input
  #   The user confirms their input by typing `END` and Enter
  changelog = prompt(
    text: "Changelog: ",
    multi_line_end_keyword: "END"
  )

  sync_code_signing
  build_app

  upload_to_testflight(changelog: changelog)
end

从文件系统或远程服务器获取变更日志

你可以从Fastfile的任何地方获取值,包括文件系统和远程服务器

lane :beta do
  # Variant 1: Read from file system
  #   note the `..`, since fastlane runs in the _fastlane_ directory
  changelog = File.read("../Changelog.txt")

  # Variant 2: Fetch data from a remote web server
  changelog = download(url: "https://lookatmycms.com/changelog.txt")

  sync_code_signing
  build_app

  upload_to_testflight(changelog: changelog)
end

Best Practices

使用fastlane管理设备和测试人员

TestFlight

如果你使用的是TestFlight,你就不需要担心设备的uddid。相反,你只需根据他们的Apple ID电子邮件地址维护一个测试者列表。

fastlane 支持自动注册设备使用不同的方法

boarding

boarding 允许您为您的beta测试人员设置注册页面,以便他们可以输入他们的电子邮件地址并开始测试您的应用程序。

/img/getting-started/ios/boarding-screenshot.png

点击 boarding GitHub repo 查看更多

pilot

pilot 是与fastlane一起自动安装的,您可以使用它来注册单独的测试人员到TestFlight

# Register a new external tester
fastlane pilot add email@invite.com

# Register a new external tester and add them to your app
fastlane pilot add email@invite.com -a com.app.name

第三方beta 测试服务

如果使用firim或蒲公英等第三方测试平台,你需要把测试者的iPhone设备和设备的 UDIDs注册到Apple Developer. fastlane 已经支持设备注册和更新配置文件开箱即用.

lane :beta do
  # 在调用match之前,我们确保所有设备都在苹果开发者门户网站上注册
  register_devices(devices_file: "devices.txt")

  # 在注册新设备之后,我们将确保在必要时更新配置文件
  # 请注意,我们如何确保通过“adhoc”来获取和使用Ad Hoc分发的配置配置文件
  sync_code_signing(force_for_new_devices: true, type: "adhoc")
  build_app
  # ...
end

devices.txt 有固定格式,格式如下

Device ID Device Name
A123456789012345678901234567890123456789  DeviceName1
B123456789012345678901234567890123456789  DeviceName2

自动增加build 版本

根据我们使用的Beta测试服务, 每次我们上传新版本的时候都需要更新build版本. 比如这在TestFlight中就是必须要做的.

为了这样么做, 这里有一些内置的 fastlane actions 可用, 以下就是例子

从TestFlight获取最新的构建号

下面的代码示例将使用来自TestFlight的最新构建号并基于这个版本号再加 1 。

lane :beta do
  increment_build_number(
    build_number: latest_testflight_build_number + 1,
    xcodeproj: "Example.xcodeproj"
  )
end

将版本号的修改提交到git来控制

下面的代码示例将增加构建号,并将项目更改提交给版本控制。

lane :beta do
  # 确保git状态是干净的(可以直接提交的代码)
  ensure_git_status_clean

  # 增加构建号(而不是版本号)
  # 提供的xcodeproj是可选的
  increment_build_number(xcodeproj: "Example.xcodeproj")

  # 提交版本变化-bump
  commit_version_bump(xcodeproj: "Example.xcodeproj")

  # 自动为这个构建添加一个git标签
  # 使用一个合适的git标签名
  add_git_tag

  # 将新的提交和标记推回到git远程端
  push_to_git_remote
end

对于以上所有步骤,还有更多可用的参数,运行以下命令获取完整列表:

fastlane action [action_name]

使用提交的版本号

不建议这样做,但是一些团队更喜欢这种方法。你可以使用当前分支的提交次数(通过' number_of_commits ')作为构建号。只有当你总是在同一个分支上运行构建时,这才会起作用。

lane :beta do
  increment_build_number(build_number: number_of_commits)
end