Fastlane 是一款為 iOS 和 Android 開發(fā)者提供的自動(dòng)化構(gòu)建工具,它可以幫助開發(fā)者將 App 打包、簽名、測(cè)試、發(fā)布、信息整理、提交 App Store 等工作完整的連接起來,實(shí)現(xiàn)完全自動(dòng)化的工作流,如果使用得當(dāng),可以顯著的提高開發(fā)者的開發(fā)效率。
安裝Fastlane
1.檢查有沒有ruby環(huán)境
ruby --version
2.安裝Xcode命令行工具
xcode-select --install
安裝完成后效果如下所示:


注意:如果未安裝,終端會(huì)開始安裝,如果報(bào)錯(cuò)誤:command line tools are already installed, use "Software Update" to install updates.代表已經(jīng)安裝。
3.以上依賴配置好之后就可以通過 rubygem 進(jìn)行安裝了:
sudo gem install fastlane
安心等待一會(huì),fastlane就安裝完成了,如圖所示

注意!此時(shí)可能會(huì)報(bào)錯(cuò):SSL verification error at depth 0: certificate has expired (10)

解決辦法:
(1).查看當(dāng)前鏡像環(huán)境 : gem sources -l
(2).移除淘寶鏡像: gem sources --remove https://rubygems.org/
(3).添加ruby-chian鏡像:gem sources --add https://gems.ruby-china.com/

Fastlane使用
1.初始化Fastlane
cd到你都工程目錄下執(zhí)行,然后執(zhí)行 fastlane init
allisondeMacBook-Pro:BDFCountyEdit allison$ fastlane init
[?] ?? /Users/allison/.rvm/gems/ruby-2.4.1@global/gems/fastlane-2.125.2/spaceship/lib/spaceship/test_flight/build.rb:91: warning: already initialized constant Spaceship::TestFlight::Build::BUILD_STATES
/Users/allison/.rvm/rubies/ruby-2.4.1/lib/ruby/gems/2.4.0/gems/fastlane-2.125.2/spaceship/lib/spaceship/test_flight/build.rb:91: warning: previous definition of BUILD_STATES was here
[?] ??
/Users/allison/.rvm/rubies/ruby-2.4.1/lib/ruby/gems/2.4.0/gems/fastlane-2.125.2/credentials_manager/lib/credentials_manager/account_manager.rb:8: warning: already initialized constant CredentialsManager::AccountManager::DEFAULT_PREFIX
/Users/allison/.rvm/gems/ruby-2.4.1@global/gems/fastlane-2.125.2/credentials_manager/lib/credentials_manager/account_manager.rb:8: warning: previous definition of DEFAULT_PREFIX was here
[11:09:37]: Sending anonymous analytics information
[11:09:37]: Learn more at https://docs.fastlane.tools/#metrics
[11:09:37]: No personal or sensitive data is sent.
[11:09:37]: You can disable this by adding `opt_out_usage` at the top of your Fastfile
[?] Looking for iOS and Android projects in current directory...
[11:09:38]: Created new folder './fastlane'.
[11:09:38]: Detected an iOS/macOS project in the current directory: 'CountyEdit.xcworkspace'
/Users/allison/.rvm/gems/ruby-2.4.1@global/gems/fastlane-2.125.2/spaceship/lib/spaceship/portal/certificate.rb:155: warning: already initialized constant Spaceship::Portal::Certificate::IOS_CERTIFICATE_TYPE_IDS
...... //省略無用信息
[11:09:38]: -----------------------------
[11:09:38]: --- Welcome to fastlane ?? ---
[11:09:38]: -----------------------------
[11:09:38]: fastlane can help you with all kinds of automation for your mobile app
[11:09:38]: We recommend automating one task first, and then gradually automating more over time
[11:09:38]: 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
?
Ambiguous choice. Please choose one of [1, 2, 3, 4, ?? Automate
screenshots, ????? Automate beta distribution to TestFlight, ??
Automate App Store distribution, ?? Manual setup - manually
setup your project to automate your tasks].
? 4
[11:10:33]: ---------------------------------------------------------
[11:10:33]: Setting up fastlane so you can manually configure it
[11:10:33]: ---------------------------------------------------------
[11:10:33]: Installing dependencies for you...
[11:10:33]: $ bundle update
[11:20:46]: --------------------------------------------------------
[11:20:46]: --- ? Successfully generated fastlane configuration ---
[11:20:46]: --------------------------------------------------------
[11:20:46]: Generated Fastfile at path `./fastlane/Fastfile`
[11:20:46]: Generated Appfile at path `./fastlane/Appfile`
[11:20:46]: Gemfile and Gemfile.lock at path `Gemfile`
[11:20:46]: Please check the newly generated configuration files into git along with your project
[11:20:46]: This way everyone in your team can benefit from your fastlane setup
[11:20:46]: Continue by pressing Enter ?
[11:27:51]: fastlane will collect the number of errors for each action to detect integration issues
[11:27:51]: No sensitive/private information will be uploaded, more information: https://docs.fastlane.tools/#metrics
[11:27:51]: ----------------------
[11:27:51]: --- fastlane lanes ---
[11:27:51]: ----------------------
[11:27:51]: fastlane uses a `Fastfile` to store the automation configuration
[11:27:51]: Within that, you'll see different lanes.
[11:27:51]: Each is there to automate a different task, like screenshots, code signing, or pushing new releases
[11:27:51]: Continue by pressing Enter ?
[11:27:52]: --------------------------------------
[11:27:52]: --- How to customize your Fastfile ---
[11:27:52]: --------------------------------------
[11:27:52]: Use a text editor of your choice to open the newly created Fastfile and take a look
[11:27:52]: You can now edit the available lanes and actions to customize the setup to fit your needs
[11:27:52]: To get a list of all the available actions, open https://docs.fastlane.tools/actions
[11:27:52]: Continue by pressing Enter ?
[11:27:57]: ------------------------------
[11:27:57]: --- Where to go from here? ---
[11:27:57]: ------------------------------
[11:27:57]: ?? Learn more about how to automatically generate localized App Store screenshots:
[11:27:57]: https://docs.fastlane.tools/getting-started/ios/screenshots/
[11:27:57]: ????? Learn more about distribution to beta testing services:
[11:27:57]: https://docs.fastlane.tools/getting-started/ios/beta-deployment/
[11:27:57]: ?? Learn more about how to automate the App Store release process:
[11:27:57]: https://docs.fastlane.tools/getting-started/ios/appstore-deployment/
[11:27:57]: ????? Learn more about how to setup code signing with fastlane
[11:27:57]: https://docs.fastlane.tools/codesigning/getting-started/
[11:27:57]:
[11:27:57]: To try your new fastlane setup, just enter and run
[11:27:57]: $ fastlane custom_lane
allisondeMacBook-Pro:BDFCountyEdit Allison$
注意:選擇配置方式,根據(jù)提示完成配置 ,這里我選擇的是第四個(gè)

