自動化工具Fastlane: 安裝, 打包,上傳(testFlight,app store)


什么是Fastlane?

官方自己的定義是這樣的:

fastlane is a tool for iOS, Mac, and Android developers to automate tedious tasks like generating screenshots, dealing with provisioning profiles, and releasing your application.

從這里我們可以看出,fastlane針對的developers主要是 iOS, Mac, and Android。目的在于替代完成一切 沒有技術(shù)含量的、乏味的工作。簡單列舉了比如screenshot,provisioning profiles,deliver。

我們第一次接觸fastlane或者之前從來沒有使用過CLI工具的人,對于上邊的概念描述只能知道他的功能,但是不知道fastlane到底是哪個類型的工具,怎么實(shí)現(xiàn)上邊功能。 關(guān)于這一點(diǎn),我在閱讀 Wang Hailong 的文章得到了很簡練的答案,摘錄如下

fastlane命令是一個流程控制的命令行工具(CLI),通過內(nèi)部集成action和第三方的action完成一系列控制流程。運(yùn)行fastlane命令行工具,會讀取當(dāng)前目錄或者./fastlane目錄下的Fastfile配置文件。


在Fastfile中:

action => Fastlane中的每一條命令都是一個擴(kuò)展(action),上面提到的deliver,sigh之類的工具本身是CLI,但是在Fastlane中內(nèi)嵌了對他們支持的action

lane => Fastlane中流程的合集,每一個動作即可以是action,也可以是其他的lane。語法和ruby中的rake非常像

一個簡單的發(fā)布流程:

lane :deploy do

# 執(zhí)行 pod instasll

cocoapods

# 執(zhí)行 carthage bootstrap

carthage

# 增加build版本號

increment_build_number

# 編譯代碼

gym

# 發(fā)布到Apple Store

deliver(force: true)

end


安裝

搞了半小天和在同事的幫助下終于安裝上了,也把在安裝時候遇到的一些問題分享給大家,看完下面相信大家,很快就會搞定的。

首先:

fastlane安裝的方式有三種,分別是Homebrew、Installer Script,Rubygems。 這里需要注意的是,不管選擇哪種方式,你都要安裝Ruby。

我使用的是rubygems?

1.Ruby必須在2.0以上版本

2.macOS 10.9以上系統(tǒng)

3.Xcode 7.3.1 以上版本

4.擁有一個付費(fèi)的蘋果開發(fā)者賬號


其他:#

安裝Ruby/Rubygems,這里注意你在下來一個Ruby版本之后,之前的Ruby也是存在的,所以你需要通過rvm use XXXX --default指定你要使用哪一個Ruby版本,具體可以參考這篇文章《MAC機(jī)中安裝RUBY環(huán)境》

配置環(huán)境

更新ruby版本,安裝rvm

curl -L get.rvm.io | bash -s stable? ? # 安裝

rvm -v? ? ? ? # 測試是否安裝正常

rvm list known? ? ? ? # 列出已知ruby版本

rvm install ruby-xxxxx? ? #? 安裝一個最新ruby版本 注:此處xxxxx為list中的最新版本號

如果報錯的話

brew install openssl

reinstall|install ruby-xxxxx? ? #? ? 注意修改xxxxxx

以上所需的ruby環(huán)境基本配置好了

2.打開終端,選擇ruby 源

#查看gem源

gem sources

# 刪除默認(rèn)的gem源

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

# 增加rubygems作為gem源

gem sources --a https://rubygems.org/

# 查看當(dāng)前的gem源

gem sources

*** CURRENT SOURCES ***

https://rubygems.org/

# 清空源緩存

gem sources -c

# 更新源緩存

gem sources -u

安裝Fastlane

1.安裝xcode-select

xcode-select --install

# 如果 Xcode CLT 已經(jīng)安裝,則會報如下錯誤

# command line tools are already installed, use "Software Update" to install updates.

# 如果未安裝,終端會開始安裝 CLT

2.安裝fastlane

sudo gem install fastlane --verbose

#? 如果報錯:ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/commander

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

# 等待著安裝完畢....coffee or tea

# 安裝結(jié)束后,查看版本(2016.9.26我的版本是1.104.0)

fastlane --version

# 實(shí)際上目前安裝的fastlane并不是最新版本,還需要更新,怎么更新呢,看下面

# cd到項(xiàng)目文件夾

