iOS中使用Fastlane实现自动化打包和发布

2020-01-20 13:09:34于海丽

你也可以为每个lane(后面会讲到)提供不同的 app_identifier, apple_id 和 team_id,例如:


app_identifier "com.aaa.aaa"
apple_id "aaa@aaa.com"
team_id "AAAAAAAAAA"

for_lane :inhouse do
 app_identifier "com.bbb.bbb"
 apple_id "bbb@bbb.com"
 team_id "AAAAAAAAAA"
end

这里就是为Fastfile中定义的:inhouse设置单独的信息。

Fastfile

Fastfile管理你所创建的 lane ,了解详情。它的格式是这样的:


···
# 自动更新fastlane 工具
# update_fastlane
#需要的fastlane的最小版本,在每次执行之后会检查是否有新版本,如果有会在最后末尾追加新版本提醒
fastlane_version "2.30.1"
#默认使用平台是 ios,也就是说文件可以定义多个平台
default_platform :ios
platform :ios do
 before_all do
  # ENV["SLACK_URL"] = "https://www.easck.com/services/..."
  cocoapods
 end
 desc "Runs all the tests"
 lane :test do
  scan
 end
 desc "提交一个新的Beta版本到 Apple TestFlight"
 desc "This will also make sure the profile is up to date"
 lane :beta do
  # match(type: "appstore") # more information: https://www.easck.com/build", # 打包后的 ipa 文件存放的目录
   output_name "XXX" # ipa 文件名
  )
 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就可以了。成功之后会在相应的路径下生成ipa文件,如果报错的话就根据错误信息好好查看文档。

其他

1、这里是官方提供的一些例子。

2、想了解fastlane命令的话可以执行$ fastlane –help

3、查看可用任务的列表,可以执行命令$ fastlane lanes

4、fastlane也提供了很多插件方便我们使用,例如pgyer(发布app到蒲公英)。我们也可以打完包直接传到蒲公英上,具体的可以看蒲公英提供的文档。

如果你感觉有些插件不符合自己的情况,你甚至可以自定义插件

5、多个 lane 的话实际上是可以相互调用的,这个其实特别实用。例如:


default_platform :ios
platform :ios do
 lane :prepare do
  cocoapods
  match
 end
 desc 'fastlane build'  'fastlane build type:adhoc'
 lane :build do |options|
  # 调用上面的 prepare 任务
  prepare
  case options[:type]
  when 'adhoc'
   adhoc
  else
   appstore
  end
 end
 lane : adhoc do
 ···
 end
 lane : appstore do
 ···
 end
end