iOS開發之使用fastlane工具實現自動化打包發布
1、Fastlane工具的簡介:
Fastlane是一套使用Ruby寫的自動化工具集,旨在簡化Android和iOS的部署過程,自動化你的工作流。它可以簡化一些乏味、單調、重復的工作,像截圖、代碼簽名以及發布App。可以使用 fastlane 上傳到firim和蒲公英。
2、Fastlane工具的功能分類:
Testing 測試相關
Building 打包
Screenshots 截圖
Project 項目配置
Code Signing 代碼簽名
Documentation 文檔
Beta 內測相關
Push 推送
Releasing your app 發布
Source Control Git工作流
Notifications 通知相關
Misc 其他的東西
3、Fastlane工具的安裝:
《1》檢查Ruby版本,需要2.0及以上版本,在終端輸入以下命令:
ruby -v
《2》需要註意的是需要將gem的source改為https://gems.ruby-china.org/,在終端輸入以下命令:
gem source
檢查結果為:
*** CURRENT SOURCES ***
https://ruby.taobao.org/
http://gems.ruby-china.org/
《3》檢查Xcode命令行工具是否安裝,在終端輸入以下命令:
xcode-select --install
如果沒有安裝會進行安裝,如果已經安裝則會提示如下:
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
解決辦法:
sudo xcode-select --switch /Library/Developer/CommandLineTools/
《4》安裝Fastlane,在終端輸入命令如下:
sudo gem install fastlane --verbose
如果安裝出現以下錯誤:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don‘t have write permissions for the /usr/bin directory.
解決辦法:
sudo gem install -n /usr/local/bin fastlane或sudo gem install fastlane -NV
《5》檢查Fastlane是否安裝正確,輸入以下命令:
fastlane --version
4、Fastlane工具的使用:
《1》使用命令切換到項目工程:
cd /Users/yh/Desktop/xxx/FloatingWindow_Demo
《2》初始化Fastlane:
fastlane init
如果初始化出現以下錯誤:
[!] Unable to locate Xcode. Please make sure to have Xcode installed on your machine
解決辦法:
在Xcode中沒有設置“Command Line Tools”:打開Xcode偏好設置,選擇"Location"選項卡,選擇相應的“Command Line Tools”即可。比如:Xcode9.1
《3》提示輸入開發者賬號、密碼, 登錄成功後會提示你輸入App名稱,是否需要下載你的App的metadata。點y等待就可以了:
完成後顯示結果為:
+----------------+--------------------------------+
| Summary for produce 2.98.0 |
+----------------+--------------------------------+
| username | admin@xxxxxx.com |
| team_id | xxxxxx |
| itc_team_id | xxxxxx |
| platform | ios |
| app_identifier | com.xxxxxx.FloatingWindow-Demo |
| skip_itc | true |
| sku | xxxxxx |
| language | English |
| skip_devcenter | false |
+----------------+--------------------------------+
註意事項:
A、會問你想使用fastlane做什麽?這裏我們輸入3,自動發布到Apple Store。
B、接著會問是否想用fastlane來管理你的app metadata。輸入y,fastlane則會下載現有的元數據和屏幕截圖。輸入n,不做任何操作,仍然能使用fastlane上傳app到App Store。
C、如果最後出現fastlane release,就表示init成功了。
5、Fastlane工具組成的內容:
《1》初始化成功後會在當前工程目錄生成一個fastlane文件夾和一個Gemfile文件, 文件夾目錄為
《2》Appfile:存放App的apple_id,team_id, app_identifier等信息
《3》Deliverfile:存放發布的配置信息
《4》Fastfile:工作編輯文件
《5》metadata:App元數據
《6》screenshots:商店應用截圖
6、操作並編輯Fastfile文件:
# 自動更新fastlane 工具
# update_fastlane
#需要的fastlane的最小版本,在每次執行之後會檢查是否有新版本,如果有會在最後末尾追加新版本提醒
fastlane_version "2.98.0"
default_platform(:ios)
platform :ios do
#lane之前的操作
before_all do
# ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
cocoapods
end
#執行lane成功後的回調
after_all do |lane|
# slack(
# message: "Successfully deployed new App Update."
# )
end
desc "提交一個新的Beta版本到 Apple TestFlight"
desc "This will also make sure the profile is up to date"
lane :beta do
# match(type: "appstore") # more information: https://codesigning.guide
gym(scheme: "FloatingWindow_Demo") # Build your app - more options available
pilot
# sh "your_script.sh"
end
desc "部署一個新版本到App Store"
lane :release do
# match(type: "appstore")
# snapshot
gym(scheme: "FloatingWindow_Demo") # Build your app - more options available
deliver(force: true)
# frameit
end
# 你可以定義自己的lane
desc "打包到蒲公英"
lane :test do |options|
gym(
clean:true, #打包前clean項目
export_method: "ad-hoc", #導出方式
scheme:"shangshaban", #scheme
configuration: "Debug",#環境
output_directory:"./app",#ipa的存放目錄
output_name:get_build_number()#輸出ipa的文件名為當前的build號
)
#蒲公英的配置 替換為自己的api_key和user_key
pgyer(api_key: "xxxxxxx", user_key: "xxxxxx",update_description: options[:desc])
end
# 如果流程發生異常會走這裏並終止
error do |lane, exception|
# slack(
# message: exception.message,
# success: false
# )
end
end
7、Fastlane插件命令:
《1》列出所有可用插件
fastlane search_plugins
《2》搜索指定名稱的插件:
fastlane search_plugins [query]
《3》添加插件:
fastlane add_plugin [name]
《4》安裝插件:
fastlane install_plugins
其它命令字:
gym:編譯打包生成 ipa 文件
sigh:可以生成並下載開發者的 App Store 配置文件
deliver:用於上傳應用的二進制代碼,應用截屏和元數據到 App Store
snapshot:可以自動化iOS應用在每個設備上的本地化截屏過程
scan:自動化測試工具,很好的封裝了 Unit Test
match :同步團隊每個人的證書和 Provision file 的超贊工具
8、使用Fastlane上傳到蒲公英:
《1》打開終端輸入命令:
fastlane add_plugin pgyer
《2》新建一個定義自己的lane,option用於接收我們的外部參數,這裏可以傳入當前build的描述信息到蒲公英平臺:
desc "打包到蒲公英"
lane :test do |options|
gym(
clean:true, #打包前clean項目
export_method: "ad-hoc", #導出方式
scheme:"FloatingWindow_Demo", #scheme
configuration: "Debug",#環境
output_directory:"./app",#ipa的存放目錄
output_name:get_build_number()#輸出ipa的文件名為當前的build號
)
sigh( #發布打包
clean:true, #打包前clean項目
export_method: "app-store”, #導出方式
scheme:"FloatingWindow_Demo", #scheme
configuration: “Release”,#環境
output_directory:"./app",#ipa的存放目錄
output_name:get_build_number()#輸出ipa的文件名為當前的build號
)
#蒲公英的配置 替換為自己的api_key和user_key
pgyer(api_key: "xxxxxxxxxxxx", user_key: "xxxxxxxxxxxx",update_description: options[:desc])
end
《3》在工作目錄的終端執行命令:
fastlane test desc:測試蒲公英打包
9、使用Fastlane上傳到firim:
《1》打開終端輸入命令:
fastlane add_plugin firim
《2》新建一個定義自己的lane
desc "打包到fir”
lane :test2 do |options|
gym(#測試打包
clean:true, #打包前clean項目
export_method: "ad-hoc", #導出方式
scheme:"FloatingWindow_Demo", #scheme
configuration: "Debug",#環境
output_directory:"./app",#ipa的存放目錄
output_name:get_build_number()#輸出ipa的文件名為當前的build號
)
sigh( #發布打包
clean:true, #打包前clean項目
export_method: "app-store”, #導出方式
scheme:"FloatingWindow_Demo", #scheme
configuration: “Release”,#環境
output_directory:"./app",#ipa的存放目錄
output_name:get_build_number()#輸出ipa的文件名為當前的build號
)
#firim的配置 替換為自己的api_key和user_key
firim(firim_api_token: “xxxxxxxxxxxx”)
end
《3》在工作目錄的終端執行命令:
fastlane test2 desc:測試firim打包
iOS開發之使用fastlane工具實現自動化打包發布