cd xxxxx

fastlane init

# 需要按照提示輸入 AppID以及密碼, 這個是你項(xiàng)目的開發(fā)者帳號,下邊要輸入項(xiàng)目的bundleIdentifier,然后出現(xiàn)了提示

########################################################################

# fastlane 1.104.0 is available. You are on 1.103.0.

# It is recommended to use the latest version.

# Update using 'sudo gem update fastlane'.

#######################################################################

# 更新最新版本

sudo gem install -n /usr/local/bin/ fastlane --version1.104.0

# 如果報錯 Could not find a valid gem 'fastlane' (= 1.66.0) in any repository , 那么更換一個ruby源,詳見步驟2

# 繼續(xù)更新最新版本

sudo gem install -n /usr/local/bin/ fastlane --version1.104.0

# 1.104.0成功安裝!

接著

確保Xcode Command Line Tools 安裝了最新版

`xcode-select --install`

如果你單獨(dú)安裝過ruby(如果你能看得懂這句),去掉sudo。如果使用系統(tǒng)自帶的ruby,需要sudo權(quán)限

sudo gem install fastlane`

提示安裝成功,恭喜你,你已經(jīng)成功了一半。

初始化

cd 到項(xiàng)目目錄中

fastlane init

# 注意:如果你看到一個 “permission denied” 錯誤,你可能要在命令前加上 sudo。

接著

請輸入? App Identifier (com.krausefx.app):

請輸入一個唯一的 app ID。請記住這個 ID,因?yàn)楹竺婺氵€會用到它! 你的 Apple ID (fastlane@krausefx.com):

請輸入你的 Apple ID 是否創(chuàng)建 deliver 命令,即上傳 app 屏幕截圖、app 元數(shù)據(jù)和 app 更新到 App 商店或者蘋果的 TestFlight?(y/n)

輸入 n 是否創(chuàng)建 snapshot 命令,它會自動獲取 app 在所有語言和設(shè)備類型上的屏幕截圖?(y/n)

輸入 y 是否使用 sigh 命令,它會維護(hù)和下載 app 的 provisioning profile?(y/n)

輸入 y 可選項(xiàng):app 的 scheme 名稱:(如果不需要,直接回車)

在項(xiàng)目根目錄下,初始化Fastlane:

fastlane init

提問了你的Apple ID,Team的問題之后,fastlane會自動檢測當(dāng)前目錄下項(xiàng)目的App Name和App Identifier。如果檢測的不對,選擇n自行輸入。

接下來會問你這個app是否需要在iTC和ADC中創(chuàng)建(上一步中如果選擇y會自動檢測是否需要創(chuàng)建),fastlane會調(diào)用produce進(jìn)行初始化,如果現(xiàn)在還不想創(chuàng)建,也可以之后再運(yùn)行produce init進(jìn)行這個流程。如果不執(zhí)行produce的流程,deliver的流程也會被掠過,當(dāng)然之后也可以deliver init運(yùn)行完全一樣的流程。

在執(zhí)行deliver init的過程中,會同步iTC中的所有語言的元數(shù)據(jù)和截圖,并按照目錄結(jié)構(gòu)組織好。目錄結(jié)構(gòu)應(yīng)該類似下面:

fastlane

├── Appfile

├── Deliverfile

├── Fastfile

├── metadata

│? ├── copyright.txt

│? ├── en-US

│? │? ├── description.txt

│? │? ├── keywords.txt

│? │? ├── marketing_url.txt

│? │? ├── name.txt

│? │? ├── privacy_url.txt

│? │? ├── release_notes.txt

│? │? └── support_url.txt

│? ├── primary_category.txt

│? ├── primary_first_sub_category.txt

│? ├── primary_second_sub_category.txt

│? ├── secondary_category.txt

│? ├── secondary_first_sub_category.txt

│? ├── secondary_second_sub_category.txt

│? └── zh-Hans

│? ? ? ├── description.txt

│? ? ? ├── keywords.txt

│? ? ? ├── marketing_url.txt

│? ? ? ├── name.txt

│? ? ? ├── privacy_url.txt

│? ? ? ├── release_notes.txt

│? ? ? └── support_url.txt

└── screenshots

├── README.txt

├── en-US

│? ├── 一堆png圖片

這里肯定會被創(chuàng)建的是Appfile和Fastfile。如果Deliverfile,screenshots和metadata目錄沒被創(chuàng)建,可以運(yùn)行deliver init來創(chuàng)建。

1. Fastfile => 用來定義所有的lane任務(wù)Fastfile幫助

2. Appfile => 是用來存儲一些公共信息的,比如app_identifier,apple_id,team_id,itc_team_id等。Appfile幫助

3. Deliverfile => deliver的配置文件Deliverfile幫助

PS:

1. 這里有個小問題,iTC和ADC中的Team ID是不一樣的,在fastlane init中只會自動在Appfile里寫入ADC的team_id,所以在這個過程中會不停的問你iTC的Team ID,所以在創(chuàng)建完Appfile后,手動在里面添加itc_team_id。

2. 這個問答對不同的項(xiàng)目可能有各種各樣的分支。我已經(jīng)用不同的項(xiàng)目試過很多次了,但是可能還不是全部,所以你還需要見招拆招。

3. 在這里可以安心的輸入密碼,所有的密碼都加密保存在系統(tǒng)的Keychain里。

接著在項(xiàng)目目錄中會生成fastlane文件夾

其中Fastfile,Appfile,Deliverfile這三個文件,功能如上介紹,如果不出問題的話,大家的這三個文件內(nèi)容應(yīng)該是和我一樣的如下(可以選擇sublime打開,然后把當(dāng)前顯示樣式設(shè)置為Ruby):

Fastfile:

fastlane_version "2.5.0"

default_platform :ios

platform :ios do

before_all do

# ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."

end

desc "Runs all the tests"

lane :test do

scan

end

desc "Submit a new Beta Build to 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: "BlusinessAreaPlat") # Build your app - more options available

pilot

# sh "your_script.sh"

# You can also use other beta testing services here (run `fastlane actions`)

end

desc "Deploy a new version to the App Store"

lane :release do

# match(type: "appstore")

# snapshot

gym(scheme: "BlusinessAreaPlat") # Build your app - more options available

deliver(force: true)

# frameit

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

在Fastfile這個文件中,就是我們打包,發(fā)布到fir,testFlight,appstore等等操作的具體配置文件。

Appfile:

app_identifier "com.xxxx.xxxx" # The bundle identifier of your app

apple_id "xxxxx@xxxx.com" # Your Apple email address

team_id "XXXXXXXXX"? # Developer Portal Team ID

Deliverfile:

app_identifier "com.xxxx.xxxx" # The bundle identifier of your app

username "xxxxx@xxxx.com" # your Apple ID user

關(guān)于Fastfile文件

對照我們上邊f(xié)astlane init命令創(chuàng)建出來的默認(rèn)是的Fastfile默認(rèn)文件,我們一次解析文件中的「元素」所代表的含義

fastlane_version => 指定fastlane最小版本

default_platform => 指定當(dāng)前平臺,可選ios,android,mac

desc => 一個lane(英文翻譯是:小巷,小路,我們這里可以理解為任務(wù))的描述,一般說明這個lane是做什么的

lane : 任務(wù)的名字 do => 指定任務(wù)的名字,我們在執(zhí)行這個任務(wù)的時候,調(diào)用命令的格式為 fastlane 任務(wù)的名字,比如fastlane release。 在Fastfile文件中會有很多的lane,我們通俗的把理解為小任務(wù),每個小任務(wù)都負(fù)責(zé)一個功能。 然后我們調(diào)用不同的小任務(wù),來實(shí)現(xiàn)打包、上傳到development、上傳到testFlight、上傳到app store等功能。 換句話來說,也就是,我們具體關(guān)于怎么打包、怎么上傳的配置是放到對應(yīng)的lane里邊的

(lane......)end => lane到end標(biāo)識之間的內(nèi)容,聲明了一個任務(wù)具體執(zhí)行哪些操作,不指定哪些操作


這個模塊是表示我們上傳到app store的。

打包

這里再執(zhí)行打包就很簡單了,默認(rèn)Fastfile中包含gym這一個功能模塊的lane,都可以執(zhí)行打包操作。 在gym中指定output_directory一項(xiàng)來指定輸出的.ipa文件的輸出目錄,如果不指定的話,只在項(xiàng)目根目錄下。

執(zhí)行l(wèi)ane命令:

fastlane lane的名字

比如 fast lane release 執(zhí)行打包上傳到appstore

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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