当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS中使用Fastlane实现自动化打包和发布

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

敦化新闻网,罗子乔,义马车祸

简介

fastlane是一套使用ruby写的自动化工具集,用于ios和android的自动化打包、发布等工作,可以节省大量的时间。

github:

官网:

文档:

安装

1、首先要安装正确的 ruby 版本。在终端窗口中用下列命令来确认:

ruby -v

2、然后检查 xcode 命令行工具是否安装。在终端窗口中输入命令:

xcode-select --install

如果未安装,终端会开始安装,如果报错误:command line tools are already installed, use "software update" to install updates.代表已经安装。

3、以上依赖配置好之后就可以通过 rubygem 进行安装了:

$ sudo gem install fastlane

安心等待一会,fastlane就安装完成了。

初始化

打开终端,cd到你的工程目录,然后执行fastlane init:

$ cd to/your/ios/project 
$ fastlane init
[14:21:43]: detected ios/mac project in current directory...
[14:21:43]: this setup will help you get up and running in no time.
[14:21:43]: fastlane will check what tools you're already using and set up
[14:21:43]: the tool automatically for you. have fun! 
[14:21:43]: created new folder './fastlane'.
[14:21:43]: $ xcodebuild -showbuildsettings -project ./xxx.xcodeproj
[14:21:48]: your apple id (e.g. fastlane@krausefx.com): xxx@xxx.xom
[14:21:54]: verifying that app is available on the apple developer portal and itunes connect...
[14:21:54]: starting login with user 'xxx@xxx.com'
+----------------+--------------------------------------+
|          detected values          |
+----------------+--------------------------------------+
| apple id    | xxx@xxx.com          |
| app name    | xxx              |
| app identifier | com.xxx.xxx        |
| project    | /users/lisong/desktop/xxx/x |
|        | xx.xcodeproj           |
+----------------+--------------------------------------+
[14:22:06]: please confirm the above values (y/n)
y
[14:22:09]: created new file './fastlane/appfile'. edit it to manage your preferred app metadata information.
[14:22:09]: loading up 'deliver', this might take a few seconds
[14:22:09]: login to itunes connect (xxx@xxx.com)
[14:22:13]: login successful
+-----------------------+------------------------+
|       deliver 2.30.1 summary       |
+-----------------------+------------------------+
| screenshots_path   | ./fastlane/screenshots |
| metadata_path     | ./fastlane/metadata  |
| username       | xxx@xxx.com   |
| app_identifier    | com.xxx.xxx |
| edit_live       | false         |
| platform       | ios          |
| skip_binary_upload  | false         |
| skip_screenshots   | false         |
| skip_metadata     | false         |
| force         | false         |
| submit_for_review   | false         |
| automatic_release   | false         |
| dev_portal_team_id  | wkr87ttkml       |
| overwrite_screenshots | false         |
+-----------------------+------------------------+
[14:22:21]: writing to 'fastlane/metadata/zh-hans/description.txt'
...
[14:22:21]: writing to 'fastlane/metadata/review_information/notes.txt'
[14:22:21]: successfully created new configuration files.
[14:22:22]: successfully downloaded large app icon
[14:22:22]: downloading all existing screenshots...
[14:22:27]: downloading existing screenshot '1_iphone4_1.1.jpg' for language 'zh-hans'
···
[14:22:34]: downloading existing screenshot '5_iphone6_5.5.jpg' for language 'zh-hans'
[14:22:34]: successfully downloaded all existing screenshots
[14:22:34]: successfully created new deliverfile at path 'fastlane/deliverfile'
[14:22:34]: $ xcodebuild -list -project ./xxx.xcodeproj
[14:22:35]: 'snapshot' not enabled.
[14:22:35]: 'cocoapods' enabled.
[14:22:35]: 'carthage' not enabled.
[14:22:35]: created new file './fastlane/fastfile'. edit it to manage your own deployment lanes.
[14:22:35]: fastlane will collect the number of errors for each action to detect integration issues
[14:22:35]: no sensitive/private information will be uploaded
[14:22:35]: learn more at https://github.com/fastlane/fastlane#metrics
[14:22:35]: successfully finished setting up fastlane

在 “your apple id” 这一步输入苹果开发者账号。在“please confirm the above values”这一步,确认信息,没问题输入 y。然后,fastlane 会进行一系列的初始化操作,包括下载 app store 上的元数据和截屏文件。

等待初始化完成之后,工程目录下就多了一个 fastlane目录,其内容如下:

咱们来看两个主要的,appfile和fastfile。

appfile

appfile用来存放app_identifier,apple_id和team_id。 了解详情,它的格式是这样的:

app_identifier "com.xxx.xxx" # app的bundle identifier
apple_id "xxx@xxx.com" # 你的apple id
team_id "xxxxxxxxxx" # team id

你也可以为每个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://hooks.slack.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://codesigning.guide
  gym(scheme: "docment") # build your app - more options available
  pilot
  # sh "your_script.sh"
 end
 desc "部署一个新版本到app store"
 lane :release do
  # match(type: "appstore")
  # snapshot
  gym(scheme: "docment") # build your app - more options available
  deliver(force: true)
  # frameit
 end
 # 你可以定义自己的lane
 #执行lane成功后的回调
 after_all do |lane|
  # slack(
  #  message: "successfully deployed new app update."
  # )
 end
 # 如果流程发生异常会走这里并终止
 error do |lane, exception|
  # slack(
  #  message: exception.message,
  #  success: false
  # )
 end
end

我们也可以定义一个自己的lane:

 desc "企业版"
 lane :inhouse do
 gym(scheme: "xxx",
   export_method:"enterprise",
   output_directory "./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

我们可以在 fastfile 文件中添加一个函数来设置version号和build号。

default_platform :ios

def prepare_version(options)
  increment_version_number(
    version_number: options[:version]
  )
  increment_build_number(
    build_number: options[:build]
  )
end

然后可以在一个lane中使用这个函数:

lane :appstore do |options|
  ···
  prepare_version(options)
  ···
end

然后执行这个lane的时候:

$ fastlane appstore version:2.4.0 build:2.0

好啦,先说到这里吧,fastlane能做的事情还有很多,大家可以去好好看看文档,研究一些高级的用法吧!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网