feat: 支持多项目打包
- config.json 改为按项目存储(projects.<id>),上传配置全局共用;旧单项目配置自动迁移为 readoor
- 新增项目管理(管理员):Git 仓库/凭据、源码目录、Xcode 工程、拷贝条目、配置替换方式、自定义字段、替换规则
- 通用配置替换:pbxproj 版本号与签名,patch_rules 支持 define / replace_line 及 ${app.X}、${server.X} 模板
- 请求通过 X-Project 头选择项目;打包任务记录 project_id,历史按项目过滤
- 前端顶部切换项目,App/服务器环境表单按项目显示字段
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
2bd5c7057f
commit
8f2f23682f
@@ -3,6 +3,7 @@ import asyncio
|
||||
import json
|
||||
import os
|
||||
import plistlib
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import zipfile
|
||||
@@ -12,16 +13,14 @@ from typing import List
|
||||
|
||||
from ..config import (
|
||||
BUILD_BASE_DIR,
|
||||
COPY_ITEMS,
|
||||
DEFAULT_BUILD_DIR_RETENTION_HOURS,
|
||||
BUILD_TIMEOUT_HOURS,
|
||||
SKINS_DIR,
|
||||
AUTOMATION_DIR,
|
||||
get_source_dir,
|
||||
get_shared_source_dir,
|
||||
get_git_remote_url,
|
||||
mask_git_remote_url,
|
||||
)
|
||||
from ..projects import LEGACY_PROJECT_ID, get_definition, get_project_source
|
||||
from .log_streamer import log_streamer
|
||||
from .distribution import DistributionError, publish_dsym, publish_ipa
|
||||
from .notification import NotificationError, send_dingtalk_notification
|
||||
@@ -37,9 +36,36 @@ class BuildError(Exception):
|
||||
self.detail = detail
|
||||
|
||||
|
||||
# 共享源码目录只能同时被一个任务切换、清理和复制。
|
||||
# 后续在各自 build_dir 中执行的配置、依赖安装和编译不受此锁限制。
|
||||
source_prepare_lock = asyncio.Lock()
|
||||
# 共享源码目录只能同时被一个任务切换、清理和复制,每个源码目录一把锁,
|
||||
# 不同项目的源码准备互不阻塞。后续在各自 build_dir 中执行的配置、依赖安装和编译不受此锁限制。
|
||||
_source_prepare_locks: dict = {}
|
||||
|
||||
# 打包目录名:build_<项目 ID>_<任务 ID>
|
||||
_BUILD_DIR_PATTERN = re.compile(r"^build_[a-z][a-z0-9_-]{0,31}_.+")
|
||||
|
||||
|
||||
def _source_prepare_lock(source_dir: Path) -> asyncio.Lock:
|
||||
return _source_prepare_locks.setdefault(str(source_dir.resolve()), asyncio.Lock())
|
||||
|
||||
|
||||
def _task_project_id(task) -> str:
|
||||
project_id = getattr(task, "project_id", None)
|
||||
return project_id if isinstance(project_id, str) and project_id else LEGACY_PROJECT_ID
|
||||
|
||||
|
||||
def load_task_project(task) -> dict:
|
||||
"""读取任务所属项目的配置视图(见 routers.config.load_config)。"""
|
||||
from fastapi import HTTPException
|
||||
from ..routers.config import load_config
|
||||
try:
|
||||
return load_config(_task_project_id(task))
|
||||
except HTTPException as exc:
|
||||
raise BuildError(str(exc.detail), category="config") from exc
|
||||
|
||||
|
||||
def get_task_definition(task) -> dict:
|
||||
project_id = _task_project_id(task)
|
||||
return get_definition(project_id, load_task_project(task))
|
||||
|
||||
|
||||
def _patch_afnetworking_private_headers(build_dir: Path) -> int:
|
||||
@@ -217,7 +243,7 @@ def _cleanup_old_builds(db):
|
||||
_cleanup_superseded_tasks(db)
|
||||
return
|
||||
for d in base.iterdir():
|
||||
if d.is_dir() and d.name.startswith("build_readoor_"):
|
||||
if d.is_dir() and _BUILD_DIR_PATTERN.match(d.name):
|
||||
try:
|
||||
if d.stat().st_mtime < cutoff:
|
||||
shutil.rmtree(d)
|
||||
@@ -291,16 +317,21 @@ def _db_update(db, task, **fields):
|
||||
db.commit()
|
||||
|
||||
|
||||
async def update_source(task_id: str, source_dir: Path, branch: str, ref_type: str = "branch"):
|
||||
"""将共享源码工作目录强制同步到指定远程分支或 Tag(ref_type="tag" 时 branch 为 Tag 名)。"""
|
||||
remote_url = get_git_remote_url(with_credentials=True)
|
||||
async def update_source(task_id: str, source_dir: Path, branch: str, ref_type: str = "branch",
|
||||
remote_url: str = None):
|
||||
"""将共享源码工作目录强制同步到指定远程分支或 Tag(ref_type="tag" 时 branch 为 Tag 名)。
|
||||
|
||||
remote_url 为带凭证的项目仓库地址,未传时使用 .env 中的 GIT_REMOTE_URL。
|
||||
"""
|
||||
if remote_url is None:
|
||||
remote_url = get_git_remote_url(with_credentials=True)
|
||||
remote_url_masked = mask_git_remote_url(remote_url)
|
||||
git_env = _git_env()
|
||||
|
||||
cloned = not source_dir.exists()
|
||||
if cloned:
|
||||
if not remote_url:
|
||||
raise Exception(f"源码目录不存在且未配置 GIT_REMOTE_URL: {source_dir}")
|
||||
raise Exception(f"源码目录不存在且未配置 Git 仓库地址: {source_dir}")
|
||||
source_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||
await log_streamer.emit(task_id, f"初始化共享源码目录: {remote_url_masked}")
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
@@ -381,25 +412,34 @@ async def get_source_commit(source_dir: Path) -> str:
|
||||
|
||||
|
||||
async def prepare_source_snapshot(task_id: str, task) -> tuple[Path, str]:
|
||||
"""准备任务独立源码快照;远程仓库模式下此过程全局串行。"""
|
||||
if not get_git_remote_url():
|
||||
source_dir = get_source_dir(task.branch)
|
||||
"""准备任务独立源码快照;远程仓库模式下同一源码目录的准备过程串行。"""
|
||||
project_id = _task_project_id(task)
|
||||
project = load_task_project(task)
|
||||
definition = get_definition(project_id, project)
|
||||
source = get_project_source(project_id, project)
|
||||
await log_streamer.emit(task_id, f"项目: {definition['name']}({project_id})")
|
||||
|
||||
if not source.remote_url:
|
||||
if source.manual_base is None:
|
||||
raise BuildError(f"项目 {definition['name']} 未配置 Git 仓库地址", category="config")
|
||||
source_dir = source.manual_base / task.branch
|
||||
if not source_dir.exists():
|
||||
raise Exception(
|
||||
f"分支源码目录不存在: {source_dir},请先在 GIT_SOURCE_BASE 下准备分支(或 Tag)代码,或配置 GIT_REMOTE_URL"
|
||||
)
|
||||
await log_streamer.emit(task_id, f"使用分支源码: {source_dir}")
|
||||
return await copy_source_code(task_id, task, source_dir), ""
|
||||
return await copy_source_code(task_id, task, source_dir, definition), ""
|
||||
|
||||
source_dir = get_shared_source_dir()
|
||||
source_dir = source.shared_dir
|
||||
await log_streamer.emit(task_id, "等待共享源码准备队列...")
|
||||
async with source_prepare_lock:
|
||||
async with _source_prepare_lock(source_dir):
|
||||
await log_streamer.emit(task_id, "开始准备共享源码快照")
|
||||
ref_type = "tag" if task.ref_type == "tag" else "branch"
|
||||
await update_source(task_id, source_dir, task.branch, ref_type)
|
||||
await update_source(task_id, source_dir, task.branch, ref_type,
|
||||
remote_url=source.remote_url_with_credentials)
|
||||
commit = await get_source_commit(source_dir)
|
||||
await log_streamer.emit(task_id, f"源码版本: {task.branch} @ {commit}")
|
||||
build_dir = await copy_source_code(task_id, task, source_dir)
|
||||
build_dir = await copy_source_code(task_id, task, source_dir, definition)
|
||||
await log_streamer.emit(task_id, "共享源码快照已就绪,开始并行打包")
|
||||
return build_dir, commit
|
||||
|
||||
@@ -438,6 +478,7 @@ async def run_build_task(task_id: str):
|
||||
nonlocal build_dir
|
||||
# 1. 串行准备源码快照;后续步骤在任务独立目录中并行执行。
|
||||
await asyncio.to_thread(_db_update, db, task, current_step="source_prepare")
|
||||
definition = await asyncio.to_thread(get_task_definition, task)
|
||||
build_dir, source_commit = await prepare_source_snapshot(task_id, task)
|
||||
await asyncio.to_thread(_db_update, db, task, build_dir=str(build_dir))
|
||||
|
||||
@@ -452,7 +493,7 @@ async def run_build_task(task_id: str):
|
||||
|
||||
# 3. 替换项目配置
|
||||
await asyncio.to_thread(_db_update, db, task, current_step="patch")
|
||||
await patch_project(task_id, task, config_data, build_dir)
|
||||
await patch_project(task_id, task, config_data, build_dir, definition)
|
||||
|
||||
# 3.5 安装依赖
|
||||
await asyncio.to_thread(_db_update, db, task, current_step="pod_install")
|
||||
@@ -465,7 +506,7 @@ async def run_build_task(task_id: str):
|
||||
|
||||
# 5. 构建项目
|
||||
await asyncio.to_thread(_db_update, db, task, current_step="build")
|
||||
ipa_path = await build_project(task_id, task, config_data, build_dir)
|
||||
ipa_path = await build_project(task_id, task, config_data, build_dir, definition["xcode"])
|
||||
|
||||
# 查找 dSYM
|
||||
dsym_path = await find_dsym(task_id, build_dir, config_data.get("SCHEME", ""))
|
||||
@@ -590,16 +631,36 @@ async def run_build_task(task_id: str):
|
||||
db.close()
|
||||
|
||||
|
||||
async def copy_source_code(task_id: str, task, source_dir: Path) -> Path:
|
||||
async def copy_source_code(task_id: str, task, source_dir: Path, definition: dict = None) -> Path:
|
||||
"""从分支源码目录拷贝代码到打包目录"""
|
||||
await log_streamer.emit_step(task_id, "拷贝代码")
|
||||
|
||||
build_dir = BUILD_BASE_DIR / f"build_readoor_{task_id}"
|
||||
project_id = _task_project_id(task)
|
||||
if definition is None:
|
||||
definition = get_definition(project_id, {})
|
||||
build_dir = BUILD_BASE_DIR / f"build_{project_id}_{task_id}"
|
||||
if build_dir.exists():
|
||||
shutil.rmtree(build_dir)
|
||||
build_dir.mkdir(parents=True)
|
||||
|
||||
for item in COPY_ITEMS:
|
||||
# 未指定拷贝条目时拷贝整个仓库(不含 Git 数据和编译缓存)
|
||||
copy_items = definition.get("copy_items") or []
|
||||
if not copy_items:
|
||||
await log_streamer.emit(task_id, "拷贝整个仓库")
|
||||
root = str(source_dir)
|
||||
|
||||
def _ignore(directory, names):
|
||||
ignored = {name for name in names if name in (".git", "DerivedData")}
|
||||
# 仓库根目录的 build 与打包产物目录同名,不拷贝
|
||||
if directory == root and "build" in names:
|
||||
ignored.add("build")
|
||||
return ignored
|
||||
|
||||
shutil.copytree(source_dir, build_dir, symlinks=True, ignore=_ignore)
|
||||
await log_streamer.emit(task_id, "代码拷贝完成")
|
||||
return build_dir
|
||||
|
||||
build_dir.mkdir(parents=True)
|
||||
for item in copy_items:
|
||||
src = source_dir / item
|
||||
# 历史分支中该文件可能仍是小写 podfile;构建副本统一使用 Pods.xcodeproj
|
||||
# 引用的标准名称 Podfile,避免大小写敏感文件系统报路径不一致。
|
||||
@@ -626,7 +687,8 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
|
||||
is_release_track, next_build_no, _ensure_track,
|
||||
)
|
||||
|
||||
config = load_config()
|
||||
project_id = _task_project_id(task)
|
||||
config = load_config(project_id)
|
||||
apps = config.get("apps", {})
|
||||
schemes = config.get("schemes", {})
|
||||
|
||||
@@ -652,7 +714,7 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
|
||||
# feature 轨始终使用手动设定的构建号。
|
||||
if is_release and task.build_type == "App_Store":
|
||||
async with _config_lock:
|
||||
config = load_config()
|
||||
config = load_config(project_id)
|
||||
track = _ensure_track(config, track_name)
|
||||
track.setdefault("app_ver", app_ver)
|
||||
n = next_build_no(config["versions"]["tracks"], app_ver)
|
||||
@@ -666,6 +728,7 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
|
||||
|
||||
# 构建配置
|
||||
config_data = {
|
||||
"PROJECT_ID": project_id,
|
||||
"VERSION": app_ver,
|
||||
"BUILD_VERSION": build_ver,
|
||||
"SERVER": app.get("server", ""),
|
||||
@@ -739,6 +802,16 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
|
||||
if key in app:
|
||||
config_data[key] = app[key]
|
||||
|
||||
# 项目自定义字段,供通用替换规则以 ${app.X} / ${server.X} 引用
|
||||
definition = get_definition(project_id, config)
|
||||
server = config.get("servers", {}).get(app.get("server", ""), {})
|
||||
config_data["APP_VARS"] = {
|
||||
field["key"]: app.get(field["key"], "") for field in definition["app_fields"] if field.get("key")
|
||||
}
|
||||
config_data["SERVER_VARS"] = {
|
||||
field["key"]: server.get(field["key"], "") for field in definition["server_fields"] if field.get("key")
|
||||
}
|
||||
|
||||
# 写入配置文件
|
||||
config_output_path = build_dir / "config_output.json"
|
||||
with open(config_output_path, "w", encoding="utf-8") as f:
|
||||
@@ -748,12 +821,12 @@ async def generate_config(task_id: str, task, build_dir: Path) -> dict:
|
||||
return config_data
|
||||
|
||||
|
||||
async def patch_project(task_id: str, task, config_data: dict, build_dir: Path):
|
||||
async def patch_project(task_id: str, task, config_data: dict, build_dir: Path, definition: dict = None):
|
||||
"""替换项目配置"""
|
||||
await log_streamer.emit_step(task_id, "替换项目配置")
|
||||
|
||||
try:
|
||||
patch_lines = await asyncio.to_thread(apply_project_config, build_dir, config_data)
|
||||
patch_lines = await asyncio.to_thread(apply_project_config, build_dir, config_data, definition)
|
||||
except ProjectPatchError as exc:
|
||||
raise BuildError(str(exc), category="config") from exc
|
||||
(build_dir / "patch_result.log").write_text("\n".join(patch_lines), encoding="utf-8")
|
||||
@@ -766,6 +839,10 @@ async def run_pod_install(task_id: str, build_dir: Path):
|
||||
"""安装 CocoaPods 依赖"""
|
||||
await log_streamer.emit_step(task_id, "安装依赖")
|
||||
|
||||
if not (build_dir / "Podfile").is_file() and not (build_dir / "podfile").is_file():
|
||||
await log_streamer.emit(task_id, "项目未使用 CocoaPods,跳过")
|
||||
return
|
||||
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
"pod", "install",
|
||||
cwd=str(build_dir),
|
||||
@@ -845,14 +922,21 @@ async def run_obfuscation(task_id: str, task, config_data: dict, build_dir: Path
|
||||
await log_streamer.emit(task_id, "代码混淆完成")
|
||||
|
||||
|
||||
async def build_project(task_id: str, task, config_data: dict, build_dir: Path) -> Path:
|
||||
"""构建项目"""
|
||||
async def build_project(task_id: str, task, config_data: dict, build_dir: Path, xcode: dict = None) -> Path:
|
||||
"""构建项目;xcode 为项目定义中的 {workspace, project},优先使用 workspace"""
|
||||
await log_streamer.emit_step(task_id, "构建项目")
|
||||
|
||||
scheme = config_data.get("SCHEME", "")
|
||||
export_path = build_dir / "build"
|
||||
archive_path = export_path / f"{scheme}.xcarchive"
|
||||
workspace_path = build_dir / "readoor.xcworkspace"
|
||||
if xcode is None:
|
||||
xcode = get_definition(LEGACY_PROJECT_ID, {})["xcode"]
|
||||
if xcode.get("workspace"):
|
||||
container_args = ("-workspace", xcode["workspace"])
|
||||
elif xcode.get("project"):
|
||||
container_args = ("-project", xcode["project"])
|
||||
else:
|
||||
raise BuildError("项目未配置 Xcode workspace 或 project", category="config")
|
||||
|
||||
export_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -860,7 +944,7 @@ async def build_project(task_id: str, task, config_data: dict, build_dir: Path)
|
||||
await log_streamer.emit(task_id, "清理项目...")
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
"xcodebuild", "clean",
|
||||
"-workspace", workspace_path.name,
|
||||
*container_args,
|
||||
"-scheme", scheme,
|
||||
"-configuration", "Release",
|
||||
"-derivedDataPath", str(export_path / "derived_data"),
|
||||
@@ -879,7 +963,7 @@ async def build_project(task_id: str, task, config_data: dict, build_dir: Path)
|
||||
archive_output = []
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
"xcodebuild", "archive",
|
||||
"-workspace", workspace_path.name,
|
||||
*container_args,
|
||||
"-scheme", scheme,
|
||||
"-configuration", "Release",
|
||||
"-archivePath", str(archive_path),
|
||||
@@ -957,9 +1041,8 @@ async def upload_ipa(task_id: str, task, config_data: dict, ipa_path: Path, buil
|
||||
await log_streamer.emit_step(task_id, "上传分发平台")
|
||||
|
||||
# 从当前服务的 config.json 读取上传配置,不执行分支源码中的上传脚本。
|
||||
from ..routers.config import load_config
|
||||
full_config = load_config()
|
||||
config_data["_upload_config"] = full_config.get("upload", {})
|
||||
from ..routers.config import load_upload_config
|
||||
config_data["_upload_config"] = load_upload_config()
|
||||
try:
|
||||
await log_streamer.emit(task_id, f"使用 {config_data['_upload_config'].get('mode', '')} 发布 IPA...")
|
||||
return await asyncio.to_thread(publish_ipa, config_data, ipa_path, build_dir)
|
||||
|
||||
Reference in New Issue
Block a user