iOS-Fastlane自動(dòng)化打包上傳蒲公英+踩坑、

簡(jiǎn)介

Fastlane是一套使用Ruby寫(xiě)的自動(dòng)化工具集,用于iOS和Android的自動(dòng)化打包、發(fā)布等工作,可以節(jié)省大量的時(shí)間。

官網(wǎng):https://fastlane.tools/
文檔:https://docs.fastlane.tools/
Github:https://github.com/fastlane/fastlane

前言

項(xiàng)目上線之際總需要打包測(cè)試,每次使用Xcode - Archive等待好幾分鐘的Buliding,然后Export,通過(guò)Application Loader上傳到 AppStore,或者分發(fā)到蒲公英,F(xiàn)ir等發(fā)布平臺(tái)。一系列操作是耗時(shí)且沒(méi)有技術(shù)含量的工作,所以自動(dòng)化打包ipa,很有必要掌握。點(diǎn)擊前往Github下載Demo

工具

腳本工具有很多,這里使用的是 FastLane,Github里面24+K的Star和3.7K的Fork,說(shuō)明大量的開(kāi)發(fā)者信任并一起維護(hù)使用。

安裝

使用 FastLane需要以下內(nèi)容:

  • OS X10.9及以上版本
  • Ruby2.0及以上版本
  • Xcode命令行工具(CLT)
  • 付費(fèi)Apple開(kāi)發(fā)者帳戶(hù)

FastLaneRuby腳本的集合,首先需要正確版本的Ruby,從OS X10.9及以后默認(rèn)Ruby2.0。
我們可以通過(guò)命令確認(rèn),打開(kāi)終端輸入如下命令:

ruby -v

檢測(cè)是否安裝了 Xcode CLT 打開(kāi)終端輸入如下命令:

xcode-select --install

如果已經(jīng)安裝了Xcode CLT ,則會(huì)收到

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

如果未安裝,它將為您安裝Xcode CLT。
完成先決條件后,您就可以安裝FastLane了。 輸入以下命令:

sudo gem install -n /usr/local/bin fastlane --verbose

正常情況,輸入系統(tǒng)密碼之后,終端窗口會(huì)有一系列活動(dòng),表明安裝正在進(jìn)行中,需要等待幾分鐘。
備注:這里如果報(bào)錯(cuò),參考文章最下面 踩坑一

配置FastLane

1、fastlane init

新建一個(gè)項(xiàng)目工程,名字暫定 fastlaneTest,cd 到項(xiàng)目根目錄執(zhí)行

fastlane init

這里會(huì)彈出四個(gè)選項(xiàng),問(wèn)你想要用Fastlane做什么? 這里我選的是3

1. ??  Automate screenshots
2. ?????  Automate beta distribution to TestFlight (自動(dòng)testfilght型配置)
3. ??  Automate App Store distribution (自動(dòng)發(fā)布型配置)
4. ??  Manual setup - manually setup your project to automate your (需要手動(dòng)配置內(nèi)容)

如果你的工程是用cocoapods的那么可能會(huì)提示讓你勾選工程的Scheme;
步驟就是打開(kāi)你的xcode,
點(diǎn)擊Manage Schemes,
找到你的項(xiàng)目Scheme,在后面的多選框中進(jìn)行勾選,
然后可以手動(dòng)刪除 fastlane文件夾,重新fastlane init一下。

2、登錄Apple ID

[11:10:04]: ----------------------------------------------------------
[11:10:04]: --- Setting up fastlane for iOS App Store distribution ---
[11:10:04]: ----------------------------------------------------------
[11:10:04]: Parsing your local Xcode project to find the available schemes and the app identifier
[11:10:05]: $ xcodebuild -showBuildSettings -scheme fastlaneTest -project fastlaneTest.xcodeproj
[11:10:06]: $ cd /Users/hans3d/Desktop/fastlaneTest && agvtool what-version -terse
[11:10:07]: --------------------------------
[11:10:07]: --- Login with your Apple ID ---
[11:10:07]: --------------------------------
[11:10:07]: To use App Store Connect and Apple Developer Portal features as part of fastlane,
[11:10:07]: we will ask you for your Apple ID username and password
[11:10:07]: This is necessary for certain fastlane features, for example:
[11:10:07]: 
[11:10:07]: - Create and manage your provisioning profiles on the Developer Portal
[11:10:07]: - Upload and manage TestFlight and App Store builds on App Store Connect
[11:10:07]: - Manage your App Store Connect app metadata and screenshots
[11:10:07]: 
[11:10:07]: Your Apple ID credentials will only be stored in your Keychain, on your local machine
[11:10:07]: For more information, check out
[11:10:07]:     https://github.com/fastlane/fastlane/tree/master/credentials_manager
[11:10:07]: 
[11:10:07]: Please enter your Apple ID developer credentials
[11:10:07]: Apple ID Username:

輸入Apple Developer的賬號(hào)和密碼,因?yàn)?019.03蘋(píng)果開(kāi)啟二級(jí)認(rèn)證,需要手機(jī)登錄開(kāi)發(fā)者賬號(hào),進(jìn)行允許權(quán)限。
登陸成功如下:

[11:41:06]: ?  Logging in with your Apple ID was successful
[11:41:06]: Checking if the app 'com.cz.****.fastlaneTest' exists in your Apple Developer Portal...
[11:41:07]: It looks like the app 'com.cz.****.fastlaneTest' isn't available on the Apple Developer Portal
[11:41:07]: for the team ID '9A77SS**JG' on Apple ID '****@163.com '
[11:41:07]: Do you want fastlane to create the App ID for you on the Apple Developer Portal? (y/n)

選擇 y

+----------------+--------------------------+
|        Summary for produce 2.118.1        |
+----------------+--------------------------+
| username       | ****@163.com           |
| team_id        | 9A77SS**JG               |
| itc_team_id    | 118370***                |
| platform       | ios                      |
| app_identifier | com.cz.****.fastlaneTest |
| skip_itc       | true                     |
| sku            | 1552967***               |
| language       | English                  |
| skip_devcenter | false                    |
+----------------+--------------------------+

然后會(huì)提示輸入App Name:

[11:43:46]: App Name: fastlane_wnkp_demo

然后根據(jù)操作點(diǎn)擊回車(chē)即可!此時(shí)目錄結(jié)構(gòu)大概如下所示:

Appfile,用于存儲(chǔ)應(yīng)用程序標(biāo)識(shí)符和Apple ID。
Fastfile,用于管理您創(chuàng)建的用于調(diào)用某些操作的通道。最主要的文件,在這個(gè)文件中可以編寫(xiě)我們需要使用的各個(gè)工具的順序、方式等。

3、配置腳本

通過(guò)Xcode打開(kāi)Fastfile文件如下

default_platform(:ios)

platform :ios do
  desc "Push a new release build to the App Store"
  lane :release do
    build_app(scheme: "fastlaneTest")
    upload_to_app_store(skip_metadata: true, skip_screenshots: true)
  end
end

編寫(xiě)內(nèi)容代碼如下:

default_platform(:ios)

platform :ios do

    desc "fastlane 打包上傳到蒲公英發(fā)布網(wǎng)站"
    lane :fsv_pgyer do

    gym(
        clean:true,                      # Clean項(xiàng)目
        scheme:"fastlaneTest",           # 項(xiàng)目名稱(chēng)
        export_method:"ad-hoc",          # 打包的類(lèi)型
        configuration:"Release",         # 模式,默認(rèn)Release,還有Debug
        output_directory:"./build",      # 輸出的位置
    )
end
end

一個(gè)lane就是一個(gè)任務(wù),fsv_pgyer 名字可以自己改。
gym是fastlane提供的打包工具,括號(hào)里面就是你自己配置的一些東西。gym科普

4、執(zhí)行命令、本地打包

最后就是打開(kāi)終端,在你的工程目錄下,運(yùn)行fastlane fsv_pgyer就行了。

fastlane fsv_pgyer 

編譯成功:

[15:49:55]: Successfully exported and compressed dSYM file
[15:49:55]: Successfully exported and signed the ipa file:
[15:49:55]: /Users/hans3d/Desktop/fastlaneTest/build/fastlaneTest.ipa

+------+------------------+-------------+
|           fastlane summary            |
+------+------------------+-------------+
| Step | Action           | Time (in s) |
+------+------------------+-------------+
| 1    | default_platform | 0           |
| 2    | gym              | 12          |
+------+------------------+-------------+

