iOS中使用Fastlane實現(xiàn)自動化打包和發(fā)布

轉(zhuǎn)載自http://www.cocoachina.com/ios/20170519/19317.html

簡介

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


這里寫圖片描述

Github:https://github.com/fastlane/fastlane

官網(wǎng):https://fastlane.tools/

文檔:https://docs.fastlane.tools/

安裝

1沼本、首先要安裝正確的 Ruby 版本。在終端窗口中用下列命令來確認:

ruby -v

2榨惰、然后檢查 Xcode 命令行工具是否安裝。在終端窗口中輸入命令:

xcode-select --install

如果未安裝静汤,終端會開始安裝琅催,如果報錯誤:command line tools are already installed, use "Software Update" to install updates.代表已經(jīng)安裝。

3虫给、以上依賴配置好之后就可以通過 rubygem 進行安裝了:

$ sudo gem install fastlane

安心等待一會藤抡,fastlane就安裝完成了。

初始化

打開終端抹估,cd到你的工程目錄缠黍,然后執(zhí)行fastlane init:

$ cd to/your/ios/project 
$ fastlane init
[14:21:43]: Detected iOS/Mac project in current directory...
[14:21:43]: This setup will help you get up and running in no time.
[14:21:43]: fastlane will check what tools you're already using and set up
[14:21:43]: the tool automatically for you. Have fun! 
[14:21:43]: Created new folder './fastlane'.
[14:21:43]: $ xcodebuild -showBuildSettings -project ./xxx.xcodeproj
[14:21:48]: Your Apple ID (e.g. fastlane@krausefx.com): xxx@xxx.xom
[14:21:54]: Verifying that app is available on the Apple Developer Portal and iTunes Connect...
[14:21:54]: Starting login with user 'xxx@xxx.com'
+----------------+--------------------------------------+
|                    Detected Values                    |
+----------------+--------------------------------------+
| Apple ID       | xxx@xxx.com                    |
| App Name       | xxx                            |
| App Identifier | com.xxx.xxx                |
| Project        | /Users/lisong/Desktop/xxx/x  |
|                | xx.xcodeproj                     |
+----------------+--------------------------------------+
[14:22:06]: Please confirm the above values (y/n)
y
[14:22:09]: Created new file './fastlane/Appfile'. Edit it to manage your preferred app metadata information.
[14:22:09]: Loading up 'deliver', this might take a few seconds
[14:22:09]: Login to iTunes Connect (xxx@xxx.com)
[14:22:13]: Login successful
+-----------------------+------------------------+
|             deliver 2.30.1 Summary             |
+-----------------------+------------------------+
| screenshots_path      | ./fastlane/screenshots |
| metadata_path         | ./fastlane/metadata    |
| username              | xxx@xxx.com      |
| app_identifier        | com.xxx.xxx  |
| edit_live             | false                  |
| platform              | ios                    |
| skip_binary_upload    | false                  |
| skip_screenshots      | false                  |
| skip_metadata         | false                  |
| force                 | false                  |
| submit_for_review     | false                  |
| automatic_release     | false                  |
| dev_portal_team_id    | WKR87TTKML             |
| overwrite_screenshots | false                  |
+-----------------------+------------------------+
[14:22:21]: Writing to 'fastlane/metadata/zh-Hans/description.txt'
...
[14:22:21]: Writing to 'fastlane/metadata/review_information/notes.txt'
[14:22:21]: Successfully created new configuration files.
[14:22:22]: Successfully downloaded large app icon
[14:22:22]: Downloading all existing screenshots...
[14:22:27]: Downloading existing screenshot '1_iphone4_1.1.jpg' for language 'zh-Hans'
···
[14:22:34]: Downloading existing screenshot '5_iphone6_5.5.jpg' for language 'zh-Hans'
[14:22:34]: Successfully downloaded all existing screenshots
[14:22:34]: Successfully created new Deliverfile at path 'fastlane/Deliverfile'
[14:22:34]: $ xcodebuild -list -project ./xxx.xcodeproj
[14:22:35]: 'snapshot' not enabled.
[14:22:35]: 'cocoapods' enabled.
[14:22:35]: 'carthage' not enabled.
[14:22:35]: Created new file './fastlane/Fastfile'. Edit it to manage your own deployment lanes.
[14:22:35]: fastlane will collect the number of errors for each action to detect integration issues
[14:22:35]: No sensitive/private information will be uploaded
[14:22:35]: Learn more at https://github.com/fastlane/fastlane#metrics
[14:22:35]: Successfully finished setting up fastlane

在 "Your Apple ID" 這一步輸入蘋果開發(fā)者賬號。在“Please confirm the above values”這一步药蜻,確認信息瓷式,沒問題輸入 y。然后语泽,fastlane 會進行一系列的初始化操作贸典,包括下載 App Store 上的元數(shù)據(jù)和截屏文件。

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


這里寫圖片描述

咱們來看兩個主要的据过,Appfile和Fastfile。

Appfile

Appfile用來存放app_identifier妒挎,apple_id和team_id绳锅。 了解詳情,它的格式是這樣的:

app_identifier "com.xxx.xxx" # app的bundle identifier
apple_id "xxx@xxx.com" # 你的Apple ID
 
team_id "XXXXXXXXXX" # Team ID
···

你也可以為每個lane(后面會講到)提供不同的 app_identifier, apple_id 和 team_id酝掩,例如:

app_identifier "com.aaa.aaa"
apple_id "aaa@aaa.com"
team_id "AAAAAAAAAA"
 
for_lane :inhouse do
  app_identifier "com.bbb.bbb"
  apple_id "bbb@bbb.com"
  team_id "AAAAAAAAAA"
end

這里就是為Fastfile中定義的:inhouse設置單獨的信息鳞芙。

Fastfile

Fastfile管理你所創(chuàng)建的 lane ,了解詳情期虾。它的格式是這樣的:

···
# 自動更新fastlane 工具
# update_fastlane
 
#需要的fastlane的最小版本积蜻,在每次執(zhí)行之后會檢查是否有新版本,如果有會在最后末尾追加新版本提醒
fastlane_version "2.30.1"
 
#默認使用平臺是 ios彻消,也就是說文件可以定義多個平臺
default_platform :ios
 
platform :ios do
  before_all do
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    cocoapods
 
  end
 
  desc "Runs all the tests"
  lane :test do
    scan
  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: "Docment") # Build your app - more options available
    pilot
 
    # sh "your_script.sh"
  end
 
  desc "部署一個新版本到App Store"
  lane :release do
    # match(type: "appstore")
    # snapshot
    gym(scheme: "Docment") # Build your app - more options available
    deliver(force: true)
    # frameit
  end
 
  # 你可以定義自己的lane
 
  #執(zhí)行l(wèi)ane成功后的回調(diào)
  after_all do |lane|
    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end
 
  # 如果流程發(fā)生異常會走這里并終止
  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end

我們也可以定義一個自己的lane:

  desc "企業(yè)版"
  lane :inHouse do
  gym(scheme: "XXX",
      export_method:"enterprise",
      output_directory "./build", # 打包后的 ipa 文件存放的目錄
      output_name "XXX"  # ipa 文件名
   )
  end

其中一個lane就是一個任務竿拆,里面是一個個的action組成的工作流。

利用目前支持的工具可以做所有包含自動化和可持續(xù)化構建的每個環(huán)節(jié)宾尚,例如:

  • scan 自動化測試工具丙笋,很好的封裝了 Unit Test
  • sigh 針對于 iOS 項目開發(fā)證書和 Provision file 的下載工具
  • match 同步團隊每個人的證書和 Provision file 的超贊工具
  • gym 針對于 iOS 編譯打包生成 ipa 文件
  • deliver 用于上傳應用的二進制代碼,應用截屏和元數(shù)據(jù)到 App Store
  • snapshot 可以自動化iOS應用在每個設備上的本地化截屏過程

執(zhí)行

定義完lane之后怎么執(zhí)行呢煌贴?打開終端御板,切換到項目的根目錄:執(zhí)行fastlane lane'name就可以了。成功之后會在相應的路徑下生成ipa文件牛郑,如果報錯的話就根據(jù)錯誤信息好好查看文檔怠肋。

其他

1、這里是官方提供的一些例子淹朋。

2笙各、想了解fastlane命令的話可以執(zhí)行$ fastlane --help

3、查看可用任務的列表础芍,可以執(zhí)行命令$ fastlane lanes

4杈抢、fastlane也提供了很多插件方便我們使用,例如pgyer(發(fā)布app到蒲公英)仑性。我們也可以打完包直接傳到蒲公英上惶楼,具體的可以看蒲公英提供的文檔

如果你感覺有些插件不符合自己的情況诊杆,你甚至可以自定義插件

5歼捐、多個 lane 的話實際上是可以相互調(diào)用的,這個其實特別實用晨汹。例如:

default_platform :ios
 
platform :ios do
 
  lane :prepare do
    cocoapods
    match
  end
 
 
  desc 'fastlane build'   'fastlane build type:adhoc'
  lane :build do |options|
    # 調(diào)用上面的 prepare 任務
    prepare
 
    case options[:type]
    when 'adhoc'
      adhoc
    else
      appstore
    end
  end
 
 
  lane : adhoc do
  ···
  end
 
  lane : appstore do
  ···
  end
 
end

我們可以在 Fastfile 文件中添加一個函數(shù)來設置version號和build號豹储。

default_platform :ios
 
def prepare_version(options)
    increment_version_number(
        version_number: options[:version]
    )
 
    increment_build_number(
        build_number: options[:build]
    )
end

然后可以在一個lane中使用這個函數(shù):

lane :appstore do |options|
   ···
    prepare_version(options)
   ···
end

然后執(zhí)行這個lane的時候:

$ fastlane appstore version:2.4.0 build:2.0

好啦,先說到這里吧宰缤,F(xiàn)astlane能做的事情還有很多颂翼,大家可以去好好看看文檔晃洒,研究一些高級的用法吧慨灭!

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末朦乏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子氧骤,更是在濱河造成了極大的恐慌呻疹,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件筹陵,死亡現(xiàn)場離奇詭異刽锤,居然都是意外死亡,警方通過查閱死者的電腦和手機朦佩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門并思,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人语稠,你說我怎么就攤上這事宋彼。” “怎么了仙畦?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵输涕,是天一觀的道長。 經(jīng)常有香客問我慨畸,道長莱坎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任寸士,我火速辦了婚禮檐什,結果婚禮上,老公的妹妹穿的比我還像新娘弱卡。我一直安慰自己厢汹,他們只是感情好,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布谐宙。 她就那樣靜靜地躺著烫葬,像睡著了一般。 火紅的嫁衣襯著肌膚如雪凡蜻。 梳的紋絲不亂的頭發(fā)上搭综,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音划栓,去河邊找鬼兑巾。 笑死,一個胖子當著我的面吹牛忠荞,可吹牛的內(nèi)容都是我干的蒋歌。 我是一名探鬼主播帅掘,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼堂油!你這毒婦竟也來了修档?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤府框,失蹤者是張志新(化名)和其女友劉穎吱窝,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迫靖,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡院峡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了系宜。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片照激。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖盹牧,靈堂內(nèi)的尸體忽然破棺而出俩垃,到底是詐尸還是另有隱情,我是刑警寧澤欢策,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布吆寨,位于F島的核電站,受9級特大地震影響踩寇,放射性物質(zhì)發(fā)生泄漏啄清。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一俺孙、第九天 我趴在偏房一處隱蔽的房頂上張望辣卒。 院中可真熱鬧,春花似錦睛榄、人聲如沸荣茫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽啡莉。三九已至,卻和暖如春旨剥,著一層夾襖步出監(jiān)牢的瞬間咧欣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工轨帜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留魄咕,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓蚌父,卻偏偏與公主長得像哮兰,于是被迫代替她去往敵國和親毛萌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容