使用fastlane工具自動打包發布App Store或蒲公英測試平臺
?在以往的iOS開發過程中,我們大都使用xcode提供的工具進行手動打包ipa,這種打包方式會帶來大量時間上的浪費,想必對于有追求的開發者來說時間都是寶貴的。那么,有沒有一種方式可以通過一行指令自動完成打包呢?答案是:有的,通過筆者行走于江湖多年,得到了fastlane
的這個開源的神器,可以到以下地址進行查看參考和學習:
- Github:https://github.com/fastlane/fastlane
- 官網:https://fastlane.tools/
- 文檔:https://docs.fastlane.tools/
?本篇文章將分為兩大部分進行全面的講解關于打包的內容:
- 使用xcode提供的打包工具進行手動打包;
- 使用fastlane工具進行自動打包;
?下面我們先來看下xcode工具的打包流程:
寫在前面:本文中您所看到的效果都是基于xcode9.1進行的,當然,開發環境證書配置都是自動配置的,這里我不會再多贅述。
1.設置如圖所示:
- ad-hoc:打包發布蒲公英測試平臺;
- app-store:打包發布App Store;
- 其他的簽名方式在xcode9.0后是自動選擇;選擇導出ipa包方式.png
?到這里,打包的流程就完成了,下面我們就把導出的ipa通過xcode提供的上傳工具進行發布app至App Store:上傳ipa.png
?至此,我們的ipa就會在iTunes content 中構建出新的版本,選擇該版本上線即可;my friend ~這種操作,你能忍?!我可是忍不了!
如果對于維護多個app或者多個target的工程,發布測試平臺或者App Store來講,這樣的操作是令人崩潰的,那么,我們收拾下令人窒息的心情,請繼續往下看~
?大家先來腦補一下這張圖
fastlane打包流程.png
?接下來,向大家詳細的講解fastlane自動打包,這里分以下 3 部分:
1.單個工程使用fastlane
自動打包
2.針對多個target環境使用fastlane自動打包
3.建議安裝下蒲公英的mac客戶端
首先來看單個工程使用fastlane自動打包的流程及配置:
- 使用
fastlane
環境打包,我們得先配置一下環境,fastlane
是基于ruby的,所以我們需要安裝下ruby環境,安裝ruby環境參考此鏈接;
*然后檢查 Xcode 命令行工具是否安裝。在終端窗口中輸入命令:
xcode-select --install
如果未安裝,終端會開始安裝,如果報錯誤:command line tools are already installed, use "Software Update" to install updates.
代表已經安裝。
- 以上依賴配置好之后就可以通過 rubygem 進行安裝了:
sudo gem install fastlane
安心等待一會,fastlane就安裝完成了。
- 接下來就是要初始化
fastlane
:
打開終端,cd到你的工程目錄,然后執行fastlane init:
Peter-2:~ Peter$ cd desktop
Peter-2:desktop Peter$ cd Popo/
Peter-2:Popo Peter$ fastlane init
[?] ?? /Users/peter/.rvm/rubies/ruby-2.4.1/lib/ruby/gems/2.4.0/gems/tty-screen-0.6.4/lib/tty/version.rb:3: warning: already initialized constant TTY::Screen::VERSION
/Users/peter/.rvm/gems/ruby-2.4.1@global/gems/tty-screen-0.6.4/lib/tty/version.rb:3: warning: previous definition of VERSION was here
[?] ??
[?] Looking for iOS and Android projects in current directory...
[17:08:01]: Created new folder './fastlane'.
[17:08:01]: Detected an iOS/macOS project in the current directory: 'Popo.xcodeproj'
[17:08:01]: -----------------------------
[17:08:01]: --- Welcome to fastlane ?? ---
[17:08:01]: -----------------------------
[17:08:01]: fastlane can help you with all kinds of automation for your mobile app
[17:08:01]: We recommend automating one task first, and then gradually automating more over time
[17:08:01]: 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
?
寫在當前:
- 1、代表app市場圖片素材;
- 2、發布到TestFlight進行測試;
- 3、發布到App Store;
- 4、自定義
在初始化```fastlane```的過程中,可能會出現要輸入蘋果開發者賬號,
在 "Your Apple ID" 這一步輸入蘋果開發者賬號。
在“Please confirm the above values”這一步,確認信息,沒問題輸入 y。
然后,```fastlane ```會進行一系列的初始化操作,包括下載 App Store 上的元數據和截屏文件。
在這里,我們選擇序號 4 ,來手動去設置自定義的打包內容,在這一步,你會發現新的世界~
? 4
[17:17:37]: ------------------------------------------------------------
[17:17:37]: --- Setting up fastlane so you can manually configure it ---
[17:17:37]: ------------------------------------------------------------
[17:17:37]: Installing dependencies for you...
[17:17:37]: $ bundle update
[17:17:48]: --------------------------------------------------------
[17:17:48]: --- ? Successfully generated fastlane configuration ---
[17:17:48]: --------------------------------------------------------
[17:17:48]: Generated Fastfile at path `./fastlane/Fastfile`
[17:17:48]: Generated Appfile at path `./fastlane/Appfile`
[17:17:48]: Gemfile and Gemfile.lock at path `Gemfile`
[17:17:48]: Please check the newly generated configuration files into git along with your project
[17:17:48]: This way everyone in your team can benefit from your fastlane setup
[17:17:48]: Continue by pressing Enter ?
[17:17:51]: fastlane will collect the number of errors for each action to detect integration issues
[17:17:51]: No sensitive/private information will be uploaded, more information: https://docs.fastlane.tools/#metrics
[17:17:51]: ----------------------
[17:17:51]: --- fastlane lanes ---
[17:17:51]: ----------------------
[17:17:51]: fastlane uses a `Fastfile` to store the automation configuration
[17:17:51]: Within that, you'll see different lanes.
[17:17:51]: Each is there to automate a different task, like screenshots, code signing, or pushing new releases
[17:17:51]: Continue by pressing Enter ?
[17:17:51]: --------------------------------------
[17:17:51]: --- How to customize your Fastfile ---
[17:17:51]: --------------------------------------
[17:17:51]: Use a text editor of your choice to open the newly created Fastfile and take a look
[17:17:51]: You can now edit the available lanes and actions to customize the setup to fit your needs
[17:17:51]: To get a list of all the available actions, open https://docs.fastlane.tools/actions
[17:17:51]: Continue by pressing Enter ?
[17:17:52]: ------------------------------
[17:17:52]: --- Where to go from here? ---
[17:17:52]: ------------------------------
[17:17:52]: ?? Learn more about how to automatically generate localized App Store screenshots:
[17:17:52]: https://docs.fastlane.tools/getting-started/ios/screenshots/
[17:17:52]: ????? Learn more about distribution to beta testing services:
[17:17:52]: https://docs.fastlane.tools/getting-started/ios/beta-deployment/
[17:17:52]: ?? Learn more about how to automate the App Store release process:
[17:17:52]: https://docs.fastlane.tools/getting-started/ios/appstore-deployment/
[17:17:52]: ????? Learn more about how to setup code signing with fastlane
[17:17:52]: https://docs.fastlane.tools/codesigning/getting-started/
[17:17:52]:
[17:17:52]: To try your new fastlane setup, just enter and run
[17:17:52]: $ fastlane custom_lane
#######################################################################
# fastlane 2.88.0 is available. You are on 2.87.0.
# You should use the latest version.
# Please update using `sudo gem install fastlane`.
#######################################################################
2.88.0 Improvements
* [action] Fix crashlytics to not autoload gsp_path if api_token is set (#12176) via Josh Holtz
* Improve error message when specified scheme is not found (#12182) via Cédric Luthi
* [action] Support checking remote git tags existence (#11675) via Takeru Chuganji
* Use Android environment to find adb (don't just rely on it being in PATH) (#12168) via Adam Cohen-Rose
* [snapshot] Make sure matched window has a non-empty frame (#12174) via Fran?ois Pradel
* [swift] Fixes string return value and shows all lanes (even without description) (#12171) via Josh Holtz
* [scan] Default skip_build to true in Scanfile template (#12162) via Aaron Brager
Please update using `sudo gem install fastlane`
等待初始化完成之后,工程目錄下就多了一個 fastlane
目錄,其內容如下:
初始化后,我們還需要添加一個蒲公英測試平臺的插件以供提交測試,在終端輸入
fastlane add_plugin pgyer
即可;然后到工程目錄下可以看到有個
fastlane
文件創建成功了,子目錄里面有三個文件,一個Appfile
,一個Fastfile
文件,另一個Pluginfile
,我們通過名稱可知他們的大概用途:Appfile用來編輯設置app_identifier,apple_id和team_id
的信息;Pluginfile是存放蒲公英測試平臺的默認配置;Fastfile管理你所創建的 lane ,了解詳情。它的格式是這樣的:
#---------- begin -----------
default_platform :ios
platform :ios do
desc "發布 蒲公英"
lane :beta_pgy do
gym(scheme: "Test",
export_method: "ad-hoc",
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
pgyer(api_key: "b*******************************4",
user_key: "e********************************7",
update_description: get_update_description(),
# password: "123456",
# install_type: "2"
)
end
desc "發布 到 蘋果TestFlight"
lane :beta_apple do
gym(scheme: "Test"],
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
pilot #管理TestFlight測試用戶,上傳二進制文件
end
desc "發布蘋果商店"
lane :release_apple do
gym(scheme: "Test"],
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
deliver #上傳截圖、元數據、App到iTunesConnect
end
#---------- end -------------
# You can define as many lanes as you want
after_all do |lane|
# This block is called, only if the executed lane was successful
# slack(
# message: "Successfully deployed new App Update."
# )
end
error do |lane, exception|
# slack(
# message: exception.message,
# success: false
# )
end
end
其中一個lane就是一個任務,里面是一個個的action組成的工作流。
利用目前支持的工具可以做所有包含自動化和可持續化構建的每個環節,例如:
- scan 自動化測試工具,很好的封裝了 Unit Test
- sigh 針對于 iOS 項目開發證書和 Provision file 的下載工具
- match 同步團隊每個人的證書和 Provision file 的超贊工具
- gym 針對于 iOS 編譯打包生成 ipa 文件
- deliver 用于上傳應用的二進制代碼,應用截屏和元數據到 App Store
- snapshot 可以自動化iOS應用在每個設備上的本地化截屏過程
執行lane
定義完lane之后怎么執行呢?打開終端,切換到項目的根目錄:執行fastlane lane'name
就可以了。成功之后會在相應的路徑下生成ipa文件,如果報錯的話就根據錯誤信息好好查看文檔。
如果安裝了mac版的蒲公英客戶端的朋友,在自動打包完成之后,客戶端會自動攔截ipa的動作(前提是保證你的賬戶和fastfile里面對于蒲公英的配置api_key
和user_key
的配置保持一致即可)然后將ipa上傳即可;
?怎么樣,my friend,這一波操作下來是不是省去的時間可以去喝杯咖啡了。來來來,接下來就是多個target環境使用fastlane自動打包。這里分 3 部分來講解配置:
- 配置.env 文件(工程里面有幾個traget,需要創建幾個.env)
- 配置 Deliverfile文件
- 如何使用其進行打包
比如工程里面有兩個target(一個Test1,一個Test2),那么通過終端命令cd到上次初始化的fastlane目錄,然后在終端輸入touch .env.Test1
,touch .env.Test2
,繼續在當前目錄下創建Deliverfiale,在終端輸入touch Deliverfile
,創建完成后,你會發現在fastlane
目錄下找不到剛剛創建的兩個.env
文件,可參考此文章,接下來對.env.Test1
和.env.Test2
兩個文件進行編輯;
.env.Test1
內容如下:
#APP唯一標識符
APP_IDENTIFIER = "com.Peter.Test1"
#發布版本號
APP_VERSION_RELEASE = "2.4.0"
#新版本修改記錄
RELEASE_NOTES = "1) 升級測試第一行\n2) 升級測試第二行"
#蒲公英 更新描述
PGY_UPDATE_DESCRIPTION = "fastlane自動打包上傳測試"
#自動提交審核
SUBMIT_FOR_REVIEW = false
#審核通過后立刻發布
AUTOMATIC_RELEASE = false
#蘋果開發者賬號
APPLE_ID = "XXXXXXXXXXXXXX"
#蘋果開發者帳號密碼
FASTLANE_PASSWORD = "*************"
#套裝ID
TEAM_ID = "XXXXXXXXXXXXXX"
#應用名稱
SCHEME_NAME = "Test1"
#APP元數據及截圖存放路徑
METADATA_PATH = "./metadata/Test1Copy"
SCREENSHOTS_PATH = "./screenshots/Test1Copy"
#APP元數據及截圖下載時,直接覆蓋原有數據,不詢問
DELIVER_FORCE_OVERWRITE = true
.env.Test2
內容如下:
#APP唯一標識符
APP_IDENTIFIER = "com.Peter.Test2"
#發布版本號
APP_VERSION_RELEASE = "2.4.1"
#新版本修改記錄
RELEASE_NOTES = "1) 升級測試第一行\n2) 升級測試第二行"
#蒲公英 更新描述
PGY_UPDATE_DESCRIPTION = "fastlane自動打包上傳測試"
#自動提交審核
SUBMIT_FOR_REVIEW = false
#審核通過后立刻發布
AUTOMATIC_RELEASE = false
#蘋果開發者賬號
APPLE_ID = "XXXXXXXXXXXXXXXXXXXXX"
#蘋果開發者帳號密碼
FASTLANE_PASSWORD = "****************"
#套裝ID
TEAM_ID = "XXXXXXXXXXXXXXXXXXXXX"
#應用名稱
SCHEME_NAME = "Test2"
#APP元數據及截圖存放路徑
METADATA_PATH = "./metadata/Test2Copy"
SCREENSHOTS_PATH = "./screenshots/Test2Copy"
#APP元數據及截圖下載時,直接覆蓋原有數據,不詢問
DELIVER_FORCE_OVERWRITE = true
Deliverfile
內容如下:
app_identifier ENV['APP_IDENTIFIER'] # The bundle identifier of your app
username ENV['APPLE_ID'] # your Apple ID user
# 元數據的路徑
metadata_path ENV['METADATA_PATH']
screenshots_path ENV['SCREENSHOTS_PATH']
#App store 中待發布的 App 版本,若每個 target 的版本號不同,可以通過.env 文件來分別定義
app_version ENV['APP_VERSION_RELEASE']
#下載 metadata 及 screenshots 時直接覆蓋,不詢問
force true
#不覆蓋 iTunes Connect原有截圖
skip_screenshots true
#自動提交審核
submit_for_review ENV['SUBMIT_FOR_REVIEW']
#審核通過后立刻發布
automatic_release ENV['AUTOMATIC_RELEASE']
#新版本修改記錄
release_notes({
"zh-Hans" => ENV['RELEASE_NOTES']
})
Appfile內容如下:
# app_identifier("[[APP_IDENTIFIER]]") # The bundle identifier of your app
# apple_id("[[APPLE_ID]]") # Your Apple email address
# For more information about the Appfile, see:
# https://docs.fastlane.tools/advanced/#appfile
app_identifier ENV['APP_IDENTIFIER'] # The bundle identifier of your app
apple_id ENV['APPLE_ID'] # Your Apple email address
team_id ENV['TEAM_ID'] # Developer Portal Team ID
Fastfile
內容如下:
#---------- begin -----------
default_platform :ios
platform :ios do
desc "1).Test1版本 發布到 蒲公英"
lane :beta_test1_pgy do
sh "fastlane beta_pgy --env Test1"
end
desc "2).Test2版本 發布到 蒲公英"
lane :beta_test2_pgy do
sh "fastlane beta_pgy --env Test2"
end
desc "3).Test1版本 發布到 蘋果商店"
lane :release_test1_apple do
sh "fastlane release_apple --env Test1"
end
desc "4).Test2版本 發布到 蘋果商店"
lane :release_test2_apple do
sh "fastlane release_apple --env Test2"
end
desc "5).Test1版本 和 Test2版本 同時 發布到 蒲公英"
lane :beta_all_pgy do
sh "fastlane beta_pgy --env Test1"
sh "fastlane beta_pgy --env Test2"
end
desc "6).Test1版本 和 Test2版本 同時 發布到 蘋果TestFlight"
lane :beta_all_apple do
sh "fastlane beta_apple --env Test1"
sh "fastlane beta_apple --env Test2"
end
desc "7).Test1版本 和 Test2版本 同時 發布到 蘋果商店"
lane :release_all_apple do
sh "fastlane release_apple --env Test1"
sh "fastlane release_apple --env Test2"
end
desc "發布 指定版本 到 蒲公英"
lane :beta_pgy do
gym(scheme: ENV['SCHEME_NAME'],
export_method: "ad-hoc",
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
pgyer(api_key: "b******************************4",
user_key: "e*******************************7",
update_description: get_update_description(),
# password: "123456",
# install_type: "2"
)
end
desc "發布 指定版本 到 蘋果TestFlight"
lane :beta_apple do
gym(scheme: ENV['SCHEME_NAME'],
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
pilot
end
desc "發布 指定版本 到 蘋果商店"
lane :release_apple do
gym(scheme: ENV['SCHEME_NAME'],
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
deliver
end
#---------- end -------------
# You can define as many lanes as you want
after_all do |lane|
# This block is called, only if the executed lane was successful
# slack(
# message: "Successfully deployed new App Update."
# )
end
error do |lane, exception|
# slack(
# message: exception.message,
# success: false
# )
end
end
到這里,所有的配置都已經配置好了,另外還有一個需要設置的地方,在xcode開發工具中請將scheme的shared勾選上,如圖:
緊接著,在終端用命令進入項目根目錄,輸入
fastlane ios
命令,效果如下:
fastlane ios
[?] ??
[22:28:04]: fastlane detected a Gemfile in the current directory
[22:28:04]: however it seems like you don't use `bundle exec`
[22:28:04]: to launch fastlane faster, please use
[22:28:04]:
[22:28:04]: $ bundle exec fastlane ios
[22:28:04]:
[22:28:04]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[22:28:04]: In the config file './fastlane/Deliverfile' you have the line app_identifier, but didn't provide any value. Make sure to append a value right after the option name. Make sure to check the docs for more information
+-----------------------+---------+--------+
| Used plugins |
+-----------------------+---------+--------+
| Plugin | Version | Action |
+-----------------------+---------+--------+
| fastlane-plugin-pgyer | 0.2.1 | pgyer |
+-----------------------+---------+--------+
[22:28:04]: ------------------------------
[22:28:04]: --- Step: default_platform ---
[22:28:04]: ------------------------------
[22:28:04]: Welcome to fastlane! Here's what your app is setup to do:
+--------+-------------------------+----------------------------------------+
| Available lanes to run |
+--------+-------------------------+----------------------------------------+
| Number | Lane Name | Description |
+--------+-------------------------+----------------------------------------+
| 1 | ios beta_test1_pgy | 1).test1版本 發布到 蒲公英 |
| 2 | ios beta_test2_pgy | 2).test2版本 發布到 蒲公英 |
| 3 | ios release_test1_apple | 3).test1版本 發布到 蘋果商店 |
| 4 | ios release_test2_apple | 4).test2版本 發布到 蘋果商店 |
| 5 | ios beta_all_pgy | 5).test1版本 和 test2版本 同時 發布到 |
| | | 蒲公英 |
| 6 | ios beta_all_apple | 6).test1版本 和 test2版本 同時 發布到 |
| | | 蘋果TestFlight |
| 7 | ios release_all_apple | 7).test1版本 和 test2版本 同時 發布到 |
| | | 蘋果商店 |
| 8 | ios beta_pgy | 發布 指定版本 到 蒲公英 |
| 9 | ios beta_apple | 發布 指定版本 到 蘋果TestFlight |
| 10 | ios release_apple | 發布 指定版本 到 蘋果商店 |
| 0 | cancel | No selection, exit |
| | | fastlane! |
+--------+-------------------------+----------------------------------------+
[22:28:04]: Which number would you like run?
1
此時,你想打什么包就打什么包,是不是很隨心所欲呀?!
由于時間的問題,我就不一一進行測試了,剩下的就交給各位朋友們自行測試看看效果。。。。
?親們,如遇到問題可隨時交流,歡迎來撩!