[15:49:55]: fastlane.tools finished successfully ??

打開(kāi)項(xiàng)目目錄,會(huì)發(fā)現(xiàn)有一個(gè)bulid文件夾,打開(kāi)即是我們的ipa包


5、上傳到蒲公英或者fir配置

1、cd到項(xiàng)目下,安裝pgyer插件執(zhí)行命令 、

 fastlane add_plugin pgyer

執(zhí)行結(jié)果:

[15:55:52]: Plugin 'fastlane-plugin-pgyer' was added to './fastlane/Pluginfile'
[15:55:52]: It looks like fastlane plugins are not yet set up for this project.
[15:55:52]: fastlane will modify your existing Gemfile at path '/Users/hans3d/Desktop/fastlaneTest/Gemfile'
[15:55:52]: This change is necessary for fastlane plugins to work
[15:55:52]: Should fastlane modify the Gemfile at path '/Users/hans3d/Desktop/fastlaneTest/Gemfile' for you? (y/n)
y
[15:55:59]: Successfully modified '/Users/hans3d/Desktop/fastlaneTest/Gemfile'
[15:55:59]: Make sure to commit your Gemfile, Gemfile.lock and Pluginfile to version control
Installing plugin dependencies...
Successfully installed plugins

2、重新編寫(xiě)項(xiàng)目目錄下的Fastfile文件,如下:

default_platform(:ios)

platform :ios do

    desc "fastlane 打包上傳到蒲公英發(fā)布網(wǎng)站"
    lane :fsv_pgyer do

    gym(
        clean:true,                      # Clean項(xiàng)目
        scheme:"fastlaneTest",           # 項(xiàng)目名稱(chēng)
        export_method:"ad-hoc",          # 打包的類(lèi)型
        configuration:"Release",         # 模式,默認(rèn)Release,還有Debug
        output_directory:"./build",      # 輸出的位置
    )

    time      = Time.new.strftime("%Y%m%d")
    version   = get_version_number
    api_key   = "06e5563d5a8b94aac1c297426d351df6"
    user_key  = "974d1feb99ad49c955ed82a16ae2c21a"
    usipaName = "Release_#{version}_#{time}.ipa"

    pgyer(api_key: "#{api_key}", user_key: "#{user_key}")

end
end

pgyer中的api_keyuser_key為蒲公英的應(yīng)用->API中查看,如下圖:
蒲公英配置 蒲公英文檔

2、cd到項(xiàng)目根目錄下,執(zhí)行命令:

fastlane fsv_pgyer 

輸出結(jié)果:

[16:03:23]: Upload success. Visit this URL to see: https://www.pgyer.com/jnfC

+------+--------------------+-------------+
|            fastlane summary             |
+------+--------------------+-------------+
| Step | Action             | Time (in s) |
+------+--------------------+-------------+
| 1    | default_platform   | 0           |
| 2    | gym                | 12          |
| 3    | get_version_number | 0           |
| 4    | pgyer              | 1           |
+------+--------------------+-------------+
[16:03:23]: fastlane.tools finished successfully ??

打包上傳成功,收到短信:



踩坑一

執(zhí)行命令sudo gem install -n /usr/local/bin fastlane --verbose之后如下提示:

Could not find a valid gem 'fastlane' (>= 0), here is why:
          Unable to download data from https://ruby.taobao.org - SSL_connect returned=1 errno=0 state=error: certificate verify failed (https://ruby.taobao.org/specs.4.8.gz)

錯(cuò)誤原因:
查看gem

gem sources

輸出結(jié)果:

*** CURRENT SOURCES ***
https://ruby.taobao.org
所以需要更換源

刪除之前的源

gem sources --remove https://ruby.taobao.org

增加新的源

gem sources -a https://gems.ruby-china.com

查看源:

*** CURRENT SOURCES ***
https://gems.ruby-china.com
然后再執(zhí)行:
sudo gem install -n /usr/local/bin fastlane --verbose

即可正常下載、問(wèn)題解決?。?!

踩坑二

打包失敗還有需要開(kāi)發(fā)者賬號(hào)配置:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容