使用 fastlane 自动打包

fastlane.png

一、环境准备及安装步骤

1、确保已经安装 Xcode 和正确的 ruby 版本(最好都是最新版本)。

2、安装 xcode-select

xcode-select --install

3、安装 fastlane

sudo gem install fastlane --verbose

4、检查是否安装成功

fastlane -version

如果输出输出正确版本号则说明已安装成功。

二、使用 fastlane 进行自动打包

1、cd 到项目文件夹
例:我项目路径为 /Users/alan/Desktop/test

cd /Users/alan/Desktop/test

2、初始化 fastlane 在项目中到配置文件

fastlane init

根据提示输入 Apple ID 和密码以及项目到 App Identifier,其他配置也根据提示填写即可
注:也有可能遇到下面到错误

[16:21:03]: Couldn't find any schemes in this project, make sure that the scheme is shared if you are using a workspace
[16:21:03]: Open Xcode, click on Manage Schemes and check the Shared box for the schemes you want to use
[16:21:03]: Afterwards make sure to commit the changes into version control
[!] No Schemes found
如图:

No Schemes found.png

解决方法:

根据错误提示或者读 fastlane 文档得知,我们需要打开 Xcode 找到 schemes 的 Shared 属性打上勾就可以了。如图:
Manage Schemes.png

点击模拟器左边的项目名称弹出下啦菜单,选择 Manage Schemes 选项。


schemes.png

在 shared 下面的勾选框打上勾,完成;重新执行 fastlane init 命令。


确认项目配置.png

输入 Apple ID 核对项目信息,按照步骤来就可以,完成以后项目文件夹会多出一个名为 fastlane 的文件夹中有两个文件,分别是 Appfile 、Fastfile。

三、自动打包脚本

1、cd 到项目文件夹,新建脚本文件

vim autopacking.command

2、把下面到脚本粘贴到文件中,按照自己到项目和需要更改脚本中到项目名称、项目地址,打包方式、参数,导出地址等信息,修改完成之后保存推出。

#!/bin/bash

#更新timeout
export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120

#计时
SECONDS=0

#假设脚本放置在与项目相同的路径下
project_path="$(dirname "$(pwd)")"
#取当前时间字符串添加到文件结尾
now=$(date +"%m_%d")

#指定项目的scheme名称
scheme="test"
#指定打包方式Debug和Release两种
configuration="Release"
#指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development, 和developer-id,即xcodebuild的method参数
export_method='enterprise'

#指定项目地址
project_path="/Users/alan/Desktop/test/test.xcodeproj"
#指定项目地址(如果使用 cocopods)
#workspace_path="/Users/alan/Desktop/test/test.xcworkspace"
#指定输出路径
output_path="/Users/alan/Desktop/enterprisePackag"
#指定输出归档文件地址
archive_path="$output_path/test_${now}.xcarchive"
#指定输出ipa地址
ipa_path="$output_path/test_${now}.ipa"
#指定输出ipa名称
ipa_name="test_${now}.ipa"
#获取执行命令时的commit message
commit_msg="$1"

#输出设定的变量值
echo "===workspace path: ${workspace_path}==="
echo "===archive path: ${archive_path}==="
echo "===ipa path: ${ipa_path}==="
echo "===export method: ${export_method}==="
echo "===commit msg: $1==="

#先清空前一次build(如果使用 cocoapods 请把 --project ${project_path} 改为 --workspace ${workspace_path})
fastlane gym --project ${project_path} --scheme ${scheme} --clean --configuration ${configuration} --archive_path ${archive_path} --export_method ${export_method} --output_directory ${output_path} --output_name ${ipa_name}

#上传到fir
#fir publish ${ipa_path} -T fir_token -c "${commit_msg}"

#输出总用时
echo "===Finished. Total time: ${SECONDS}s==="
exit;

3、给脚本添加可执行权限

chmod 777 autoPacking.command

4、动一下鼠标双击 autoPacking.command 文件,喝杯咖啡,包已准备好。

参考链接:
https://docs.fastlane.tools
http://www.jianshu.com/p/54ab07f2e63b

推荐阅读更多精彩内容