簡(jiǎn)介
Fastlane是一套使用Ruby寫的自動(dòng)化工具集,用于iOS和Android的自動(dòng)化打包桦卒、發(fā)布等工作立美,可以節(jié)省大量的時(shí)間。
官網(wǎng):https://fastlane.tools/
文檔:https://docs.fastlane.tools/
Github:https://github.com/fastlane/fastlane
前言
項(xiàng)目上線之際總需要打包測(cè)試方灾,每次使用
Xcode - Archive
等待好幾分鐘的Buliding建蹄,然后Export,通過Application Loader
上傳到AppStore
裕偿,或者分發(fā)到蒲公英洞慎,F(xiàn)ir
等發(fā)布平臺(tái)。一系列操作是耗時(shí)且沒有技術(shù)含量的工作嘿棘,所以自動(dòng)化打包ipa劲腿,很有必要掌握。點(diǎn)擊前往Github下載Demo
工具
腳本工具有很多鸟妙,這里使用的是
FastLane
焦人,Github里面24+K的Star和3.7K的Fork挥吵,說明大量的開發(fā)者信任并一起維護(hù)使用。
安裝
使用 FastLane
需要以下內(nèi)容:
OS X10.9
及以上版本Ruby2.0
及以上版本- Xcode命令行工具
(CLT)
- 付費(fèi)Apple開發(fā)者帳戶
FastLane
是Ruby
腳本的集合花椭,首先需要正確版本的Ruby
,從OS X10.9
及以后默認(rèn)Ruby2.0
忽匈。
我們可以通過命令確認(rèn),打開終端輸入如下命令:
ruby -v
檢測(cè)是否安裝了 Xcode CLT
打開終端輸入如下命令:
xcode-select --install
如果已經(jīng)安裝了Xcode CLT
矿辽,則會(huì)收到
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
如果未安裝丹允,它將為您安裝Xcode CLT
。
完成先決條件后袋倔,您就可以安裝FastLane
了雕蔽。 輸入以下命令:
sudo gem install -n /usr/local/bin fastlane --verbose
正常情況,輸入系統(tǒng)密碼之后宾娜,終端窗口會(huì)有一系列活動(dòng)萎羔,表明安裝正在進(jìn)行中,需要等待幾分鐘碳默。
備注:這里如果報(bào)錯(cuò)贾陷,參考文章最下面 踩坑一
配置FastLane
1、fastlane init
新建一個(gè)項(xiàng)目工程嘱根,名字暫定 fastlaneTest
髓废,cd 到項(xiàng)目根目錄執(zhí)行
fastlane init
這里會(huì)彈出四個(gè)選項(xiàng),問你想要用Fastlane做什么该抒? 這里我選的是3
1. ?? Automate screenshots
2. ????? Automate beta distribution to TestFlight (自動(dòng)testfilght型配置)
3. ?? Automate App Store distribution (自動(dòng)發(fā)布型配置)
4. ?? Manual setup - manually setup your project to automate your (需要手動(dòng)配置內(nèi)容)
如果你的工程是用cocoapods的那么可能會(huì)提示讓你勾選工程的Scheme慌洪;
步驟就是打開你的xcode,
點(diǎn)擊Manage Schemes凑保,
找到你的項(xiàng)目Scheme冈爹,在后面的多選框中進(jìn)行勾選,
然后可以手動(dòng)刪除 fastlane文件夾欧引,重新fastlane init一下频伤。
2、登錄Apple ID
[11:10:04]: ----------------------------------------------------------
[11:10:04]: --- Setting up fastlane for iOS App Store distribution ---
[11:10:04]: ----------------------------------------------------------
[11:10:04]: Parsing your local Xcode project to find the available schemes and the app identifier
[11:10:05]: $ xcodebuild -showBuildSettings -scheme fastlaneTest -project fastlaneTest.xcodeproj
[11:10:06]: $ cd /Users/hans3d/Desktop/fastlaneTest && agvtool what-version -terse
[11:10:07]: --------------------------------
[11:10:07]: --- Login with your Apple ID ---
[11:10:07]: --------------------------------
[11:10:07]: To use App Store Connect and Apple Developer Portal features as part of fastlane,
[11:10:07]: we will ask you for your Apple ID username and password
[11:10:07]: This is necessary for certain fastlane features, for example:
[11:10:07]:
[11:10:07]: - Create and manage your provisioning profiles on the Developer Portal
[11:10:07]: - Upload and manage TestFlight and App Store builds on App Store Connect
[11:10:07]: - Manage your App Store Connect app metadata and screenshots
[11:10:07]:
[11:10:07]: Your Apple ID credentials will only be stored in your Keychain, on your local machine
[11:10:07]: For more information, check out
[11:10:07]: https://github.com/fastlane/fastlane/tree/master/credentials_manager
[11:10:07]:
[11:10:07]: Please enter your Apple ID developer credentials
[11:10:07]: Apple ID Username:
輸入Apple Developer
的賬號(hào)和密碼芝此,因?yàn)?019.03蘋果開啟二級(jí)認(rèn)證憋肖,需要手機(jī)登錄開發(fā)者賬號(hào),進(jìn)行允許權(quán)限婚苹。
登陸成功如下:
[11:41:06]: ? Logging in with your Apple ID was successful
[11:41:06]: Checking if the app 'com.cz.****.fastlaneTest' exists in your Apple Developer Portal...
[11:41:07]: It looks like the app 'com.cz.****.fastlaneTest' isn't available on the Apple Developer Portal
[11:41:07]: for the team ID '9A77SS**JG' on Apple ID '****@163.com '
[11:41:07]: Do you want fastlane to create the App ID for you on the Apple Developer Portal? (y/n)
選擇 y
+----------------+--------------------------+
| Summary for produce 2.118.1 |
+----------------+--------------------------+
| username | ****@163.com |
| team_id | 9A77SS**JG |
| itc_team_id | 118370*** |
| platform | ios |
| app_identifier | com.cz.****.fastlaneTest |
| skip_itc | true |
| sku | 1552967*** |
| language | English |
| skip_devcenter | false |
+----------------+--------------------------+
然后會(huì)提示輸入App Name:
[11:43:46]: App Name: fastlane_wnkp_demo
然后根據(jù)操作點(diǎn)擊回車即可岸更!此時(shí)目錄結(jié)構(gòu)大概如下所示:
Appfile
,用于存儲(chǔ)應(yīng)用程序標(biāo)識(shí)符和Apple ID膊升。
Fastfile
怎炊,用于管理您創(chuàng)建的用于調(diào)用某些操作的通道。最主要的文件,在這個(gè)文件中可以編寫我們需要使用的各個(gè)工具的順序评肆、方式等赞咙。
3、配置腳本
通過Xcode打開Fastfile
文件如下
default_platform(:ios)
platform :ios do
desc "Push a new release build to the App Store"
lane :release do
build_app(scheme: "fastlaneTest")
upload_to_app_store(skip_metadata: true, skip_screenshots: true)
end
end
編寫內(nèi)容代碼如下:
default_platform(:ios)
platform :ios do
desc "fastlane 打包上傳到蒲公英發(fā)布網(wǎng)站"
lane :fsv_pgyer do
gym(
clean:true, # Clean項(xiàng)目
scheme:"fastlaneTest", # 項(xiàng)目名稱
export_method:"ad-hoc", # 打包的類型
configuration:"Release", # 模式糟港,默認(rèn)Release,還有Debug
output_directory:"./build", # 輸出的位置
)
end
end
一個(gè)lane就是一個(gè)任務(wù)院仿,
fsv_pgyer
名字可以自己改秸抚。
gym
是fastlane提供的打包工具,括號(hào)里面就是你自己配置的一些東西。gym科普
4歹垫、執(zhí)行命令剥汤、本地打包
最后就是打開終端,在你的工程目錄下排惨,運(yùn)行fastlane fsv_pgyer
就行了吭敢。
fastlane fsv_pgyer
編譯成功:
[15:49:55]: Successfully exported and compressed dSYM file
[15:49:55]: Successfully exported and signed the ipa file:
[15:49:55]: /Users/hans3d/Desktop/fastlaneTest/build/fastlaneTest.ipa
+------+------------------+-------------+
| fastlane summary |
+------+------------------+-------------+
| Step | Action | Time (in s) |
+------+------------------+-------------+
| 1 | default_platform | 0 |
| 2 | gym | 12 |
+------+------------------+-------------+
[15:49:55]: fastlane.tools finished successfully ??
打開項(xiàng)目目錄,會(huì)發(fā)現(xiàn)有一個(gè)bulid文件夾暮芭,打開即是我們的ipa包
5鹿驼、上傳到蒲公英或者fir配置
1、cd到項(xiàng)目下辕宏,安裝pgyer插件執(zhí)行命令 畜晰、
fastlane add_plugin pgyer
執(zhí)行結(jié)果:
[15:55:52]: Plugin 'fastlane-plugin-pgyer' was added to './fastlane/Pluginfile'
[15:55:52]: It looks like fastlane plugins are not yet set up for this project.
[15:55:52]: fastlane will modify your existing Gemfile at path '/Users/hans3d/Desktop/fastlaneTest/Gemfile'
[15:55:52]: This change is necessary for fastlane plugins to work
[15:55:52]: Should fastlane modify the Gemfile at path '/Users/hans3d/Desktop/fastlaneTest/Gemfile' for you? (y/n)
y
[15:55:59]: Successfully modified '/Users/hans3d/Desktop/fastlaneTest/Gemfile'
[15:55:59]: Make sure to commit your Gemfile, Gemfile.lock and Pluginfile to version control
Installing plugin dependencies...
Successfully installed plugins
2、重新編寫項(xiàng)目目錄下的Fastfile文件瑞筐,如下:
default_platform(:ios)
platform :ios do
desc "fastlane 打包上傳到蒲公英發(fā)布網(wǎng)站"
lane :fsv_pgyer do
gym(
clean:true, # Clean項(xiàng)目
scheme:"fastlaneTest", # 項(xiàng)目名稱
export_method:"ad-hoc", # 打包的類型
configuration:"Release", # 模式凄鼻,默認(rèn)Release,還有Debug
output_directory:"./build", # 輸出的位置
)
time = Time.new.strftime("%Y%m%d")
version = get_version_number
api_key = "06e5563d5a8b94aac1c297426d351df6"
user_key = "974d1feb99ad49c955ed82a16ae2c21a"
usipaName = "Release_#{version}_#{time}.ipa"
pgyer(api_key: "#{api_key}", user_key: "#{user_key}")
end
end
pgyer中的
api_key
和user_key
為蒲公英的應(yīng)用->API中查看聚假,如下圖:
蒲公英配置 蒲公英文檔
2块蚌、cd到項(xiàng)目根目錄下,執(zhí)行命令:
fastlane fsv_pgyer
輸出結(jié)果:
[16:03:23]: Upload success. Visit this URL to see: https://www.pgyer.com/jnfC
+------+--------------------+-------------+
| fastlane summary |
+------+--------------------+-------------+
| Step | Action | Time (in s) |
+------+--------------------+-------------+
| 1 | default_platform | 0 |
| 2 | gym | 12 |
| 3 | get_version_number | 0 |
| 4 | pgyer | 1 |
+------+--------------------+-------------+
[16:03:23]: fastlane.tools finished successfully ??
打包上傳成功膘格,收到短信:
踩坑一
執(zhí)行命令sudo gem install -n /usr/local/bin fastlane --verbose
之后如下提示:
Could not find a valid gem 'fastlane' (>= 0), here is why:
Unable to download data from https://ruby.taobao.org - SSL_connect returned=1 errno=0 state=error: certificate verify failed (https://ruby.taobao.org/specs.4.8.gz)
錯(cuò)誤原因:
查看gem
源
gem sources
輸出結(jié)果:
*** CURRENT SOURCES ***
https://ruby.taobao.org
所以需要更換源
刪除之前的源
gem sources --remove https://ruby.taobao.org
增加新的源
gem sources -a https://gems.ruby-china.com
查看源:
*** CURRENT SOURCES ***
https://gems.ruby-china.com
然后再執(zhí)行:
sudo gem install -n /usr/local/bin fastlane --verbose
即可正常下載峭范、問題解決!1窦虎敦!
踩坑二
打包失敗還有需要開發(fā)者賬號(hào)配置: