使用 Fastlane 实现 iOS 跟 android 自动打包脚本

对于一个iOS APP的发布上线,一般来说都需要经历:编译打包 -> 截图 -> 填写一些说明文字 -> 上传ipa到itunes connect -> 提交供审核。每次都要进行这么多“繁琐”的步骤,对于某些步骤可能一次还不能执行成功需要等着界面提示上传错误然后手动重新再来一次(想想都觉得可怕)。 在日常开发中,打包也是最后上线不可缺少的环节,如果需要生成ipa文件通常需要在Xcode里点击Product -> Archive,然后在弹出来的Organizer中选择导出什么类型(ad hoc/enterprise)的包。对于大项目来说动辄编译十分钟以上的来说,一天打几个包就差不多过去了。 为了解决这些问题,Felix Krause 大神写了一个工具集 fastlane。fastlane 是一套使用Ruby写的自动化工具集,用于 iOS 和 Android 的自动化打包、发布等工作。

前言

最近用shell打包ipa发现终端总是提示:
shell error: exportArchive: "***.app" requires a provisioning profile.
把配置文件删除重新下载也一样,所以索性重新换用另一种脚本工具来打包ipa,发现这个还是挺好用的

介绍

配置环境

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

  • 如果没有安装,则输入命令安装 gym: sudo gem install gym

  • 确保Xcode命令行工具安装最新版本,使用如下命令进行安装: xcode-select --install

  • 以上依赖配置好之后就可以通过 rubygem 进行安装 fastlane: sudo gem install fastlane

  • 完成安装

fastlane实战

初始化

  • 打开终端,cd到你的工程目录,然后执行 fastlane init 命令开始初始化

  • 在执行的过程中会要求填写一些项目的资料,如 Apple ID 等,fastlane 会自动检测当前目录下项目的 App Name 和 App Identifier,可以选择自行输入这些信息。初始化完成会在当前目录下面生成一个fastlane的文件夹。

  • 最重要的两个文件就是 Appfile 和 Fastfile,主要的说明如下

  • Appfile里面存放了App的基本信息包括 app_identifier、apple_id、team_id 等,如果在 init 的时候输入了正确的 apple_id 和密码会自动获取 team_id。

  • Fastfile 是最重要的一个文件,在这个里面可以编写和定制我们的自动化脚本,所有的流程控制功能都写在这个文件里面。

fastfile 文件

Fastfile 管理你所创建的 lane ,了解详情。它的格式是这样的:
desc "企业版"
lane :inHouse do
gym(scheme: "XXX",
export_method:"enterprise",
output_directory "./build", # 打包后的 ipa 文件存放的目录
output_name "XXX"  # ipa 文件名
)
end
复制代码

我的用法

fastfile 文件里主要修改四个地方内容

  1. platform :ios do(安卓,iOS 都可以用)

  2. desc "ad_Hoc 版本"(对 lane 的描述,fastlane 会自动将 desc 的内容生成说明文档)

  3. lane :beta do (定义一个 lane (任务),可以理解为一个函数,我们在执行的时候使用 fastlane lane 名称,比如 cd到项目根目录,然后 fastlane beta )

  4. gym(scheme: “项目名称”, export_method:"app-store",output_directory: "./build",)

我一般用gym语法操作

gym(scheme: scheme_name, clean: true, export_method:'appstore', configuration: configuration, output_directory: output_directory, output_name: output_name)
复制代码

结果

这次我只是用来打包测试,fastlane里的源码:

# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
# For a list of all available actions, check out``
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane
# 指定打包所使用的输出方式,目前支持 app-store, package, ad-hoc, enterprise, development, 和developer-id


default_platform(:ios)

platform :ios do
desc "ad_Hoc 版本"
lane :beta do
gym(scheme: "***",
export_method:"ad-hoc",
output_directory: "./build",#文件路径
)
end
end
复制代码

接下来我们开始进阶教程,将打包好的 ipa 上传到 fir 在Fastfile文件里写入:

# This is the minimum version number required.
# Update this, if you use features of a newer version
# 指定打包所使用的输出方式,目前支持 app-store, package, ad-hoc, enterprise, development, 和developer-id



default_platform :ios

platform :ios do


desc "开始打包-内测版--开发证书 - dev"
#内测版--开发证书
lane :adhoc do 
#开始打包
puts "开始打包-内测版--开发证书 - dev"

gym(
export_method:"ad-hoc",
output_directory:"/Users/weiyuxiang/Desktop/Order/build",# 打包后的 ipa 文件存放的目录
) 
#使用fir-cli上传ipa
sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T fir的token"
end

desc "开始打包  --  企业公测版--hoc"
lane :inhoc do
gym(
export_method:"app-store",
output_directory:"/Users/weiyuxiang/Desktop/Order/build",
)
#使用fir-cli上传ipa
sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T  fir的token"
end

end
复制代码

在测试的时候用
fastlane adhoc
上架则用
fastlane inhoc
到这里一般没有问题,但是楼主我还会遇到一个问题,最后用fir上传的时候会报这些错误:

/Users/weiyuxiang/.rvm/rubies/ruby-2.2.4/lib/ruby/gems/2.2.0/gems/fastlane-2.95.0/fastlane_core/lib/fastlane_core/ui/interface.rb:153:in `shell_error!': [!] Exit status of command 'fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T a5bd6574b7292220d5c4b44b6' was 1 instead of 0. (FastlaneCore::Interface::FastlaneShellError)
/Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:458:in `block in replace_bin_path': can't find executable fir for gem fir-cli. fir-cli is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:478:in `block in replace_bin_path'
from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/fir:22:in `<main>'
from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `eval'
from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `<main>'
复制代码

按照错误提示,在 Gemfile 文件里加一句: gem "fir" 即可


Fastlane 能做的事情还有很多,大家可以去好好看看Fastlane文档,研究一些高级的用法吧!

猜你喜欢

转载自juejin.im/post/5bff917ce51d450c487d2901