概述
APP自動化打包常見的主流工具有Jenkins
助赞、fastlane
买羞。Jenkins
功能強大,但是需要的配置也比較多雹食,團隊較大的可以優(yōu)先考慮畜普,fastlane
是用Ruby語言編寫的一套自動化工具集,比較輕便群叶,配置簡單吃挑,使用起來也很方便
fastlane安裝
- 第一步
安裝Ruby開發(fā)環(huán)境
ruby -v
- 第二步
安裝Xcode
命令行工具
xcode-select --install
- 第三步
安裝fastlane
sudo gem install -n /usr/local/bin fastlane
fastlane 配置
進入到你的iOS項目目錄下,執(zhí)行fastlane
初始化命令
fastlane init
命令執(zhí)行完畢后街立,fastlane 給我們提示讓我們選擇需要執(zhí)行哪種操作
[?] ??
[?] Looking for iOS and Android projects in current directory...
[16:54:04]: Created new folder './fastlane'.
[16:54:04]: Detected an iOS/macOS project in the current directory: 'xxxx.xcworkspace'
[16:54:04]: -----------------------------
[16:54:04]: --- Welcome to fastlane ?? ---
[16:54:04]: -----------------------------
[16:54:04]: fastlane can help you with all kinds of automation for your mobile app
[16:54:04]: We recommend automating one task first, and then gradually automating more over time
[16:54:04]: 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
?
- 第一種獲取
App Store
的App
預覽照片舶衬。 - 第二種打包上傳至
TestFlight
工具上。 - 第三種打包上傳到
App Store
赎离。 - 第四種自定義打包方式逛犹。
這里我們選擇第3種 或者第4種,執(zhí)行后梁剔,會提示我們輸入appleID 開發(fā)者賬號
虽画、密碼
、驗證碼
等操作荣病,然后一直到結(jié)束码撰。
我們會在項目的根目錄看到一個fastlane
的文件夾,里面包含Appfile
和 Fastfile
文件,外部還有Gemfile
和 Gemfile.lock
等新增文件;
Appfile: 存儲有關(guān)開發(fā)者賬號相關(guān)信息
Fastfile: 核心文件个盆,用于命令行調(diào)用和處理具體的流程脖岛,lane相對于一個action方法或函數(shù)
Gemfile 類似于cocopods 的Podfile文件
Appfile 文件配置如下:
基本上通過終端輸入蘋果開發(fā)者賬號
和 密碼
等一系列操作后,這個文件的配置基本不需要修改砾省;
app_identifier("com.****.****") # The bundle identifier of your app
apple_id("********@qq.com") # Your Apple email address
itc_team_id("*******") # App Store Connect Team ID
team_id("*******") # Developer Portal Team ID
# For more information about the Appfile, see:
# https://docs.fastlane.tools/advanced/#appfile
Fastlane + pgyer 配置
因為這里需要用到一個第三方的應(yīng)用分發(fā)平臺蒲公英
鸡岗,所以需要通過fastlane
添加其它插件。
fastlane add_plugin pgyer
fastlane add_plugin versioning
這里先配置一個 Release
版本的打包编兄,然后通過 pgyer
上傳的配置
定義打包平臺
default_platform :ios
# 參數(shù)定義
app_scheme = "[scheme]" #這里雙引號內(nèi)填寫自己的參數(shù) ([]只是提示)
app_workspace = "[project.xcworkspace]"
app_identifier = "[app_identifier]"
# 打包日期拼寫版本號
dateTime = Time.new.strftime("%Y%m%d")
version = get_version_number #獲取版本號
buildNumber = get_build_number # 獲取build號
fileName = "#{version}_#{buildNumber}_#{dateTime}.ipa"
platform :ios do
before_all do
git_pull
last_git_commit
sh "rm -f ./Podfile.lock"
cocoapods(use_bundle_exec: false)
end
# 以下是Release版本的配置
lane :release_pgyer do |options|
sigh(app_identifier: app_identifier) #項目的bundle identifler
build_app(
workspace: app_workspace,
configuration: "Release",
scheme: app_scheme,
silent: true, #在構(gòu)建時隱藏不必要輸出的信息
clean: true, #在構(gòu)建前是否clean工程
include_bitcode:false, #是否開啟bitecode
output_directory:"./packets" , #ipa的輸出路徑
output_name:fileName, #ipa的文件名(上面??定義)
export_method: "app-store", #打包類型
export_options: {
# 描述文件匹配哪個 identifier 和 profile文件名
provisioningProfiles: { "com.*****.live" => "recoder_release_profile"}
}
)
pgyer(api_key: "30ef1623**********07fbd79") # 開始上傳蒲公英及相關(guān)配置
end
end
蒲公英API
參數(shù)的獲取地址 https://www.pgyer.com/account/api
執(zhí)行命令小試牛刀
fastlane release_pgyer
執(zhí)行結(jié)果如下:
21:42:44]: Upload success. Visit this URL to see: https://www.pgyer.com/v9GR
+------+----------------------+-------------+
| fastlane summary |
+------+----------------------+-------------+
| Step | Action | Time (in s) |
+------+----------------------+-------------+
| 1 | default_platform | 0 |
| 2 | get_version_number | 0 |
| 3 | get_build_number | 0 |
| 4 | last_git_commit | 0 |
| 5 | rm -f ./Podfile.lock | 0 |
| 6 | cocoapods | 1 |
| 7 | sigh | 5 |
| 8 | build_app | 47 |
| 9 | pgyer | 8 |
+------+----------------------+-------------+
[21:42:44]: fastlane.tools finished successfully ??
經(jīng)過一堆log
日志輸出后轩性,會顯示succes
成功的提示;
在項目的 packets/
路徑下狠鸳,能看到我們的ipa
包已經(jīng)打包成功了,ipa
的命名方式是 版本號+ build號 +日期
的格式揣苏;
在蒲公英平臺上可以查看我們的ipa
是否上傳成功,這里我們貼一下通過 fastlane + pgyer
的方式上傳成功的截圖件舵;
Fastlane + TestFlight 配置
除了蒲公英卸察,我們絕大部分時候需要通過 App Connect
網(wǎng)站上傳Release
版本的ipa
,或者通過TestFlight
分發(fā)內(nèi)測包铅祸,這里我們提供一下 Fastlane + TestFlight
的配置, 當然這里還有一個坑點坑质,就是有的蘋果開發(fā)者賬號
開啟了雙重認證
合武,需要額外做一些操作;
配置如下:
# 定義打包平臺
default_platform :ios
# 參數(shù)定義
app_scheme = "[****scheme]"
app_workspace = "****.xcworkspace"
app_identifier = "com.****.www"
# 打包日期拼寫版本號
dateTime = Time.new.strftime("%Y%m%d")
version = get_version_number #獲取版本號
buildNumber = get_build_number # 獲取build號
fileName = "#{version}_#{buildNumber}_#{dateTime}.ipa"
platform :ios do
before_all do
git_pull
last_git_commit
sh "rm -f ./Podfile.lock"
cocoapods(use_bundle_exec: false)
end
# 提交一個新的Beta版本
lane :beta do |options|
sigh(app_identifier: app_identifier) #項目的bundle identifler
build_app(
workspace: app_workspace,
configuration: "Release",
scheme: app_scheme,
silent: true, #在構(gòu)建時隱藏不必要輸出的信息
clean: true, #在構(gòu)建前是否clean工程
include_bitcode:false, #是否開啟bitecode
output_directory:"./packets" , #ipa的輸出路徑
output_name:fileName, #ipa的文件名(上面??定義)
export_method: "app-store", #打包方式
export_options: {
# 描述文件匹配哪個 identifier 和 profile文件名
provisioningProfiles: { "com.****.www" => "描述文件名稱"}
}
)
#蘋果賬號鑒權(quán)
api_key = app_store_connect_api_key(
key_id: "C5P****KHG",
issuer_id: "8a88fec8*************5ce96430a21",
key_filepath: "./certificate/AuthKey_C5PG*****HG.p8",
duration: 1200, # optional (maximum 1200)
in_house: false # optional but may be required if using match/sigh
)
#上傳到TestFlight
upload_to_testflight(
api_key: api_key,
skip_waiting_for_build_processing: true,
ipa: "./packets/#{fileName}",
skip_submission:true
)
end
執(zhí)行命令:
fastlane beta
執(zhí)行結(jié)果:
在終端看到 執(zhí)行success后,能在蘋果開發(fā)者后臺 https://appstoreconnect.apple.com/ 網(wǎng)站看到 我們剛才自動打包并上傳的ipa
涡扼;并且在TestFlight
內(nèi)測的用戶會收到相應(yīng)的通知稼跳;
蘋果賬號雙重認證問題
關(guān)于蘋果賬號的雙重認證問題,需要我們登陸Developer
后臺中生成一個API密鑰吃沪,在App Store Connect -> 用戶與訪問
中進行配置汤善;并把xxxxxxxx.p8
文件保存下來(xxxxxxxx.p8
文件只能下載一次)
Question1 工程版本號 和build number 導致的問題
[17:33:41]: Before being able to increment and read the version number from your Xcode project, you first need to setup your project properly. Please follow the guide at https://developer.apple.com/library/content/qa/qa1827/_index.html
(MARKETING_VERSION):表示使用build Setting中的Build的值;
配置如下:
Question2
+---------------------+-----------------------------------------------------+
| Lane Context |
+---------------------+-----------------------------------------------------+
| DEFAULT_PLATFORM | ios |
| VERSION_NUMBER | 23 |
| BUILD_NUMBER | 23 |
| PLATFORM_NAME | ios |
| LANE_NAME | ios debug_pgyer |
| CERT_FILE_PATH | /Users/pengchao/Desktop/zheshi/ios_recoder/25KJ9FK |
| | Q2Q.cer |
| CERT_CERTIFICATE_ID | 25KJ9FKQ2Q |
+---------------------+-----------------------------------------------------+
[18:03:59]: Could not find a matching code signing identity for type 'Development'. It is recommended to use match to manage code signing for you, more information on https://codesigning.guide. If you don't want to do so, you can also use cert to generate a new one: https://fastlane.tools/cert
解決辦法:
總結(jié)+后續(xù)
關(guān)于一些不清楚的參數(shù)可以參考 fastlane 官網(wǎng)的文檔票彪,項目中首先保證自己這臺Mac 的配置 是可以正常打包的红淡,否則很容易遇到問題;
參考文章
https://docs.fastlane.tools
iOS Fastlane 使用文檔
Fastlane的集成和使用 包括 ios/mac 項目示例
fastlane自動化打包iOS APP