2.等待初始化完成之后,工程目錄下就多了一個(gè) fastlane目錄,其內(nèi)容如下:

3.配置Appfile文件以及FastFile文件
Appfile:存儲(chǔ)App公共信息

關(guān)于Appfile的信息請(qǐng)閱讀 https://docs.fastlane.tools/advanced/#appfile
Fastfile:包含了要對(duì)你app操作的所有信息, 如:
在Fastfile中加入蒲公英插件的配置信息。例如:


注意:
1.以上的 api_key 和 user_key,請(qǐng)開發(fā)者在自己賬號(hào)下的 應(yīng)用管理 - App概述 - API 中可以找到,并替換到以上相應(yīng)的位置。
2.在 Xcode 8.3 和 Xcode 8.3 以后的版本中,對(duì)于 build_app 的 export_method 的值,需要根據(jù)開發(fā)者的打包類型進(jìn)行設(shè)置,可選的值有:app-store、ad-hoc、development、enterprise。對(duì)于 Xcode 8.3 以下的版本,則不需要設(shè)置 export_method。
詳情可參考: 使用 Fastlane 上傳 App 到蒲公英 文章.
打包并自動(dòng)上傳 App 到蒲公英
經(jīng)過以上配置后,就可以使用 Fastlane 來打包 App,并自動(dòng)上傳到蒲公英了。在終端下,定位到項(xiàng)目所在目錄,執(zhí)行fastlane beta命令即可。
allisondeMacBook-Pro:BDFCountyEdit allison$ fastlane beta
[?] ?? /Users/allison/.rvm/gems/ruby-2.4.1@global/gems/fastlane-2.125.2/spaceship/lib/spaceship/test_flight/build.rb:91: warning: already initialized constant Spaceship::TestFlight::Build::BUILD_STATES
/Users/allison/.rvm/rubies/ruby-2.4.1/lib/ruby/gems/2.4.0/gems/fastlane-2.125.2/spaceship/lib/spaceship/test_flight/build.rb:91: warning: previous definition of BUILD_STATES was here
[?] ??
......//省略無用的信息
[13:51:17]: Start upload /Users/allison/Desktop/BDFMobileDistrictCountyEditing/BDFCountyEdit/CountyEdit.ipa to pgyer...
[13:53:22]: Upload success. Visit this URL to see: https://www.pgyer.com/5gX0
+------+------------------+-------------+
| fastlane summary |
+------+------------------+-------------+
| Step | Action | Time (in s) |
+------+------------------+-------------+
| 1 | default_platform | 0 |
| 2 | build_app | 578 |
| 3 | pgyer | 125 |
+------+------------------+-------------+
[13:53:22]: fastlane.tools just saved you 12 minutes! ??
至此,fastlane打包完成,可到 https://www.pgyer.com/5gX0 網(wǎng)址下掃碼安裝最新的IPA包.
注意:如果出現(xiàn)下面的錯(cuò)誤,一定要好好檢查fastlane-->Fastfile文件,看是否遺漏一對(duì)括號(hào),或者build_app 和pgyer關(guān)鍵詞是否寫錯(cuò),最好直接copy ??
[!] Syntax error in your Fastfile on line 24: Fastfile:24: syntax error, unexpected keyword_end, expecting ')'
end
^

Fastlane安裝和簡單使用記錄至此,方便日后查閱。