自动化打包工具集Fastlane

2018-05-29  本文已影响175人  0o冻僵的企鹅o0

更新于20181025,
最近更新:

  1. export_options,
  2. fir上传命令修改

什么是Fastlane

Fastlane是一套使用Ruby写的自动化工具集,旨在简化Android和iOS的部署过程,自动化你的工作流。它可以简化一些乏味、单调、重复的工作,像截图、代码签名以及发布App.

如何安装Fastlane

  1. 安装xcode命令行工具
xcode-select --install

如果没有安装,会弹出对话框,点击安装;

如果已经安装,就会提示

xcode-select: error: command line tools are already installed, use "Software Update" to install updates。

  1. 安装Fastlane

使用下面的命令

sudo gem install fastlane -NV

或是

brew cask install fastlane

来安装Fastlane。

安装完成后,可以执行下面命令,检查是否安装好。

fastlane --version
  1. 初始化Fastlane

cd到你的项目目录执行

fastlane init

过一会会出现如下提示,让你选择一个选项:


我这里希望打包上传到app store,所以选择了3.

如果你的工程是用cocoapods的那么可能会提示让你勾选工程的Scheme,步骤就是打开你的xcode,点击Manage Schemes,在一堆三方库中找到你的项目Scheme,在后面的多选框中进行勾选,然后rm -rf fastlane文件夹,重新fastlane init一下就不会报错了。

接着会提示你输入开发者账号和密码。

[20:48:55]: Please enter your Apple ID developer credentials
[20:48:55]: Apple ID Username:

登录成功后会提示你是否需要下载你的App的metadata。点y等待就可以。

初始化成功以后,就会生成一个如下图所示的fastlane文件夹:


fastlane

其中metadatascreenshots分别对应App元数据和商店应用截图。

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 : release do
  app_identifier "com.bbb.bbb"
  apple_id "bbb@bbb.com"
  team_id "AAAAAAAAAA"
end

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

Deliverfile

Deliverfile中为发布的配置信息,一般情况用不到。

Fastfile

Fastfile是我们最应该关注的文件,也是我们的工作文件。
下面是我的fastfile文件中上传至app store的lane:

platform :ios do
#------------------------------APP STORE-----------------------------------
desc "Push a new release build to the App Store"
lane :to_appStore do
#gym用来编译ipa
gym(clean:true,# 编译前执行 clean,可减少 ipa 文件大小 
scheme: "MobileChecking",#要编译的scheme名称
export_method: "app-store", # Xcode 9增加export_method标签
silent: true, # 隐藏没有必要的信息
output_directory: "./fastlane/appstoreIPA", # ipa输出目录
output_name:"mobileChecking", #输出的ipa名称
archive_path:"./fastlane/appstoreIpaArchive", #archive文件导出地址 
export_xcargs: "-allowProvisioningUpdates",
export_options: {
        method: "ad-hoc",
            provisioningProfiles: {
                "com.whty.xxxx" => "对应provisioningProfiles文件名",
            }
        }
) 
upload_to_app_store
end

其中一个lane就是一个任务,里面是一个个的action组成的工作流。

export_method:指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development, 和developer-id,即xcodebuild的。

如何使用Fastlane

定义完lane之后怎么执行呢?打开终端,切换到项目的根目录:执行fastlane [lane'name]就可以了。比如我的lane名称叫appStore,那么久执行如下命令:

fastlane  appStore

或者采用下面的命令会更快:

bundle exec fastlane  appStore

成功之后会在相应的路径下生成ipa文件,并会自动上传至app store。

如何配置Fastfile

上传到fir

安装 插件

bundle exec fastlane add_plugin fir 

安装fir

使用如下命令,安装fir:

sudo gem install fir-cli

如果报如下错误:

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

可以改为:

sudo gem install -n /usr/local/bin fir-cli

安装完成以后,登录fir:

fir login

会让你输入你的fir.im API Token,去fir网站即可获得此token。

fir上传的lane:

#-----------------------------------FIR-------------------------------

desc "Push a new release build to the FIR"
lane :to_fir do
#build_app(workspace: "MobileChecking.xcworkspace", scheme: "MobileChecking")
#gym用来编译ipa
gym(clean: true,  # 编译前执行 clean,可减少 ipa 文件大小 
scheme: "MobileChecking", # 要编译的scheme名称
export_method: "ad-hoc", # Xcode 9增加export_method标签
silent: true, # 隐藏没有必要的信息
output_directory: "./fastlane/firIPA", # ipa输出目录
output_name:"mobileChecking", # 输出的ipa名称
archive_path:"./fastlane/firIpaArchive", #archive文件导出地址 
export_xcargs: "-allowProvisioningUpdates", 
export_options: {
        method: "ad-hoc",
            provisioningProfiles: {
                "com.xxxx" => "对应provisioningProfiles文件名",
            }
        }
) 
# 上传ipa到fir.im服务器,在fir.im获取firim_api_token
#firim(firim_api_token: "451d867c8860da31exxxxxxxxxxxx")
fir(
    api_token: '451d867c8860da31e5e4xxxx',
    apk_path: "./fastlane/firIPA/Communication.ipa", #生成ipa路径
    changelog: 'hehe123' #上传日志
)
end

如果你想上传到蒲公英,可以参考这篇文章:使用 fastlane 实现 iOS 持续集成(二)

常见错误处理

  1. FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT
[14:57:59]: fastlane finished with errors

[!] xcodebuild -showBuildSettings timed out after 4 retries with a 
base timeout of 3. You can override the base timeout value with the 
environment variable FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT, and the 
number of retries with the environment variable 
FASTLANE_XCODEBUILD_SETTINGS_RETRIES 

在遇到这个错误之后,在命令窗口运行下面代码来更新timeout时间:

#更新timeout
export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120

参考

上一篇下一篇

猜你喜欢

热点阅读