iOS自動化打包釋出(Jenkins + Fastlane + GitLab + 蒲公英)
前言
在專案測試階段,頻繁的打包釋出會耗費團隊很多的時間。搭建一個自動化打包釋出的平臺,可以將我們從那些繁瑣的打包釋出流程中解放出來。
本文采用的方案是:Jenkins + Fastlane + GitLab + 蒲公英。
Fastlane安裝
Fastlane是一套使用Ruby寫的自動化工具集,用於iOS和Android的自動化打包、釋出等工作,可以節省大量的時間。
安裝過程如下:
1.檢查Ruby版本,需要2.0及以上版本。在終端輸入以下命令確認:
ruby -v
gem sources
結果應為:
*** CURRENT SOURCES ***
https://gems.ruby-china.org/
2.檢查Xcode命令列工具是否安裝。在終端輸入以下命令:
xcode-select --install
如果沒有安裝會進行安裝。如果已經安裝了則會提示:
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
3.安裝Fastlane
sudo gem install fastlane --verbose
如果出現以下錯誤:
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted - /usr/bin/rougify
則輸入以下命令:
sudo gem install -n /usr/local/bin fastlane
4.檢查Fastlane是否正確安裝。輸入以下命令:
fastlane --version
可以看到Fastlane版本資訊,我的是2.46.1。
蒲公英的Fastlane外掛安裝
開啟終端,進入你的專案工程的根目錄,輸入以下命令:
fastlane add_plugin pgyer
出現
Plugin 'fastlane-plugin-pgyer' was added to './fastlane/Pluginfile'
It looks like fastlane plugins are not yet set up for this project.
fastlane will create a new Gemfile at path 'Gemfile'
This change is necessary for fastlane plugins to work
Should fastlane modify the Gemfile at path 'Gemfile' for you?
(y/n)
輸入y按回車,出現
Installing plugin dependencies...
Successfully installed plugins
便是安裝成功了。
Fastlane配置
1.開啟終端,進入你的專案工程的根目錄,輸入以下命令:
fastlane init
中間會讓你輸入蘋果開發者賬號的賬號和密碼,之後會在你專案工程的目錄下生成一個fastlane資料夾,裡面有Fastlane的配置檔案,一個是Appfile檔案,一個是Fastfile檔案(如果要上傳AppStore的話還有Deliverfile檔案)。Appfile儲存蘋果開發者的相關資訊、專案的相關資訊等。Fastfile是執行指令碼。
2.編輯Fastfile檔案
有時候一天需要打好幾個包,為了區分,我們這裡實現一個遞增build號的功能。
(1)修改專案工程配置
修改Build Settings中的Versioning配置,Current Project Version隨便填一個,Versioning System選擇Apple Generic。
修改Info.plist File路徑
(2)定義一個遞增build號的函式,新增到Fastfile中
def updateProjectBuildNumber
currentTime = Time.new.strftime("%Y%m%d")
build = get_build_number()
if build.include?"#{currentTime}."
# => 為當天版本 計算迭代版本號
lastStr = build[build.length-2..build.length-1]
lastNum = lastStr.to_i
lastNum = lastNum + 1
lastStr = lastNum.to_s
if lastNum < 10
lastStr = lastStr.insert(0,"0")
end
build = "#{currentTime}.#{lastStr}"
else
# => 非當天版本 build 號重置
build = "#{currentTime}.01"
end
puts("*************| 更新build #{build} |*************")
# => 更改專案 build 號
increment_build_number(
build_number: "#{build}"
)
end
實現自動打包的完整Fastfile如下:
# 定義fastlane版本號
fastlane_version “2.46.1”
# 定義打包平臺
default_platform :ios
def updateProjectBuildNumber
currentTime = Time.new.strftime("%Y%m%d")
build = get_build_number()
if build.include?"#{currentTime}."
# => 為當天版本 計算迭代版本號
lastStr = build[build.length-2..build.length-1]
lastNum = lastStr.to_i
lastNum = lastNum + 1
lastStr = lastNum.to_s
if lastNum < 10
lastStr = lastStr.insert(0,"0")
end
build = "#{currentTime}.#{lastStr}"
else
# => 非當天版本 build 號重置
build = "#{currentTime}.01"
end
puts("*************| 更新build #{build} |*************")
# => 更改專案 build 號
increment_build_number(
build_number: "#{build}"
)
end
#指定專案的scheme名稱
scheme=“TestCI”
#蒲公英api_key和user_key
api_key=“”
user_key=“”
# 任務指令碼
platform :ios do
lane :development_build do|options|
branch = options[:branch]
puts “開始打development ipa”
updateProjectBuildNumber #更改專案build號
# 開始打包
gym(
#輸出的ipa名稱
output_name:”#{scheme}_#{get_build_number()}”,
# 是否清空以前的編譯資訊 true:是
clean:true,
# 指定打包方式,Release 或者 Debug
configuration:"Release",
# 指定打包所使用的輸出方式,目前支援app-store, package, ad-hoc, enterprise, development
export_method:"development",
# 指定輸出資料夾
output_directory:"./fastlane/build",
)
puts "開始上傳蒲公英"
# 開始上傳蒲公英
pgyer(api_key: “#{api_key}”, user_key: “#{user_key}”)
end
end
注意:蒲公英的 api_key 和 user_key,開發者在自己賬號下的 賬號設定-API資訊 中可以找到。打其它型別的包的方法與development類似,可自定義一個新的lane實現。
在終端輸入
fastlane development_build
便會進行自動打包並上傳蒲公英了。
Jenkins安裝
Jenkins 是一個開源專案,提供了一種易於使用的持續整合系統,使開發者從繁雜的整合中解脫出來,專注於更為重要的業務邏輯實現上。同時 Jenkins 能實施監控整合中存在的錯誤,提供詳細的日誌檔案和提醒功能,還能用圖表的形式形象地展示專案構建的趨勢和穩定性。
2.開啟終端,進入war包所在目錄,執行以下命令:
java -jar jenkins.war
4.彈出Jenkins安裝介面,會讓你輸入安全密碼,輸入完成之後選擇預設安裝。
5.新建管理員賬號密碼
安裝外掛
安裝GitLab外掛
在可選外掛中選擇GitLab Plugin和Gitlab Hook Plugin進行安裝。
安裝Xcode外掛
在可選外掛中選擇Xcode integration進行安裝。
安裝完成之後,我們就可以配置構建專案了。
構建任務
1.點選新建,輸入名稱,構建一個自由風格的軟體專案
2.配置Git
新增Git倉庫地址,可以是HTTP也可以是SSH。點選Add
如果是HTTP
如果是SSH
注意:UserName是取一個名字,填寫的Key是私鑰。
3.配置指令碼
因為我的Git倉庫的目錄是這樣的
所以在執行fastlane development_build之前需要進入TestCI目錄,即fastlane資料夾所在目錄。
4.新增完成之後點選立即構建
5.執行成功顯示如下
可以點選進入檢視控制檯輸出