1.前言
本篇文章主要講解了 Fastlane 如何實現多 Target 自動化打包發布,以及如何配置 Appfile、Deliverfile 和 Fastfile 等配置文件,發布和構建版本號自增,修改更新內容,自動上傳蘋果商店和自動提交審核等內容,希望能夠為您的工作或學習帶來有價值的信息。
2.簡介
Fastlane 是一套使用Ruby寫的自動化工具集,用于iOS和Android的自動化打包、發布等工作,可以節省大量的時間。
3.安裝
Fastlane安裝及簡單使用,請參考另一篇文章:iOS持續集成:Fastlane + 蒲公英 自動打包發布
4.配置
4.1 打開終端,進入你的工程目錄,然后執行 fastlane init:
執行過程中,需要輸入對應的蘋果開發賬號。接下來,出現確認提示,確認無誤輸入y:
4.2 Fastlane 初始化完成后,工程目錄下會自動生成 fastlane 文件夾,如下所示:
-
metadata
: 目錄:存放 App 元數據,包括 App 簡介,Icon,Copyright 等; -
screenshots
: 目錄:顧名思義,存放 App store 中的截圖; -
Appfile
:用于指定app_identifier
,apple_id
,team_id
; -
Deliverfile
:用于指定跟 App 版本發布相關的信息,除了apple_identifier
外,還包括submit_for_review
,automatic_release
等可配置項,基本覆蓋 iTunes Connect 里面的所有選項 -
Fastfile
:用于編寫邏輯腳本,使用 ruby 語言,例如首先執行 cocoapods 更新第三方依賴庫,然后執行 pem 更新相關證書,接著通過 gym 來編譯并打包 ipa 文件,最后通過 deliver 發布到 iTunes Connect 中,并提交審核。
4.3 配置文件
因為 Fastlane 默認只有一個 target ,單個 target 配置可參考 iOS持續集成:Fastlane + 蒲公英 自動打包發布 中關于 .env
文件的配置。對于多個 target 可以通過配置多個 .env
文件。
具體操作如下:
打開終端,進入工程目錄下,分別創建兩個 .env
文件。下面是我的項目中創建的兩個 .env
文件: .env.SunfoBank
和 .env.SunfoBankPlus
,截圖如下:
文件內容如下:
.env.SunfoBank
#APP唯一標識符
APP_IDENTIFIER = "xxx.xxx"
#發布版本號
APP_VERSION_RELEASE = "3.4.0"
#新版本修改記錄
RELEASE_NOTES = "1) 升級測試第一行\n2) 升級測試第二行"
#蒲公英 更新描述
PGY_UPDATE_DESCRIPTION = "fastlane自動打包上傳測試"
#自動提交審核
SUBMIT_FOR_REVIEW = false
#審核通過后立刻發布
AUTOMATIC_RELEASE = false
#蘋果開發者賬號
APPLE_ID = "xxx@xxx.xxx"
#蘋果開發者帳號密碼
FASTLANE_PASSWORD = "xxxxxx"
#套裝ID
TEAM_ID = "90xxxxxx02"
#應用名稱
SCHEME_NAME = "SunfoBank"
#APP元數據及截圖存放路徑
METADATA_PATH = "./metadata/SunfoBank"
SCREENSHOTS_PATH = "./screenshots/SunfoBank"
#APP元數據及截圖下載時,直接覆蓋原有數據,不詢問
DELIVER_FORCE_OVERWRITE = true
.env.SunfoBankPlus
#APP唯一標識符
APP_IDENTIFIER = "xxx.xxx.xxx"
#發布版本號
APP_VERSION_RELEASE = "3.4.0"
#新版本修改記錄
RELEASE_NOTES = "1) 升級測試第一行\n2) 升級測試第二行"
#蒲公英 更新描述
PGY_UPDATE_DESCRIPTION = "fastlane自動打包上傳測試"
#自動提交審核
SUBMIT_FOR_REVIEW = false
#審核通過后立刻發布
AUTOMATIC_RELEASE = false
#蘋果開發者賬號
APPLE_ID = "xxx@xxx.com"
#蘋果開發者帳號密碼
FASTLANE_PASSWORD = "xxxxxx"
#套裝ID
TEAM_ID = "11xxxxxx06"
#應用名稱
SCHEME_NAME = "SunfoBankCopy"
#APP元數據及截圖存放路徑
METADATA_PATH = "./metadata/SunfoBankCopy"
SCREENSHOTS_PATH = "./screenshots/SunfoBankCopy"
#APP元數據及截圖下載時,直接覆蓋原有數據,不詢問
DELIVER_FORCE_OVERWRITE = true
截圖如下:
同時,我的項目中的 Appfile
、Deliverfile
和 Fastfile
配置文件修改如下:
Appfile
app_identifier ENV['APP_IDENTIFIER'] # The bundle identifier of your app
apple_id ENV['APPLE_ID'] # Your Apple email address
team_id ENV['TEAM_ID'] # Developer Portal Team ID
Deliverfile
app_identifier ENV['APP_IDENTIFIER'] # The bundle identifier of your app
username ENV['APPLE_ID'] # your Apple ID user
# 元數據的路徑
metadata_path ENV['METADATA_PATH']
screenshots_path ENV['SCREENSHOTS_PATH']
#App store 中待發布的 App 版本,若每個 target 的版本號不同,可以通過.env 文件來分別定義
app_version ENV['APP_VERSION_RELEASE']
#下載 metadata 及 screenshots 時直接覆蓋,不詢問
force true
#不覆蓋 iTunes Connect原有截圖
skip_screenshots true
#自動提交審核
submit_for_review ENV['SUBMIT_FOR_REVIEW']
#審核通過后立刻發布
automatic_release ENV['AUTOMATIC_RELEASE']
#新版本修改記錄
release_notes({
"zh-Hans" => ENV['RELEASE_NOTES']
})
#App 加密算法使用情況及廣告相關配置
submission_information({
#Export Compliance
export_compliance_available_on_french_store: "false",
export_compliance_contains_proprietary_cryptography: "false",
export_compliance_contains_third_party_cryptography: "false",
export_compliance_is_exempt: "false",
export_compliance_uses_encryption: "false",
export_compliance_app_type: nil,
export_compliance_encryption_updated: "false",
export_compliance_compliance_required: "false",
export_compliance_platform: "ios",
content_rights_contains_third_party_content: "false",
content_rights_has_rights: "false",
#Advertising Identifier
add_id_info_limits_tracking: "false",
add_id_info_serves_ads: "false",
add_id_info_tracks_action: "false",
add_id_info_tracks_install: "false",
add_id_info_uses_idfa: "false"
});
Fastfile
#---------- begin -----------
def update_build_number()
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
def prepare_version(options)
# 正式版本號
increment_version_number(
version_number: options[:version]
)
# 構建版本號
update_build_number()
end
def get_update_description()
return "版本號:\n\b\b#{ENV['APP_VERSION_RELEASE']} \n更新描述:\n\b\b#{ENV['RELEASE_NOTES']}"
end
#---------- end -------------
#---------- begin -----------
fastlane_version "2.54.3"
default_platform :ios
platform :ios do
before_all do
# ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
cocoapods
end
desc "1).優先版 發布到 蒲公英"
lane :beta_main_pgy do
sh "fastlane beta_pgy --env SunfoBank"
end
desc "2).馬甲版 發布到 蒲公英"
lane :beta_plus_pgy do
sh "fastlane beta_pgy --env SunfoBankPlus"
end
desc "3).優先版 發布到 蘋果商店"
lane :release_mian_apple do
sh "fastlane release_apple --env SunfoBank"
end
desc "4).馬甲版 發布到 蘋果商店"
lane :release_plus_apple do
sh "fastlane release_apple --env SunfoBankPlus"
end
desc "5).優先版 和 馬甲版 同時 發布到 蒲公英"
lane :beta_all_pgy do
sh "fastlane beta_pgy --env SunfoBank"
sh "fastlane beta_pgy --env SunfoBankPlus"
end
desc "6).優先版 和 馬甲版 同時 發布到 蘋果TestFlight"
lane :beta_all_apple do
sh "fastlane beta_apple --env SunfoBank"
sh "fastlane beta_apple --env SunfoBankPlus"
end
desc "7).優先版 和 馬甲版 同時 發布到 蘋果商店"
lane :release_all_apple do
sh "fastlane release_apple --env SunfoBank"
sh "fastlane release_apple --env SunfoBankPlus"
end
desc "發布 指定版本 到 蒲公英"
lane :beta_pgy do
# 更新版本號
increment_version_number_in_plist(target: ENV['SCHEME_NAME'], version_number: ENV['APP_VERSION_RELEASE'])
gym(scheme: ENV['SCHEME_NAME'],
export_method: "ad-hoc",
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
pgyer(api_key: "d066f633dc2d970eb230dba7823ff022",
user_key: "4477d913a078c11df32be931523619dc",
update_description: get_update_description(),
password: "123456",
install_type: "2")
end
desc "發布 指定版本 到 蘋果TestFlight"
lane :beta_apple do
# 更新版本號
increment_version_number_in_plist(target: ENV['SCHEME_NAME'], version_number: ENV['APP_VERSION_RELEASE'])
gym(scheme: ENV['SCHEME_NAME'],
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
pilot
end
desc "發布 指定版本 到 蘋果商店"
lane :release_apple do
# 更新版本號
increment_version_number_in_plist(target: ENV['SCHEME_NAME'],
version_number: ENV['APP_VERSION_RELEASE'])
gym(scheme: ENV['SCHEME_NAME'],
silent: true, # 隱藏沒有必要的信息
clean: true # 在構建前先clean
)
deliver
end
#---------- 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
截圖如下:
5.執行
這里以打包上傳蒲公英為例,如果沒有安裝蒲公英插件,可以參考這篇文章:iOS持續集成:Fastlane + 蒲公英 自動打包發布,如果以上環境配置成功,我們可以執行以下命令:
bundle exec fastlane ios
然后順利的話,將會出現類似如下的選擇提示,截圖圖如下:
我們可以通過選擇不同的序號并輸入,來打包上傳到不同的平臺,此時,我們選擇序號為5,同時將兩個target版本打包上傳到蒲公英平臺。
稍等幾分鐘,如果出現類似下面的信息,就說明打包上傳蒲公英成功了,是不是很方便!!!
通過終端輸出的日志文件,我們同時可以看到編譯、打包和上傳的全過程,代碼中出現的警告信息,也可以作為優化時的參考。到這里Fastlane 自動打包上傳就完成了。