前言
查閱網(wǎng)上有許多關于fastlane的文章,但是要不不全面,要不對新手不友好惫确,甚至有些講解有錯誤,自己在安裝過程中踩了很多坑,于是總結了下文關于fastlane的安裝與使用指南改化。
fastlane簡介:
Git地址:
Fastlane文檔地址:
Fastlane Document
- Fastlane是一整套的客戶端CICD工具集合掩蛤。Fastlane可以非常快速簡單的搭建一個自動化發(fā)布服務陈肛,并且支持Android揍鸟,iOS,MacOS句旱。
- Fastlane命令執(zhí)行的底層并不是自己實現(xiàn)的阳藻,而是調用其他的插件或者工具執(zhí)行的。比如說打包谈撒,F(xiàn)astlane中的gym工具只是xcodebuild工具的一個封裝腥泥,調用的其實還是xcodebuild中的打包命令。
- Fastlane本身沒有一套特殊語法啃匿,使用的Ruby語言蛔外。
- Fastlane的插件工具叫做action,每一個action都對應一個具體的功能
一溯乒、faslane 安裝準備
1 檢查Xcode項目是否為share
Xcode -> Manager Scheme -> 項目勾選??Shared
2 升級ruby版本保持2.5+(目前最新版本要求 ruby2.7.0+)
- 查看ruby版本
~ % ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]
- 若不滿足需要升級ruby
~ % brew install ruby
- 若已有ruby版本夹厌,升級后仍顯示舊版本,參考:
MacOS下裆悄,ruby安裝與正確版本及路徑設置
3 安裝Xcode command line tools
- 若已安裝矛纹,忽略
~ % xcode-select --install
4 安裝fastlane
1) 方法一:使用gem安裝fastlane
~ % sudo gem install fastlane -NV
Successfully installed fastlane-2.209.1
75 gems installed
2) 方法二:使用homebrew安裝fastlane
~ % brew install fastlane
查看fastlane當前版本
~ % fastlane -v
fastlane installation at path:
/Library/Ruby/Gems/2.6.0/gems/fastlane-2.209.1/bin/fastlane
-----------------------------
[?] ??
fastlane 2.209.1
3) 配置信息
- cmd+Shift+G 輸入~/.bash_profile 文件末尾添加配置如下:
#fastlane
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
5 安裝bundler
- 如果不安裝的話接下來的 bundle update 會失敗 報錯。
- sudo是獲取管理員權限光稼,接下來需要輸入開機密碼或南。
sudo gem install bundler:2.1.2
二、項目集成fastlane
1. 進入你的項目所在根目錄
~ % cd ../Desktop/你的項目名
2. 初始化fastlane
~ % fastlane init
- 需要按提示輸入信息艾君,中途會讓你輸入輸入開發(fā)者賬號和密碼采够,之后fastlane會自動檢測當前項目中App bundle name等,如果有問題可以選擇No腻贰,也或者待會去配置文件中修改(后面會講)吁恍。
- 其中,初始化時應該會有如下選項:
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
你想用fastlane做什么事情:
1 自動截屏播演。(幫助我們截取App的顯示到appstore上的 截圖)
2 自動發(fā)布beta到TestFlight上冀瓦,用于內測。
3 自動打包發(fā)布到AppStore上写烤。
4 手動設置翼闽。
- 我選擇3,之后按提示操作洲炊,如果開發(fā)者賬戶中沒有創(chuàng)建同名項目會自動創(chuàng)建好感局,且在本地項目文件夾下會自動生成fastlane文件夾和一個Gemfile文件
3. 配置Gemfile
1) 修改Gemfile文件內容
- 在2.中會自動生成Gemfile文件
- 內容如下
source "https://rubygems.org"
gem "fastlane" #初始化fastlane
gem "cocoapods" #如果項目中采用了cocoapods尼啡,需要自己加上
2) 更新Gemfile使生效
- 注意此時終端還是處于項目根目錄下
~ % bundle update
- 更新成功后,會生成Gemfile.lock文件
bundle update過慢解決方法:
- 方法一:終端設置代理
參考:MacOS代理設置
- 方法二:更改項目里Gemfile文件的source:
#source "https://rubygems.org"
source "https://ruby.taobao.org"
- 方法三:重啟大法=>不設置代理也不更改source询微,直接關閉終端崖瞭,重新執(zhí)行bundle update等待,多次嘗試直到成功撑毛。
4. 配置Fastfile
1) Fastfile文件主要結構如下:
fastlane_version "2.209.1" #指定fastlane使用的最小版本
default_platform(:ios) #支持 ios书聚、android、mac
platform :ios do
before_all do #在執(zhí)行每個lane之前都會先執(zhí)行
cocoapods
end
desc "自定義test的描述"
lane :test do #名叫test的lane指令集合
end
desc "自定義beta的描述"
lane :beta do #名叫beta的lane指令集合
end
desc "自定義release的描述"
lane :release do #名叫release的指令集合
end
after_all do |lane| #每個lane執(zhí)行完之后會執(zhí)行這里
end
error do |lane, exception| #每個lane執(zhí)行出錯會執(zhí)行
end
end
解釋:
- 每一個lane就是一個任務藻雌,
- lane :自定義任務名
- lane :自定義任務名 do 和 end之間雌续,是action組成的工作流
- 終端輸入 fastlane lane的自定義任務名,可執(zhí)行l(wèi)ane的do和end之間的任務
- 如下:(執(zhí)行名為test的lane任務胯杭,沒有顯示??且finished successfully ??說明成功)
~ % fastlane test
[?] ??
[15:00:01]: fastlane detected a Gemfile in the current directory
[15:00:01]: However, it seems like you didn't use `bundle exec`
[15:00:01]: To launch fastlane faster, please use
[15:00:01]:
[15:00:01]: $ bundle exec fastlane upload_toServer
[15:00:01]:
[15:00:01]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[15:00:03]: ----------------------------------------
[15:00:03]: --- Step: Verifying fastlane version ---
[15:00:03]: ----------------------------------------
[15:00:03]: Your fastlane version 2.209.1 matches the minimum requirement of 2.209.1 ?
[15:00:03]: ------------------------------
[15:00:03]: --- Step: default_platform ---
[15:00:03]: ------------------------------
[15:00:03]: Driving the lane 'mac upload_toServer' ??
+------+----------------------------+-------------+
| fastlane summary |
+------+----------------------------+-------------+
| Step | Action | Time (in s) |
+------+----------------------------+-------------+
| 1 | Verifying fastlane version | 0 |
| 2 | default_platform | 0 |
+------+----------------------------+-------------+
[15:00:03]: fastlane.tools finished successfully ??
2) 示例: 一個最簡單的Fastfile文件配置內容(ios項目打包.ipa)驯杜。
- 若為mac項目:替換ios為mac、替換.ipa為.app做个,替換為enterprise為mac-application鸽心。
- 若證書無企業(yè)版本,不能使用enterprise叁温,測試包可用ad-hoc
default_platform(:ios) #支持 ios再悼、android核畴、mac
platform :ios do
desc "項目打包"
lane :beta do
#build_app()主要是編譯打包成ipa文件這個過程的相關操作
#gym主要是編譯打包成功后膝但,對導出的IPA包的操作,gym中的相關參數(shù)可參考build_app中的參數(shù)谤草,因為兩者的參數(shù)基本一致
gym(
scheme:"項目名", # 如果有多個Target跟束,需要指定scheme名
workspace: "項目名.xcworkspace", # 指定項目工作空間,如果項目使用CocoaPods需要加上
clean: true, # 是否編譯前clean
configuration: "Debug", # Release或Debug
output_name: "自定義", # 重命名導出包的名字(可不指定,不指定默認項目名丑孩,注意若重命名會生成兩不同名的包冀宴,一個項目名一個自定義名)
output_directory:"./fastlane/build", # 自定義導出包的Copy目錄(若無自動創(chuàng)建,可不指定温学,不指定默認存到Xcode打包路徑:/Users/用戶名/Library/Developer/Xcode/Archives)
export_xcargs: "-allowProvisioningUpdates",# 啟用Xcode的自動管理簽名功能略贮,默認是關閉的
export_method:"enterprise", # 打包方式(可選項: app-store, validation, ad-hoc, package, enterprise, development, developer-id and mac-application)
)
end
end
3) 提交到第三方蒲公英服務器
- 配置Fastfile的蒲公英配置部分如下
pgyer(
api_key: "你蒲公英的api_key",
user_key: "你蒲公英的user_key"",
update_description: "fix something"
)
4) 提交到第三方firim服務器
- 配置Fastfile的firim配置部分如下
firim(
firim_api_token: '你在firim網(wǎng)站登錄后,點擊頭像仗岖,可以查看API token逃延。復制到這地方'
)
5) 使用Ftp方式提交到服務器
- 配置Fastfile的ftp配置部分如下
ftp(
host: 'ftp.domain.com',
username: 'your username',
password: 'your password',
upload: {
src: ipa_src,
dest:"/xxx/"
}
)
6) 使用scp方式提交到服務器(自測,目前使用的方法)
參考:scp配置
-
- 生成SSH Key并提交給服務器管理員,之后不需要再輸入賬號密碼轧拄。
- 使用腳本在終端執(zhí)行揽祥,測試服務器是否成功添加了本機SSHKey,腳本文件(xx.sh)內容如下:
#!/bin/bash
#上傳文件到服務器
IPAPATH="/Users/用戶名/Desktop/demo.ipa" #本機ipa存放路徑
scp -r $IPAPATH root@xxx.xx.xxx.xxx:/服務器存放地址路徑/demo.ipa #-r代表替換檩电,不加也是默認替換
- 終端輸入如下命令(即腳本路徑直接拖入終端)拄丰,直接回車執(zhí)行腳本即可府树。
- 若提示沒有權限 可以先賦權限 chmod +x 路徑,再拖入腳本回車執(zhí)行料按,如果用sh 路徑執(zhí)行可能有問題奄侠。
~ % chmod +x 腳本路徑/xx.sh
~ % 腳本路徑/xx.sh
-
- 配置Fastfile的scp配置部分如下:
- 注意:若為mac項目需要把.ipa改為.dmg
- dmg打包方法可參考文章下面的:7) mac項目打包成dmg
desc "2.上傳包文件到服務器(使用scp方式)"
lane :upload_toServer do
scp(
host: "xxx.xx.xxx.xxx", # 服務器ip地址,如180.10.200.200
username: "root", # 服務器用戶名
upload: {
src: "#{$outputDir}/#{$project_abbreviation}.ipa, #原始包文件地址载矿,必須真實存在
dst: "/服務器提供的文件目錄/#{$project_abbreviation}.ipa" #服務器上傳的包文件地址遭铺,同名的話默認每次上傳會替換該文件
}
)
end
- 3)項目路徑下添加SSH
~ % cd 項目路徑
~ % ssh-add
- 注意:如果不添加的話 fastlane upload_toServer 會報錯
[18:50:51]: fastlane finished with errors
/Library/Ruby/Gems/2.6.0/gems/net-ssh-6.1.0/lib/net/ssh/authentication/ed25519_loader.rb:21:in
`raiseUnlessLoaded': OpenSSH keys only supported if ED25519 is available (NotImplementedError)
net-ssh requires the following gems for ed25519 support:
* ed25519 (>= 1.2, < 2.0)
* bcrypt_pbkdf (>= 1.0, < 2.0)
*
- 終端執(zhí)行代碼,使用faslane通過scp方式上傳工程包到服務器
~ % fastlane upload_toServer
[18:52:57]: fastlane.tools finished successfully ??
- 5)上傳成功后恢准,可與服務器溝通獲得項目的下載地址魂挂。查看是否能正確下載項目
- 拿到地址后可以提供其他人進行下載測試。
7) mac項目打包成dmg
注意:mac的.app包文件必須打包成dmg再通過scp上傳到服務器馁筐,否則服務器接收到的是許多文件的列表涂召,而不是.app項目
1.安裝dmg插件:官方文檔具體可看
~ % fastlane add_plugin dmg
Successfully installed plugins
安裝完成后會更新項目中的Gemfile文件和./fastlane/Pluginfile文件
2.fastlane文件里打包dmg官方示例寫法:
dmg(path: "/some/myapp/", # required
output_path: "/some/myapp.dmg", # optional, by default will be at the same location as 'myapp' folder
volume_name: "myapp", # optional, by default will be the same as input folder name
filesystem: "hfs+", # optional, default is 'hfs+'
format: "udzo", # optional, default is 'udzo'
size: 10) # in megabytes, optional, by default will be calculated as input folder size + 10%
- 2.注意:不需要寫size,否則會報錯且打包dmg失敗敏沉。
- 自測能成功打包dmg的寫法:(注意此時已有打包好的.app放到:項目路徑/fastlane/build/demoMac/demo.app)
dmg(
path: "./fastlane/build/demoMac", # 或./fastlane/build/demoMac/
output_path: "./fastlane/build/demoMac.dmg", # 可選果正,不寫默認為 path.dmg
#volume_name: "demoMac", # 可選,不寫默認為demoMac
#filesystem: "hfs+", # 可選, 默認 'hfs+'
#format: "udzo", # 可選盟迟,默認 'udzo'
)
* 補充知識:hdiutil工具(fastlane的dmg封裝的是hdiutil工具命令秋泳,hdiutil為mac系統(tǒng)自帶)
- fastlane的dmg封裝的是hdiutil工具,hdiutil工具參考文章見hdiutil工具
終端直接使用hdiutil打包dmg示例:
- 先把需要封裝的.app文件放到文件夾里攒菠,如 /Desktop/MacDemoDMG/MacDemo/demo.app
~ % cd Desktop/MacDemoDMG
~ % hdiutil create -srcdir "MacDemo"/ -format UDRO "MacDemo.dmg"
......................................................................................................................
created: /Users/用戶名/Desktop/MacDemoDMG/MacDemo.dmg
- 成功會在/Users/用戶名/Desktop/MacDemoDMG/MacDemo.dmg看到打包好的dmg文件
六迫皱、Fastfile內容完整示例
1.Fastfile打包ios項目(.ipa),內容示例:
# 工程名
$project_name = "Demo-Test"
# 工程名縮寫
$project_abbreviation = "Demo"
# 項目scheme名
$scheme_name = "Demo-Appstore"
default_platform(:ios)
platform :ios do
desc "1.內測版(enterprise)Debug打包并上傳服務器(方法四scp方式)"
lane :archive do
#打包的ipa存放路徑
outputDir = "./fastlane/build"
#打包的ipa名稱
outputName = "#{$project_abbreviation}"
# 1.1 打Debug包
gym(
scheme: "#{$scheme_name}", # 如果有多個Target辖众,需要指定scheme名
workspace: "#{$project_name}.xcworkspace", # 如果項目使用CocoaPods需要加上
clean: true, # 是否編譯前clean
configuration: "Debug", # Debug或Release
output_directory: outputDir, # 自定義導出包的Copy目錄
output_name: outputName, # 重命名導出包的名字卓起,若指定會生成兩個包
include_bitcode: true, # 是否開啟bitcode,Xcode默認為true
include_symbols: true, # 是否生成符號表凹炸,Xcode默認為true
silent: true, # 是否隱藏部分編譯細節(jié)
export_xcargs: "-allowProvisioningUpdates", # 啟用Xcode自動簽名
export_method:"enterprise" # 打包方式(app-store, validation, ad-hoc, package, enterprise, development, developer-id and mac-application)
)
# 1.2 上傳1.1的已打包好的包文件(親測上傳方式四可行)
#上傳方式一:上傳ipa到firim服務器戏阅,需要先安裝firm插件
#firim(
#firim_api_token: '你在firim網(wǎng)站登錄后,點擊頭像啤它,可以查看API token奕筐。復制到這地方'
#)
#上傳方式二:上傳ipa到蒲公英服務器,需要先安裝pgyer插件
#pgyer(
#api_key: "你蒲公英的api_key",
#user_key: "你蒲公英的user_key"",
#update_description: "fix something"
#)
#上傳方式三:上傳到Ftp服務器
#ftp(
#host: 'ftp.domain.com',
#username: 'your username',
#password: 'your password',
#upload: {
#src: ipa_src,
#dest:"/xxx/"
#}
#)
#上傳方式四:上傳ipa到公司的服務器变骡,使用SSH key,需要讓服務器添加公玥
scp(
host: "xxx.xx.xxx.xxx", # 填寫你公司的服務器ip地址
username: "root", # 填寫你公司的服務器的用戶名
upload: {
src: "./fastlane/build/Demo.ipa", # 1.1中打好的ipa包本機所在地址
dst: "/填寫服務器提供的目錄/Demo.ipa" # 服務器提供的ipa存放路徑
}
)
#上傳方式五:上傳到testflight
#upload_to_testflight
#上傳方式六:上傳到appstore
#upload_to_app_store
end
end
2.Fastfile打包mac項目(.dmg)离赫,內容示例:
# 工程名
$project_name = "Demo-Test"
# 工程名縮寫
$project_abbreviation = "Demo"
# 項目scheme名
$scheme_name = "Demo-Appstore"
default_platform(:mac)
platform : mac do
desc "1.Release打包并通過scp方式上傳到服務器"
lane :archive do
#打包的.app存放路徑
outputDir = "./fastlane/build/#{$project_abbreviation}"
# 1.1 編譯打包生成.app包文件
gym(
scheme: "#{$scheme_name}", # 如果有多個Target,需要指定scheme名
workspace: "#{$project_name}.xcworkspace", # 如果項目使用CocoaPods需要加上
clean: true, # 是否編譯前clean
configuration: "Release", # Debug或Release
output_directory: outputDir, # 自定義導出包的Copy目錄锣光,默認存放outputDir/項目名.app
export_xcargs: "-allowProvisioningUpdates", # 啟用Xcode自動簽名
export_method:"mac-application" # 打包方式(app-store, validation, ad-hoc, package, enterprise, development, developer-id and mac-application)
)
# 1.2 對1.1中已經(jīng)打包好的.app進行封裝笆怠,封裝成.dmg。
# 注意mac項目必須封裝成dmg誊爹,否則直接.app上傳到服務器是許多個文件列表蹬刷。
dmg(
path: outputDir, # required
output_path: "#{outputDir}.dmg", # optional
#filesystem: "hfs+", # optional, default is 'hfs+'
#format: "udzo", # optional, default is 'udzo'
)
# 1.3 上傳1.2中已經(jīng)已經(jīng)封裝好的.dmg文件
scp(
host: "xxx.xx.xxx.xxx", # 填寫你公司的服務器ip地址
username: "root", # 填寫你公司的服務器的用戶名
upload: {
src: "./fastlane/build/Demo.dmg", # 1.1中打好的.dmg包本機所在地址
dst: "/填寫服務器提供的目錄/Demo.dmg" # 服務器提供的.dmg存放路徑
}
)
end
end
3.fastlane使用:使用腳本啟動fastlane的命令
~ % cd 項目路徑
~ % touch ./fastlane/Fastlane.sh
~ % open ./fastlane/Fastlane.sh
在新建的Fastlane.sh腳本(shell)文件里填寫內容如下:
#!/bin/bash
ssh-add #若使用sshkey上傳服務器瓢捉,必須加否則會失敗
fastlane archive #打包并上傳
- 填寫后保存,在終端直接執(zhí)行該腳本文件即可
~ % cd 項目路徑
~ % sh ./fastlane/Fastlane.sh
七办成、補充知識:gym和build_app
1.gym
gym: Easily build and sign your app (via gym)
gym is part of fastlane: The easiest way to automate beta deployments and releases for your iOS and Android apps.
What's gym?
gym builds and packages iOS apps for you. It takes care of all the heavy lifting and makes it super easy to generate a signed ipa or app file ??
解釋
1泡态、gym可以幫助你編譯和打包安卓和iOSapp铲觉,它是fastlane的一部分决记,其中ios程序打包成.ipa包,mac程序打包成.app包垢夹。
2而克、gym工具只是xcodebuild工具的一個封裝靶壮,gym使用ruby編寫,相對于xcodebuild的寫法更簡單易用1员萍。
2.build_app
- 許多參數(shù)和gym相同腾降,直接使用gym即可。
八碎绎、關于fastlane版本升級
- 目前fastlane最新版本是2.211.0螃壤,我的版本是2.209.1,所以涉及到升級筋帖。
+------+---------------------+-------------+
| fastlane summary |
+------+---------------------+-------------+
| Step | Action | Time (in s) |
+------+---------------------+-------------+
| 1 | Verifying fastlane | 0 |
| | version | |
| 2 | default_platform | 0 |
| ?? | gym | 52 |
+------+---------------------+-------------+
[10:09:45]: fastlane finished with errors
[!] Error building the application - see the log above
#######################################################################
# fastlane 2.211.0 is available. You are on 2.209.1.
# You should use the latest version.
# Please update using `gem install fastlane`.
#######################################################################
2.211.0 Improvements
- 如上所示奸晴,fastlane打包時出現(xiàn)錯誤,提示升級日麸,按照提示進行升級寄啼,提示錯誤,解決方法是使用管理員權限進行升級
~ % gem install fastlane
Fetching fastlane-2.211.0.gem
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
正確的升級方法
sudo gem install fastlane -NV
輸入開機密碼回車即可
其它補充
- 查看fastlane當前最新版本號
~ % fastlane version
- 查看本機安裝的fastlane版本
~ % fastlane -v
fastlane installation at path:
/Library/Ruby/Gems/2.6.0/gems/fastlane-2.211.0/bin/fastlane
-----------------------------
[?] ??
fastlane 2.211.0
- 若fastlane 打包一直不成功赘淮,可嘗試刪除項目現(xiàn)有fastlane文件夾和Gemfile辕录、Gemfile.lock文件睦霎,再重新開始fastlane init
fastlane 升級后 還是舊版本
- 使用gem升級fastlane后梢卸,如果fastlane -v還是舊版本,是因為配置路徑的問題副女,電腦優(yōu)先搜索了根目錄路徑蛤高,而沒有搜索gem中最新版的fastlane。
解決辦法:
- 先查看本機fastlane路徑碑幅,再查看gem的fastlane路徑戴陡,然后cmd+shift+G 輸入定位到gem的fastlane路徑即 /usr/local/lib/ruby/gems/3.3.0/gems/fastlane-2.220.0,找到bin/fastlane這個Unix可執(zhí)行文件沟涨,直接復制覆蓋到/usr/local/bin/fastlane中即可
~ % which fastlane
/usr/local/bin/fastlane
~ % gem which fastlane
/usr/local/lib/ruby/gems/3.3.0/gems/fastlane-2.220.0/fastlane/lib/fastlane.rb