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

簡(jiǎn)介

Fastlane是一套使用Ruby寫的自動(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,通過Application Loader上傳到 AppStore裕偿,或者分發(fā)到蒲公英洞慎,F(xiàn)ir等發(fā)布平臺(tái)。一系列操作是耗時(shí)且沒有技術(shù)含量的工作嘿棘,所以自動(dòng)化打包ipa劲腿,很有必要掌握。點(diǎn)擊前往Github下載Demo

工具

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

安裝

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

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

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

ruby -v

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

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),問你想要用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慌洪;
步驟就是打開你的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蘋果開啟二級(jí)認(rèn)證憋肖,需要手機(jī)登錄開發(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)擊回車即可岸更!此時(shí)目錄結(jié)構(gòu)大概如下所示:

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

3、配置腳本

通過Xcode打開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

編寫內(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)目名稱
        export_method:"ad-hoc",          # 打包的類型
        configuration:"Release",         # 模式糟港,默認(rèn)Release,還有Debug
        output_directory:"./build",      # 輸出的位置
    )
end
end

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

4歹垫、執(zhí)行命令剥汤、本地打包

最后就是打開終端,在你的工程目錄下排惨,運(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 ??

打開項(xiàng)目目錄,會(huì)發(fā)現(xiàn)有一個(gè)bulid文件夾暮芭,打開即是我們的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à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)目名稱
        export_method:"ad-hoc",          # 打包的類型
        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

即可正常下載峭范、問題解決!1窦虎敦!

踩坑二

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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市政敢,隨后出現(xiàn)的幾起案子其徙,更是在濱河造成了極大的恐慌,老刑警劉巖喷户,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件唾那,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)闹获,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門期犬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人避诽,你說我怎么就攤上這事龟虎。” “怎么了沙庐?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵鲤妥,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我拱雏,道長(zhǎng)棉安,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任铸抑,我火速辦了婚禮贡耽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鹊汛。我一直安慰自己蒲赂,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布刁憋。 她就那樣靜靜地躺著凳宙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪职祷。 梳的紋絲不亂的頭發(fā)上氏涩,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音有梆,去河邊找鬼是尖。 笑死,一個(gè)胖子當(dāng)著我的面吹牛泥耀,可吹牛的內(nèi)容都是我干的饺汹。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼痰催,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼兜辞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起夸溶,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤逸吵,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后缝裁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扫皱,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了韩脑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片氢妈。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖段多,靈堂內(nèi)的尸體忽然破棺而出首量,到底是詐尸還是另有隱情,我是刑警寧澤进苍,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布加缘,位于F島的核電站,受9級(jí)特大地震影響琅捏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜递雀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一柄延、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缀程,春花似錦搜吧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至撩满,卻和暖如春蜒程,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背伺帘。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工昭躺, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人伪嫁。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓领炫,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親张咳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子帝洪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353