fastlane 環(huán)境搭建
- 終端執(zhí)行下面命令進行安裝:
sudo gem install fastlane -NV
使用上面命令安裝時可以切換gem源為ruby-china.com 脆侮,點此鏈接去切換
或者
brew install fastlane
- iOS相關(guān)設(shè)置
Xcode command line tools (macOS) 如果沒安裝則需要安裝,如下命令:
xcode-select --install
- 在工程目錄下創(chuàng)建Gemfile文件勇劣,寫入如下內(nèi)容:
source "https://gems.ruby-china.com/"
gem "fastlane"
gem "cocoapods"
plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
eval_gemfile(plugins_path) if File.exist?(plugins_path)
上面的 source 改成 gems.ruby-china.com 會快一點
- 終端cd到工程目錄下靖避,使用下面初始化fastlane:
fastlane init
上面會彈出4個選項,我們選擇第4項手動配置比默,下面第6步會進行配置
fastlane add_plugin fir_cli
從fir.im獲取API Token幻捏,https://www.betaqr.com/apps
編輯工程目錄下fastlane/Fastfile文件:
7.1 打包ipa如下設(shè)置:
default_platform(:ios)
platform :ios do
desc "上傳到fir"
lane :gofir do
cocoapods # 執(zhí)行 pod install
build_app( # 構(gòu)建app,archive操作
workspace: "**.xcworkspace", # 指定工程文件
scheme: "**", # 指定scheme
configuration: "Adhoc", # 打包模式
export_method: "ad-hoc", # 導(dǎo)出方式
silent: true, # 隱藏構(gòu)建app時輸出的不必要的信息
)
# 上傳到fir退敦,
# api_token 參數(shù)為上面從fir.im 獲取的API Token粘咖,
# changelog 參數(shù)為變更日志,fir.im上的變更日志
# 多個參數(shù) 可以使用逗號(, )分離
answer = fir_cli api_token: "***", changelog: "**",need_release_id:true
# 返回下載地址侈百,此項為fir上面的下載地址
download_url = "***?release_id=#{answer[:release_id]}"
# 通知瓮下,此為macOS的系統(tǒng)通知,點擊通知會在瀏覽器中打開上傳到fir的鏈接钝域,復(fù)制這個鏈接就可以發(fā)送給測試人員了
notification(title: "發(fā)布成功!", message: "已成功上傳到fir平臺, 趕快聯(lián)系測試人員開始測試吧!", open: download_url)
end
desc "上傳到app_store"
lane :release do
cocoapods
build_app(
workspace: "**.xcworkspace",
scheme: "**",
configuration: "Release",
silent: true,
)
upload_to_app_store( # 上傳到app store
force: true, # 跳過HTML預(yù)覽文件的驗證
skip_metadata: true, # 不要上傳元數(shù)據(jù)(例如標題讽坏,描述)。這仍然會上傳屏幕截圖
skip_screenshots: true, # 不要上傳屏幕截圖
)
end
end
lane 后面是定義一個Action例证,Action可以做一些操作路呜,上面定義了兩個Action,分別可以上傳到fir和app store织咧,其他具體使用文檔可以看下官方文檔胀葱,http://docs.fastlane.tools/getting-started/ios/beta-deployment/。
上面的通知可以改成 企業(yè)微信的群聊機器人笙蒙,但是現(xiàn)在公司內(nèi)群聊機器人未開通抵屿,暫時不做處理
7.2 自動打包靜態(tài)庫或者動態(tài)庫,并且合并armv7 i386 x86_64 arm64捅位,如下設(shè)置:
動態(tài)庫和靜態(tài)庫區(qū)別:https://juejin.cn/post/6844904031937101838
platform :ios do
desc "靜態(tài)庫或者動態(tài)庫打包"
lane :dy do
#build for iphonesimulator
xcodebuild(
target: "ACSCategroies", #打包的Target 需要設(shè)置
configuration: "Release",#打包的模式 需要設(shè)置
silent: false,
clean: true, #打包前清理
build: true,
sdk:"iphonesimulator", #打包環(huán)境
xcargs: "-UseModernBuildSystem=0"
)
#build for iphone
xcodebuild(
target: "ACSCategroies", #打包的Target 需要設(shè)置
configuration: "Release", #打包的模式 需要設(shè)置
silent: false,
clean: true, #打包前清理
build: true, #打包環(huán)境
xcargs: "-UseModernBuildSystem=0",
)
# 上面參數(shù)文檔http://docs.fastlane.tools/actions/gym/#parameters
# 合并模擬器和真機SDK
# 獲取當前路徑
code_path = File.expand_path("..", File.dirname(__FILE__)).to_s
target = "ACSCategroies"
# SDK路徑
iphonesimulator_path = "#{code_path}/build/Release-iphonesimulator/#{target}.framework/#{target}"
iphoneos_path = "#{code_path}/build/Release-iphoneos/#{target}.framework/#{target}"
# 定義輸出的framework路徑
out_path = "#{code_path}/build/framework/#{target}.framework"
# 下面合并庫會遇到一個have the same architectures (arm64) and can't be in the same fat output file的錯誤
# 因為Xcode12模擬器架構(gòu)下的靜態(tài)庫有arm64轧葛,真機架構(gòu)下的靜態(tài)庫也有arm64搂抒,有相同的架構(gòu)導(dǎo)致不能合并笼裳。
# 先將模擬器庫的arm64架構(gòu)移除矗愧,然后再合并
# 合并后的路徑在工程路徑下build/framework/
# Two libraries are combined into one, support x86_64, armv7 ,arm64
command = "lipo #{iphonesimulator_path} -remove arm64 -output #{iphonesimulator_path} &&
mkdir #{code_path}/build/framework &&
lipo -create #{iphoneos_path} #{iphonesimulator_path} -output #{out_path}"
result = `#{command}`
end
end
合并庫會遇到一個have the same architectures (arm64) and can't be in the same fat output file的錯誤
因為Xcode12模擬器架構(gòu)下的靜態(tài)庫有arm64乏沸,真機架構(gòu)下的靜態(tài)庫也有arm64段只,有相同的架構(gòu)導(dǎo)致不能合并法牲。
先將模擬器庫的arm64架構(gòu)移除巾腕,然后再合并
合并后的路徑在工程路徑下build/framework/
如果用下面的打包成xcframework就不會出現(xiàn)上面的情況
6.3 自動打包xcframework示血,推薦使用氧卧,如下設(shè)置:
platform :ios do
desc "靜態(tài)庫或者動態(tài)庫打包"
lane :xc do
#build for iphonesimulator
xcodebuild(
target: "ACSCategroies", #打包的Target 需要設(shè)置
configuration: "Release",#打包的模式 需要設(shè)置
silent: false,
clean: true, #打包前清理
build: true,
sdk:"iphonesimulator", #打包環(huán)境
xcargs: "-UseModernBuildSystem=0"
)
#build for iphone
xcodebuild(
target: "ACSCategroies", #打包的Target 需要設(shè)置
configuration: "Release", #打包的模式 需要設(shè)置
silent: false,
clean: true, #打包前清理
build: true, #打包環(huán)境
xcargs: "-UseModernBuildSystem=0",
)
# 上面參數(shù)文檔http://docs.fastlane.tools/actions/gym/#parameters
# 獲取當前路徑
code_path = File.expand_path("..", File.dirname(__FILE__)).to_s
target = "ACSCategroies"
# SDK路徑
iphonesimulator_path = "#{code_path}/build/Release-iphonesimulator/#{target}.framework"
iphoneos_path = "#{code_path}/build/Release-iphoneos/#{target}.framework"
# 定義輸出的xcframework路徑
out_path = "#{code_path}/build/xcframework/#{target}.xcframework"
# 合并成xcframework
# 文檔http://docs.fastlane.tools/actions/create_xcframework/
create_xcframework(
frameworks:[iphonesimulator_path,iphoneos_path], # 兩個Framework
output:out_path # 輸出路徑
)
end
end
- 終端執(zhí)行如下代碼上傳到 fir
fastlane gofir
執(zhí)行如下代碼上傳到app store
fastlane release
上傳到app store 需要配置fastlane/Appfile文件右莱,文檔http://docs.fastlane.tools/advanced/Appfile/蚜锨,如果不配置,則需要在終端輸入賬戶名稱慢蜓、密碼和兩步驗證碼
執(zhí)行如下代碼打包靜態(tài)庫或者動態(tài)庫
fastlane dy
執(zhí)行如下代碼打包xcframework
fastlane xc
- git自動commit和push 參考文檔:http://docs.fastlane.tools/actions/#source-control
至此上面fastlane + Fir 自動打包上傳Fir完成了亚再!
Jenkins
如果需要提交代碼后自動打包,或者選擇分支進行打包晨抡,需要用到Jenkins
- 安裝Jenkins氛悬,安裝文檔:https://www.jenkins.io/zh/doc/book/installing/
brew install jenkins
解鎖 Jenkins,瀏覽http://localhost:8080 耘柱,安裝推薦插件
設(shè)置gitlab的憑據(jù)如捅,訪問gitlab或者github創(chuàng)建一個訪問令牌,進入http://localhost:8080/credentials/store/system/domain/_/newCredentials 添加gitlab或者github上的賬戶和訪問令牌
創(chuàng)建任務(wù)
源碼管理:添加工程的源碼地址调煎,gitlab或者github的地址镜遣,Credentials選擇上面創(chuàng)建的憑據(jù)。
觸發(fā)構(gòu)建器:此項是為自動構(gòu)建的條件
下面拿幾項說明下
定時構(gòu)建為設(shè)置一段時間自動構(gòu)建
Build when a change is pushed to GitLab. GitLab webhook URL: 此項為推送代碼到倉庫時自動構(gòu)建士袄,此項需要去gitlab設(shè)置webhook為構(gòu)建app的地址悲关,因此機器和gitlab機器不是同一網(wǎng)絡(luò),所以此項不通
輪詢SCM娄柳,是設(shè)置一段時間自動去查看倉庫有沒有commit寓辱,有commit自動去構(gòu)建
構(gòu)建:此項選擇執(zhí)行shell 腳本
#!/bin/bash -ilex
cd ./**/ # cd到工程目錄
fastlane gofir
# fastlane release 或者執(zhí)行這個上傳到app store
構(gòu)建后操作:此項為構(gòu)建完成后的操作,我們可以做發(fā)送通知的操作赤拒,鑒于上面的fastlane已經(jīng)做了構(gòu)建完成后的通知了秫筏,我們可以不做操作
5.另外加個選擇分支手動構(gòu)建
Jenkins在管理中選插件,安裝Git Parameter插件
安裝完成之后項目配置中多個如下選擇:
勾選它挎挖,選擇git Parameter
添加如下參數(shù)
構(gòu)建shell修改如下:
#!/bin/bash -ilex
cd ./**/ # cd到工程目錄下
if [ "${fastlane_type}" = "fir" ];then
bundle exec fastlane gofir
elif [ "${fastlane_type}" = "app_store" ];then
bundle exec fastlane release
fi
然后我們就可以選擇指定分支進行構(gòu)建了: