添加buildcode自动增长的业务逻辑

main
liuchunguang 2026-03-10 14:57:28 +08:00
parent 928e50c6a0
commit ab01d86919
6 changed files with 174 additions and 3 deletions

View File

@ -600,7 +600,14 @@ internal class BatchBuild
YooAssetBuildBase.isOversea = config.IsOversea;
// 处理BuildCode
if (config.BuildCode != 0)
// 若命令行已通过 -buildCode 传入(由 version_code_manager.py 自动递增),优先使用命令行值,不用 YAML 覆盖
bool hasCmdBuildCode = System.Array.Exists(
Environment.GetCommandLineArgs(), a => a == "-buildCode");
if (hasCmdBuildCode)
{
Debug.Log($"[BuildCode] 命令行已传入 -buildCode跳过 YAML 中 BuildCode={config.BuildCode} 的覆盖");
}
else if (config.BuildCode != 0)
{
Debug.Log("设置BuildCode:" + config.BuildCode);
#if UNITY_IOS

@ -1 +1 @@
Subproject commit 57fe15b36da43f1e1e62712fa9bdbb31cc3508a2
Subproject commit 917783e576205a5a558a695409833b8fe3e6d7b5

View File

@ -234,6 +234,39 @@ pipeline {
}
}
}
stage('获取构建版本号') {
steps {
script {
try {
def channelYamlPath = "${WORKSPACE}/Tool/Channels/${CHANNEL_PARAM}/ChannelSetting.yaml"
def versionCodeScript = "${WORKSPACE}/Tool/py_tools/version_code_manager.py"
// 使 python3 export
def exportLines = sh(
script: "python3 '${versionCodeScript}' '${CHANNEL_PARAM}' '${channelYamlPath}'",
returnStdout: true
).trim()
echo "[BuildCode] 脚本输出: ${exportLines}"
// NLD_CURRENT_VERSION_CODE=xxx
def versionCodeLine = exportLines.split('\n').find { it.contains('NLD_CURRENT_VERSION_CODE') }
if (versionCodeLine) {
def versionCode = versionCodeLine.replaceAll('export NLD_CURRENT_VERSION_CODE=', '').trim()
echo "[BuildCode] 当前渠道=${CHANNEL_PARAM}versionCode=${versionCode}"
// stage shell 使
def safeChannel = CHANNEL_PARAM.replace('-', '_').replace(' ', '_')
env."NLD_CLIENT_${safeChannel}_VERSION_CODE" = versionCode
env.NLD_CURRENT_VERSION_CODE = versionCode
} else {
echo "[BuildCode] 警告: 未能解析 NLD_CURRENT_VERSION_CODE将使用 YAML 中的 BuildCode"
}
} catch (err) {
echo "[BuildCode] 警告: 获取构建版本号失败: ${err},将使用 YAML 中的 BuildCode"
// 使 YAML
}
}
}
}
stage('打包') {
steps {
sh "chmod +x $WORKSPACE/Tool/build_tools/build_unity.sh"

View File

@ -274,6 +274,39 @@ pipeline {
}
}
}
stage('获取构建版本号') {
steps {
script {
try {
def channelYamlPath = "${WORKSPACE}/Tool/Channels/${CHANNEL_PARAM}/ChannelSetting.yaml"
def versionCodeScript = "${WORKSPACE}/Tool/py_tools/version_code_manager.py"
// 使 python3 export
def exportLines = sh(
script: "python3 '${versionCodeScript}' '${CHANNEL_PARAM}' '${channelYamlPath}'",
returnStdout: true
).trim()
echo "[BuildCode] 脚本输出: ${exportLines}"
// NLD_CURRENT_VERSION_CODE=xxx
def versionCodeLine = exportLines.split('\n').find { it.contains('NLD_CURRENT_VERSION_CODE') }
if (versionCodeLine) {
def versionCode = versionCodeLine.replaceAll('export NLD_CURRENT_VERSION_CODE=', '').trim()
echo "[BuildCode] 当前渠道=${CHANNEL_PARAM}versionCode=${versionCode}"
// stage shell 使
def safeChannel = CHANNEL_PARAM.replace('-', '_').replace(' ', '_')
env."NLD_CLIENT_${safeChannel}_VERSION_CODE" = versionCode
env.NLD_CURRENT_VERSION_CODE = versionCode
} else {
echo "[BuildCode] 警告: 未能解析 NLD_CURRENT_VERSION_CODE将使用 YAML 中的 BuildCode"
}
} catch (err) {
echo "[BuildCode] 警告: 获取构建版本号失败: ${err},将使用 YAML 中的 BuildCode"
// 使 YAML
}
}
}
}
stage('打包') {
steps {
sh "chmod +x $WORKSPACE/Tool/build_tools/build_unity.sh"

@ -1 +1 @@
Subproject commit 012eb770a40318bc512688cc3400a188ba5df0dc
Subproject commit 84d15c12d6240eff25c3ea2da15b2bca32e720a0

View File

@ -0,0 +1,98 @@
#!/usr/bin/env python3
# version_code_manager.py
# 用途:管理各渠道的 Android versionCode / iOS buildNumber 自动递增
#
# 逻辑:
# 1. 读取环境变量 NLD_CLIENT_{ChannelName}_VERSION_CODE
# 2. 若环境变量不存在,则从对应渠道的 ChannelSetting.yaml 中读取 BuildCode 作为初始值
# 3. 将该值 +1 后写回环境变量(通过写出 shell export 语句供调用方 source
# 4. 将最终使用的 code 打印到 stdout供调用方捕获
#
# 使用方式(在 build_unity.sh 中):
# source <(python3 .../version_code_manager.py <channel_name> <channel_yaml_path>)
# echo "VERSION_CODE=$NLD_CLIENT_<channel>_VERSION_CODE"
#
# 传入参数:
# argv[1]: ChannelName (e.g. "Dev")
# argv[2]: ChannelYamlPath (e.g. "/path/to/Tool/Channels/Dev/ChannelSetting.yaml")
#
# 退出码:
# 0: 成功
# 1: 参数不足
# 2: yaml 文件不存在
# 3: yaml 解析失败
# 4: BuildCode 字段不存在
import sys
import os
import yaml
def main():
if len(sys.argv) < 3:
print("Usage: python3 version_code_manager.py <ChannelName> <ChannelYamlPath>", file=sys.stderr)
sys.exit(1)
channel_name = sys.argv[1]
yaml_path = sys.argv[2]
# 环境变量名称,将渠道名中的特殊字符替换为下划线,保持规范
safe_channel = channel_name.replace("-", "_").replace(" ", "_")
env_var_name = f"NLD_CLIENT_{safe_channel}_VERSION_CODE"
# 1. 尝试从环境变量读取当前值
current_value_str = os.environ.get(env_var_name)
if current_value_str is not None:
# 环境变量存在,解析为整数
try:
current_code = int(current_value_str)
print(f"[VersionCode] 从环境变量 {env_var_name} 读取到当前值: {current_code}", file=sys.stderr)
except ValueError:
print(f"[VersionCode] 警告: 环境变量 {env_var_name} 的值 '{current_value_str}' 无法解析为整数,将从 YAML 重新读取", file=sys.stderr)
current_code = read_build_code_from_yaml(yaml_path)
else:
# 环境变量不存在,从 YAML 读取初始值
print(f"[VersionCode] 环境变量 {env_var_name} 不存在,从 YAML 读取初始值", file=sys.stderr)
current_code = read_build_code_from_yaml(yaml_path)
# 2. 递增
new_code = current_code + 1
print(f"[VersionCode] 渠道={channel_name}, 旧值={current_code}, 新值={new_code}", file=sys.stderr)
# 3. 输出 export 语句供 shell source写到 stdout
# 同时输出 BUILD_CODE 变量供 build_unity.sh 直接使用
print(f"export {env_var_name}={new_code}")
print(f"export NLD_CURRENT_VERSION_CODE={new_code}")
def read_build_code_from_yaml(yaml_path):
"""从 ChannelSetting.yaml 读取 BuildCode 字段"""
if not os.path.exists(yaml_path):
print(f"[VersionCode] 错误: YAML 文件不存在: {yaml_path}", file=sys.stderr)
sys.exit(2)
try:
with open(yaml_path, 'r', encoding='utf-8') as f:
data = yaml.safe_load(f)
except yaml.YAMLError as e:
print(f"[VersionCode] 错误: 解析 YAML 失败: {e}", file=sys.stderr)
sys.exit(3)
build_code = data.get("BuildCode")
if build_code is None:
print(f"[VersionCode] 错误: ChannelSetting.yaml 中不存在 BuildCode 字段", file=sys.stderr)
sys.exit(4)
try:
code = int(build_code)
except (ValueError, TypeError):
print(f"[VersionCode] 错误: BuildCode 值 '{build_code}' 无法解析为整数", file=sys.stderr)
sys.exit(4)
print(f"[VersionCode] 从 YAML 读取到 BuildCode 初始值: {code}", file=sys.stderr)
return code
if __name__ == "__main__":
main()