一、安裝Fastlane
需要先安裝了 brew,打開終端,輸入以下命令
brew install fastlane
如果是 M1 電腦,輸入上面的命令有可能沒辦法安裝,需要按照提示輸出以下命令,非 M1 電腦可以忽略這段
arch -arm64 brew install fastlane
安裝完畢后,查看版本號確認(rèn)已安裝完成
fastlane -v
fastlane installation at path:
/opt/homebrew/Cellar/fastlane/2.182.0/libexec/gems/fastlane-2.184.0/bin/fastlane
-----------------------------
[?] ??
fastlane 2.184.0
二、初始化 Fastlane
類似于 cocoapods,我們需要在每個項目中初始化 fastlane
在終端進入到項目所在文件夾中
fastlane init
接下來會出現(xiàn)4個選項,需要我們根據(jù)自己需求選擇,這里我選擇 4:手動設(shè)置,因為是發(fā)布到蒲公英。
[?] ??
[?] Looking for iOS and Android projects in current directory...
[15:14:57]: Created new folder './fastlane'.
[15:14:57]: Detected an iOS/macOS project in the current directory: 'moyu.xcodeproj'
[15:14:57]: -----------------------------
[15:14:57]: --- Welcome to fastlane ?? ---
[15:14:57]: -----------------------------
[15:14:57]: fastlane can help you with all kinds of automation for your mobile app
[15:14:57]: We recommend automating one task first, and then gradually automating more over time
[15:14:57]: What would you like to use fastlane for?
1. ?? Automate screenshots
2. ???? Automate beta distribution to TestFlight
3. ?? Automate App Store distribution
4. ?? Manual setup - manually setup your project to automate your tasks
?
安裝成功提示
[15:21:57]: --------------------------------------------------------
[15:21:57]: --- ? Successfully generated fastlane configuration ---
[15:21:57]: --------------------------------------------------------
之后我們會在項目文件夾中發(fā)現(xiàn) fastlane 文件夾 和 Gemfile,說明初始化成功了。

如果初始化過程中,一直卡在
[15:17:36]: ------------------------------------------------------------
[15:17:36]: --- Setting up fastlane so you can manually configure it ---
[15:17:36]: ------------------------------------------------------------
[15:17:36]: Installing dependencies for you...
[15:17:36]: $ bundle update
control+C 取消當(dāng)前操作,然后打開 Gemfile 文件,將
source "https://rubygems.org"
修改為:
source "https://gems.ruby-china.com"
然后在終端輸入下面命令重新運行,等待更新
bundle update
三、配置 Fastlane 文件
打開 Fastlane 文件,根據(jù)自己項目的需要編輯相關(guān)內(nèi)容
default_platform(:ios)
platform :ios do
desc "打包上傳至蒲公英"
lane :build_to_pgyer do
# 需要打包的 target 名字
scheme = "xxxxx"
# 總共有4種打包方式選擇,根據(jù)自己需求選擇
# app-store :上架App Store
# ad-hoc :內(nèi)部測試
# development :內(nèi)部測試
# enterprise :企業(yè)證書-內(nèi)部測試
export_method = "enterprise"
# 允許訪問鑰匙串(有可能會用到)
export_xcargs = "-allowProvisioningUpdates"
# 獲取版本號、build,方便自動命名包名(根據(jù)需求添加)
version = get_info_plist_value(path: "./#{scheme}/Info.plist", key: "CFBundleShortVersionString")
build = get_info_plist_value(path: "./#{scheme}/Info.plist", key: "CFBundleVersion")
# ipa 包名稱,可以根據(jù)自己需求改變下面的組合
output_name = "#{scheme}_#{version}_#{build}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
# ipa 包導(dǎo)出路徑
output_directory = "/Users/xxxx/Desktop/Fastlane"
# 蒲公英賬號的 api_key 和 user_key
pgyer_api_key = "xxxxxxxxxxxxxxxxxxxx"
pgyer_user_key = "xxxxxxxxxxxxxxxxxxx"
# 打包配置
build_app(
scheme: scheme,
export_method: export_method,
export_xcargs: export_xcargs,
output_directory: output_directory,
output_name: output_name,
export_options: {
# provisioningProfiles是手動配置證書,如果項目種選擇了自動選擇證書,則不需要添加這段
# 如果項目中包含了 extension ,需要額外配置證書的才會用到
provisioningProfiles: {
"com.xxxx.xxx" => "證書名稱",
"com.xxxx.xxx.xxxx" => "證書名稱"
}
}
)
# 上傳到蒲公英
pgyer(api_key: pgyer_api_key, user_key: pgyer_user_key)
end
end
四、添加蒲公英插件:pgyer
注意,必須要在項目文件夾路徑下添加,否則打包成功后,會上傳失敗。
中間會要求修改 Gemfile,輸入 y 即可。
fastlane add_plugin pgyer
[?] ??
[16:23:53]: fastlane detected a Gemfile in the current directory
[16:23:53]: However, it seems like you didn't use `bundle exec`
[16:23:53]: To launch fastlane faster, please use
[16:23:53]:
[16:23:53]: $ bundle exec fastlane add_plugin pgyer
[16:23:53]:
[16:23:53]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[16:23:55]: Plugin 'fastlane-plugin-pgyer' was added to './fastlane/Pluginfile'
[16:23:55]: It looks like fastlane plugins are not yet set up for this project.
[16:23:55]: fastlane will modify your existing Gemfile at path '/Users/xxxx/Desktop/moyu/Gemfile'
[16:23:55]: This change is necessary for fastlane plugins to work
[16:23:55]: Should fastlane modify the Gemfile at path
'/Users/xxxx/Desktop/moyu/Gemfile' for you? (y/n)
y
[16:23:58]: Successfully modified '/Users/xxxx/Desktop/moyu/Gemfile'
看到上面的信息,說明已經(jīng)成功安裝了pgyer 插件
五、準(zhǔn)備工作完畢,開始打包
輸入以下命令,這里的 build_to_pgyer 就是 fastlane 文件中的方法名,之后可以根據(jù)自己的需求,多添加幾個不同的方法,用于打包不同名字的、不同環(huán)境的target。
fastlane build_to_pgyer
這個時候喝口水,就可以看到下面的信息
[16:35:29]: Successfully exported and compressed dSYM file
[16:35:29]: Successfully exported and signed the ipa file:
[16:35:29]: /Users//Desktop/Fastlane/moyu_1.0_1_20210528163516.ipa
[16:35:29]: -------------------
[16:35:29]: --- Step: pgyer ---
[16:35:29]: -------------------
[16:35:29]: The pgyer plugin is working.
[16:35:29]: build_file: /Users/xxxx/Desktop/Fastlane/moyu_1.0_1_20210528163516.ipa
[16:35:29]: Start upload /Users/xxxx/Desktop/Fastlane/moyu_1.0_1_20210528163516.ipa to pgyer...
[16:35:35]: Upload success. Visit this URL to see: https://www.pgyer.com/xxx
+------+----------------------+-------------+
| fastlane summary |
+------+----------------------+-------------+
| Step | Action | Time (in s) |
+------+----------------------+-------------+
| 1 | default_platform | 0 |
| 2 | get_info_plist_value | 0 |
| 3 | get_info_plist_value | 0 |
| 4 | build_app | 12 |
| 5 | pgyer | 6 |
+------+----------------------+-------------+
[16:35:35]: fastlane.tools finished successfully ??
至此,我們的打包已經(jīng)完成。
到輸出文件下,可以看到2個文件,一個是dSYM文件,一個是ipa包。
在上面的信息也可以獲取到蒲公英的下載地址。

六、其他問題
一般會卡主的地方出現(xiàn)在證書選擇上,特別是項目比較復(fù)雜包含了extension的,證書需要額外配置,這一步會卡比較久。
另外就是蒲公英插件的添加位置不要弄錯。
對于項目種有多個target,或者多個不同包名、不同環(huán)境的,建議每個target對應(yīng)一個 lane 方法,打包時可以分別打包,這樣比較方面。
基本上 fastlane 可以做到一次配置,多次使用,非常的方便。