关于fastlane
是什么,怎么安装,网上已经有很多文章,请自行baidu
Google
fastlane 官网
最终想要实现的流程执行命令-->打包-->上传蒲公英-->提取下载链接二维码地址-->展示下载二维码
- 首先进入工程根目录执行 根据提示操作完成初始化,最终会在工程根目录下生成一个
fastlane
文件夹以及里面相关所需的文件
fastlane init
- 打开
fastlane
文件夹中的Fastfile
文件,按照需要编写,我的工程是配置了多target
,所以需要对每次打包的target
做区分,同时内部测试也是用的企业账号,同样也要区分
fastlane_version "2.19.1"
default_platform :ios
# 上传企业证书包
def upload_inhouse(op)
scheme = ""
if op[:env] == "ci"
scheme = "ShowStart_3.0_PRO_CI"
elsif op[:env] == "dev"
scheme = "ShowStart_3.0_PRO_DEV"
else
scheme = "ShowStart_3.0_PRO"
end
sh "chmod +x ./uploadipa.sh && ./uploadipa.sh ../build/#{scheme}.ipa"
end
#上传线上证书包
def upload_ipa(op)
scheme = ""
if op[:env] == "ci"
scheme = "ShowStart_3.0_CI"
elsif op[:env] == "dev"
scheme = "ShowStart_3.0_DEV"
else
scheme = "ShowStart_3.0_PRO"
end
sh "chmod +x ./uploadipa.sh && ./uploadipa.sh ../build/#{scheme}.ipa"
end
# 企业证书打包
def prepare_inhouse(op)
scheme = ""
if op[:env] == "ci"
scheme = "ShowStart_3.0_PRO_CI"
elsif op[:env] == "dev"
scheme = "ShowStart_3.0_PRO_DEV"
else
scheme = "ShowStart_3.0_PRO"
end
gym(
scheme: "#{scheme}",
workspace: "ShowStart_3.0.xcworkspace",
clean: true,
export_method: "enterprise",
output_directory: "build",
include_bitcode: false,
configuration: "Release"
)
# sh "chmod +x ./uploadipa.sh && ./uploadipa.sh ../build/#{scheme}.ipa"
upload_inhouse(op)
end
# 正式环境证书打包
def prepare_adhoc(op)
scheme = ""
if op[:env] == "ci"
scheme = "ShowStart_3.0_CI"
elsif op[:env] == "dev"
scheme = "ShowStart_3.0_DEV"
else
scheme = "ShowStart_3.0_PRO"
end
gym(
scheme: "#{scheme}",
workspace: "ShowStart_3.0.xcworkspace",
clean: true,
export_method: "ad-hoc",
output_directory: "build",
include_bitcode: false,
configuration: "Release"
)
# sh "chmod +x ./uploadipa.sh && ./uploadipa.sh ../build/#{scheme}.ipa"
upload_ipa(op)
end
# 修改 版本号和build号
def prepare_version(op)
increment_version_number(
version_number: op[:v]
)
increment_build_number(
build_number: op[:b]
)
end
platform :ios do
before_all do
# ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
# cocoapods
# sh "chmod +x ./checkfile.sh && ./checkfile.sh"
end
desc "Build appstore"
lane :appstore do |op|
sh "chmod +x ./checkfile.sh && ./checkfile.sh"
prepare_version(op)
gym(
scheme: "ShowStart_3.0",
workspace: "ShowStart_3.0.xcworkspace",
clean: true,
export_method: "app-store",
output_directory: "build",
include_bitcode: false,
configuration: "Release"
)
end
desc "Build inhouse"
lane :pro do |op|
sh "chmod +x ./checkfile.sh && ./checkfile.sh"
prepare_inhouse(op)
end
desc "Build adhoc"
lane :adhoc do |op|
sh "chmod +x ./checkfile.sh && ./checkfile.sh"
prepare_adhoc(op)
end
desc "upload_inhouse"
lane :uploadpro do |op|
upload_inhouse(op)
end
desc "upload_ipa"
lane :upload do |op|
upload_ipa(op)
end
after_all do |lane|
end
error do |lane, exception|
end
end
- 如何执行上面定义的
lane
呢?
#在工程根目录下
fastlane pro env:ci
# 意思就是 执行定义的lane为 :pro 的
# env:ci 参数
# 然后就会安装你定义的lane里面流程运行
- 上传到蒲公英,如果上传成功则解析出安装地址的二维码链接,然后下载二维码图片,然后直接在
terminal
中展示
#!/bin/sh
echo "~~~~~~~~~~~~~~~~上传ipa到蒲公英~~~~~~~~~~~~~~~~~~~"
echo "ipa 文件路径"
echo $1
if [[ ! -f "$1" ]]
then
echo "ipa 文件不存在"
exit 0
fi
# 保存蒲公英上传结果
result=""
qrcodepath=""
# 上传蒲公英 并将结果赋值给 result变量
result=$(curl -F "file=@$1" \
-F "uKey=xxx" \
-F "_api_key=xxxx" \
-F "password=xxx" \
-F "isPublishToPublic=xx" \
https://qiniu-storage.pgyer.com/apiv1/app/upload --verbose)
if [ $? = 0 ]
then
echo "\n~~~~~~~~~~~~~~~~上传蒲公英成功~~~~~~~~~~~~~~~~~~~"
else
echo "\n~~~~~~~~~~~~~~~~上传蒲公英失败~~~~~~~~~~~~~~~~~~~"
exit 0
fi
# 提取二维码地址
# 删除 result 中的字符串 保留 "appQRCodeURL":" 之后的字符串
qrcodepath=${result#*\"appQRCodeURL\":\"}
# echo $qrcodepath
# 删除 qrcodepath 中的字符串 保留 "}} 之前的字符串
qrcodepath=${qrcodepath%\"\}\}*}
# echo $qrcodepath
# 替换 \/ 为 /
qrcodepath=${qrcodepath//\\/}
# echo $qrcodepath
# 验证提取出来的地址的前缀是不是 http
if [[ ! "$qrcodepath" =~ ^http ]]; then
#statements
echo "提取二维码地址不对"
exit 0
fi
# 下载二维码
echo "二维码地址:" $qrcodepath
# 使用 curl 下载 二维码图片并保存到当前目录下
curl -o code.jpg $qrcodepath
if [[ ! -f "./code.jpg" ]]
then
echo "二维码下载失败"
exit 0
else
echo "二维码下载成功"
# 显示二维码
# 调用 imgcat.sh 脚本 展示二维码
# imgcat 是网上找的一个 https://github.com/Dorian/imgcat/blob/master/imgcat
chmod +x ./imgcat.sh && ./imgcat.sh code.jpg
fi
exit 0
-
Fastfile
中调用的checkfile.sh
脚本,没啥内容就是检查目录的
#!/bin/sh
if [ -d "../build" ]
then
rm -rf ../build
fi
- 最终的效果
fastlane pro env:ci
# 稍等一下 执行命令-->打包-->上传蒲公英-->提取下载链接二维码地址-->展示下载二维码 全自动完成
- 上面的步骤其实还可以简化
# 比如专门配置一台Mac电脑并配置好`Jenkins`的一切,再搭配`fastlane`
# 配置Jenkins定时任务 比如每天 20:00 自动开始打包-->上传分发平台-->通知测试
# 也可给测试人员配置一个Jenkins账号,由测试人员登录Jenkins 自己选择性打包
# 也可以选择用NodeJs 写一个webserver 配合 git hook 在提交时写入特定的信息 webserver 提取到特定信息后 开始执行任务