美文网首页iOS进阶待学习iOS开发笔记
使用fastlane gym/xctool编写ipa打包脚本

使用fastlane gym/xctool编写ipa打包脚本

作者: xi_lin | 来源:发表于2016-01-21 00:54 被阅读11773次

2017/09/29更新
修改project_path在新版fastlane脚本中的获取方式

2017/05/31更新
添加FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT设置

2017/02/27更新
fastlane更新后统一了各工具的使用,文章做了相应命令的更新

2016/04/21更新
文中所列脚本也更新到了https://github.com/xilin/ios-build-script

2016/03/14更新

使用fastlane gym打包

  1. 安装gym

sudo gem install fastlane

  1. 新脚本
  • 不再需要手工指定provisioning profile了
  • 打包核心命令从三行变为一行
#!/bin/bash

#更新timeout
export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120

#计时
SECONDS=0

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

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

#上述scheme/target/configuration若不清楚值可以用`xcodebuild -list`查看

#指定项目地址
workspace_path="$project_path/Demo.xcworkspace"
#指定输出路径
output_path="/Users/your_username/Documents/"
#指定输出归档文件地址
archive_path="$output_path/Demo_${now}.xcarchive"
#指定输出ipa地址
ipa_path="$output_path/Demo_${now}.ipa"
#指定输出ipa名称
ipa_name="Demo_${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
fastlane gym --workspace ${workspace_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==="

目标

  • 对于一个使用CocoaPods做依赖管理的Xcode项目,编写一个脚本实现自动打包ipa文件,并上传到fir上。

准备环境

  1. 安装xctool

brew install xctool

  1. 安装fir-cli
  • fir的命令行工具
  • 需要先装好ruby再执行

gem install fir-cli

一些概念

  1. *.workspace文件
    使用CocoaPods管理的工程在执行完pod install之后生成的workspace文件
  2. scheme
    显示在Xcode的左上角,run图标的右边,设备选择的左边
  3. configuration
    在Xcode中选项目,Info tab下会列出所有的configuration,一般有Debug和Release两种
  4. provisioning profile
    在Xcode中选target,在Build Settings tab下搜索Provisioning Profile,默认应该是Automatic,点击看到下拉列表中的就是所有可用的名称

脚本的步骤

  1. 清理项目
  2. 生成archive
  3. 导出ipa
  4. 上传到fir

最终脚本

#!/bin/bash

#计时
SECONDS=0

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

#指定项目的scheme名称
scheme="DemoScheme"
#指定要打包的配置名
configuration="Adhoc"
#指定打包所使用的provisioning profile名称
provisioning_profile='AdHoc Profile'

#指定项目地址
workspace_path="$project_path/Demo.xcworkspace"
#指定输出路径
output_path="/Users/your_username/Documents/"
#指定输出归档文件地址
archive_path="$output_path/Demo_${now}.xcarchive"
#指定输出ipa地址
ipa_path="$output_path/Demo_${now}.ipa"
#获取执行命令时的commit message
commit_msg="$1"

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

#先清空前一次build
xctool clean -workspace ${workspace_path} -scheme ${scheme} -configuration ${configuration}

#根据指定的项目、scheme、configuration与输出路径打包出archive文件
xctool build -workspace ${workspace_path} -scheme ${scheme} -configuration ${configuration} archive -archivePath ${archive_path}

#使用指定的provisioning profile导出ipa
#我暂时没找到xctool指定provisioning profile的方法,所以这里用了xcodebuild
xcodebuild -exportArchive -archivePath ${archive_path} -exportPath ${ipa_path} -exportFormat ipa -exportProvisioningProfile "${provisioning_profile}"

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

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

Refer

相关文章

网友评论

  • zidon:报错了 './fastlane/Gymfile' on line 48: (eval):48: `${' is not allowed as a global variable name 提示这段东西 Xcode9.1 能帮忙解决一下么
    xi_lin:这个感觉和Xcode无关,你跑别的fastlane命令正常不?比如直接跑fastlane gym
  • findM:commit_msg="$1" 这条什么意思?什么原理呢?
    findM:@xi_lin 嗯嗯 已经验证了 谢谢解答
    xi_lin:$1是shell脚本执行时候传入的参数
  • 855ba377df78:博主,请问一下,用gym打包时,可以自己指定证书和PP文件么?
  • Twenty_:没看到用到 fastlane啊 你这是用的 脚本
    xi_lin:@Twenty_ 我还没写过extension,有具体错误提示不?
    Twenty_:假若有扩展的情况下, 如何打一个ad-hoc包呢? 现在卡在gym选择证书的情况
    xi_lin:安装gym就是在用fastlane
  • SpringAnimation:!] Syntax error in your Fastfile on line 63: Fastfile:63: `$(' is not allowed as a global variable name
    Fastfile:63: syntax error, unexpected end-of-input
    project_path=$(pwd)这什么什么情况
    xi_lin:project_path="$(dirname "$(pwd)")" 修改一下
  • SpringAnimation:!] Syntax error in your Fastfile on line 63: Fastfile:63: `$(' is not allowed as a global variable name
    Fastfile:63: syntax error, unexpected end-of-input
    project_path=$(pwd)
  • 断剑:请问一下如果项目没有使用cocoapods的话,脚本应该怎么来写啊
    xi_lin:@断剑 应该设app-store。你configuration设对了吗?
    断剑:@xi_lin 恩,我现在用了一个简单的方法解决了,可一直接打包传到蒲公英。如果我要是上传到appstore的话,这个export_method方法参数怎么设置啊?我这边设置了app-store报错
    xi_lin:用fastlane gym -p 指定 project
  • 1050efb05b38:用你第一个.SH,遇到这个报错:
    [!] xcodebuild -showBuildSettings timed-out after 10 seconds and 3 retries. You can override the timeout value with the environment variable FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT, and the number of retries with the environment variable FASTLANE_XCODEBUILD_SETTINGS_RETRIES
    xi_lin:@Jane_01bc 我更新了,你看看文章,就加在开头就行
    0d54bff1ded5:@xi_lin 怎么加
    xi_lin:新版需要加个这个

    export FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT=120
  • Lonely__M:总时间 SECONDS为0
    xi_lin:$SECONDS是base内置变量,不应该为0的 http://tldp.org/LDP/abs/html/internalvariables.html
  • SuyChen:可以打出ipa包,但是上传不了fir???
    xi_lin:那要看你fir的token写对了没
  • 00fce043cf44:一切就绪完毕后上传fir报错 ,你看我这修改的有问题吗?
    fir publish ${ipa_path} -T "53651903eb5fa8cXXc798074XXXdb" -c "${commit_msg}"
    报错信息
    /Users/XXX/Documents/BestPlayer_2017_04_06_15_23_19.ipa
    /Users/panhongliu/Desktop/dabao.sh: line 42: fir: command not found
    00fce043cf44:啊 知道了 没安装 fir-cli找不到命令。。。
  • jkpang:升级Xcode8.3后, 按照文中的方法使用fastlane gym自动打包, 项目编译成功了,但是导出ipa包失败. 请问楼主有遇到这种情况吗?以下是控制台错误打印:
    xattr: No such file: ~/Desktop/DianCan-IPA/DianCan_2017_03_31_19_32_55.xcarchive
    [19:37:02]: Successfully stored the archive. You can find it in the Xcode Organizer.
    [19:37:02]: The generated archive is invalid, this can have various reasons:
    [19:37:02]: Usually it's caused by the `Skip Install` option in Xcode, set it to `NO`
    [19:37:02]: For more information visit https://developer.apple.com/library/ios/technotes/tn2215/_index.html
    [19:37:02]: Also, make sure to have a valid code signing identity and provisioning profile installed
    [19:37:02]: Follow this guide to setup code signing https://docs.fastlane.tools/codesigning/GettingStarted/
    [19:37:02]: If your intention was only to export an ipa be sure to provide a valid archive at the archive path.

    [!] Archive invalid
    leftw:@jkpang 我也是
    jkpang:@leftw 还没有,应该是证书配置的问题,在用fastlane cert配置项目证书的时候老是报SSL连接错误,升级了openssl也没用
    leftw:我也遇到了,你解决了没。
  • GeekFounder:你好,可以打包后自动上传到App Store吗?
    xi_lin:@SteveZhong 要上传store的话你可以结合fastlane deliver使用
  • 孤独的剑客:#指定要打包的配置名
    configuration="Adhoc"
    这个不起作用呢上传不到fir那边?
    a6ddb149eed6:楼主... 我每次都报这个错 sh: line 42: gym: command not found , 不知道为啥 我gym也装 了啊
    孤独的剑客:@xi_lin 最后都解决了
    xi_lin:这个config只是打包用的,和fir没有关系。你走到fir上传的那一步没?
  • Ever_Blacks:xctool 在Xcode 8. 不能用build了
    xi_lin:@Ever_Blacks 我现在都用gym啦,xctool好久没试了
  • 482435f300cf:我用gym 打的ipa包,要比xcode手动打的大3M,楼主你那里打出的ipa和手动的一样大么?
    xi_lin:@shunzi007 我这个版本gym打的包比xcode的大了70k
    xi_lin:@shunzi007 我这之前是差不多大的,最近没有比较过了。下次试试看。
  • 2eae4501ccb9:https://github.com/fastlane/fastlane/issues/519
    swift 的项目打包出来很大。。。 怎么办啊,楼主
    xi_lin:@2eae4501ccb9 我读了一下issues感觉问题其实应该是解决了的。你现在指定的export_method是什么?你用xctool/xcodebuild打出来的包是多大的?
  • 焚琴煮鹤de我:两种都试了,第一种97s,第二种182s,哈哈~~~
    口袋海贼王_:@焚琴煮鹤de我 脚本怎么执行啊
    焚琴煮鹤de我:@xi_lin 嗯是的,fastlane要快很多
    xi_lin:@焚琴煮鹤de我 第一种是用fastlane吧?
  • herbsun:谢谢博主分享,我有两个疑问,1:configuration = 'Release' 这样写可行吗?
    2指定打包所使用的provisioning profile名称 provisioning_profile='Automatic'这样写可以吗?为啥我找不到ipa包在哪呢 但是有xcarchive文件,谢谢
    xi_lin:@Herb_Sun :blush:
    herbsun:@xi_lin 太谢谢了 :grin:
    xi_lin:@Herb_Sun 1.如果你用的是默认XCode配置的话,可以。2.如果你要用Automatic的话我估计你可以忽略不写。不过还是推荐你打开XCode看看你的provisioning_profile叫什么。
    ipa包导出在ipa_path里,看你设置的是什么了。
  • 93d45d365eeb:自动打包
  • 徐小鸿同学:#上传到fir
    fir publish ${ipa_path} -T fir_token -c "${commit_msg}"
    这一段 是要加上fir的apitoken?
    xi_lin:@徐小鸿同学 你的provisioning profile选的是什么?我这adhoc包大小没有差别。你也可以解包对比一下
    徐小鸿同学:@xi_lin 这个打出的包比原始的方法大很多 你的有这种情况吗
    xi_lin:@徐小鸿同学 是的fir_token替换成你自己的api token就行

本文标题:使用fastlane gym/xctool编写ipa打包脚本

本文链接:https://www.haomeiwen.com/subject/pvylkttx.html