Files
iOSBuildServer/backend/projects.py
T
shenlei 83a1db21e5 feat: 接入 iOSGuard LLVM 混淆工具链与 IPA 加固门禁,按分支策略兼容旧版本打包
- 新增 toolchain_manager:构建前自愈校验 LLVM opt 工具链,按 Xcode 版本与渠道隔离 obj_cache
- readoor31/readoor31OtherPay Archive 透传 IG_DIST_CHANNEL/IG_OBF_MODE/IG_ALL_GUARD_OFF/IG_RELEASE_SEED
- Archive 前核对分发渠道编译条件:上架包缺失 IG_APPSTORE_DISTRIBUTION 即阻断,非上架包带该条件仅告警,未接入 anti_tamper 的分支跳过
- 导出后执行 IPA 加固门禁审计,失败阻断上传;持久化 dSYM、混淆映射、诊断事件码与审计报告
- 混淆是否执行改由分支 AutoPacking/build_policy.json 声明:force_obfuscation=true 强制,否则按任务勾选,兼容旧分支/旧 Tag;恢复打包页混淆勾选框
- 分支未自带混淆脚本时回退服务端脚本并告警;同步更新服务端 L1 混淆脚本
- .gitignore 忽略 iosguard_cache 与 artifacts 运行时目录;README 补充分支打包策略说明
2026-11-24 12:03:54 +09:00

188 lines
8.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""多项目定义:每个项目的源码仓库、Xcode 工程、配置替换方式等。
config.json 结构:
{
"projects": {
"<项目 ID>": {
# —— 项目定义(管理员在「项目管理」维护)——
"name": "显示名称",
"git": {"remote_url": "", "username": "", "password": "", "use_env": false},
"source_dir": "", # 共享源码工作目录,留空为 <服务目录>/sources/<项目 ID>
"xcode": {"workspace": "", "project": "X.xcodeproj"},
"copy_items": [], # 拷贝到打包目录的条目,留空拷贝整个仓库
"patcher": "generic", # readoor(阅门户专用)/ generic(通用 + patch_rules)
"patch_rules": [], # generic 的额外替换规则,见 project_patcher
"app_fields": [], # App 的项目自定义字段 [{key, label, required}]
"server_fields": [], # 服务器环境的项目自定义字段
"obfuscation": false, # 是否支持代码混淆
"dingtalk_at": {"at_mobiles": [], "at_mobiles_app_store": []},
# 本项目钉钉 @ 名单,为空则用 upload.dingtalk 的全局名单
# —— 项目数据(与原单项目时的顶层字段相同)——
"apps": {}, "schemes": {}, "servers": {}, "branches": [], "tags": [],
"versions": {}, "branch_track": {}, "next_app_id_prefix": 1
}
},
"upload": {...} # 上传与钉钉通知,所有项目共用
}
"""
import re
from contextvars import ContextVar
from pathlib import Path
from .config import (
COPY_ITEMS,
GIT_PASSWORD,
GIT_REMOTE_URL,
GIT_SOURCE_BASE,
GIT_SOURCE_DIR,
GIT_USERNAME,
SERVER_ROOT,
inject_git_credentials,
)
# 升级前的单项目配置迁移为这个项目,并沿用 .env 中的 Git 配置。
LEGACY_PROJECT_ID = "readoor"
PATCHER_READOOR = "readoor"
PATCHER_GENERIC = "generic"
PATCHERS = (PATCHER_READOOR, PATCHER_GENERIC)
# 项目定义字段;其余字段(apps、schemes、versions 等)都是项目数据。
DEFINITION_KEYS = (
"name", "git", "source_dir", "xcode", "copy_items", "patcher",
"patch_rules", "app_fields", "server_fields", "obfuscation", "dingtalk_at",
)
# 原单项目(阅门户)的定义,迁移旧配置时写入。
LEGACY_PROJECT_DEFINITION = {
"name": "Readoor",
"git": {"remote_url": "", "username": "", "password": "", "use_env": True},
"source_dir": "",
"xcode": {"workspace": "readoor.xcworkspace", "project": "readoor.xcodeproj"},
"copy_items": list(COPY_ITEMS),
"patcher": PATCHER_READOOR,
"patch_rules": [],
"app_fields": [],
"server_fields": [],
"obfuscation": True,
"dingtalk_at": {"at_mobiles": [], "at_mobiles_app_store": []},
}
# 英汉大词典(readoorDict,TARGETTYPE == 2)的服务器地址:Readoor 项目的服务器环境内置这些字段,
# 打包时写入 STSConstantsMacro.h 中 TARGETTYPE == 2 段落的同名宏(字段名去掉 DICT_ 前缀即宏名)。
READOOR_DICT_SERVER_FIELDS = [
{"key": "DICT_SERVERURL", "label": "词典 SERVERURL", "required": False},
{"key": "DICT_SERVERHTTPSURL", "label": "词典 SERVERHTTPSURL", "required": False},
{"key": "DICT_UCSERVERURL", "label": "词典 UCSERVERURL", "required": False},
{"key": "DICT_RECORDSERVERURL", "label": "词典 RECORDSERVERURL", "required": False},
{"key": "DICT_LOGSERVERURL", "label": "词典 LOGSERVERURL", "required": False},
]
# 当前请求操作的项目,由 main.py 的中间件按请求头 X-Project 设置;
# load_config() 未显式指定项目时使用它,未设置则取第一个项目。
PROJECT_HEADER = "X-Project"
current_project_id: ContextVar = ContextVar("current_project_id", default=None)
_PROJECT_ID_PATTERN = re.compile(r"^[a-z][a-z0-9_-]{0,31}$")
def is_valid_project_id(project_id: str) -> bool:
"""项目 ID 会用于目录名和请求头,只允许小写字母开头的短标识。"""
return bool(_PROJECT_ID_PATTERN.fullmatch(project_id or ""))
def default_definition(project_id: str) -> dict:
"""项目定义缺省值:旧项目沿用原有行为,新项目为通用配置。"""
if project_id == LEGACY_PROJECT_ID:
return LEGACY_PROJECT_DEFINITION
return {
"name": project_id,
"git": {"remote_url": "", "username": "", "password": "", "use_env": False},
"source_dir": "",
"xcode": {"workspace": "", "project": ""},
"copy_items": [],
"patcher": PATCHER_GENERIC,
"patch_rules": [],
"app_fields": [],
"server_fields": [],
"obfuscation": False,
"dingtalk_at": {"at_mobiles": [], "at_mobiles_app_store": []},
}
def get_definition(project_id: str, project: dict) -> dict:
"""取项目定义,缺失字段用缺省值补齐(只读,不写回配置)。"""
defaults = default_definition(project_id)
definition = {key: project.get(key, defaults[key]) for key in DEFINITION_KEYS}
definition["copy_items"] = list(definition.get("copy_items") or [])
if (
(project_id == LEGACY_PROJECT_ID or definition.get("patcher") == PATCHER_READOOR)
and definition["copy_items"]
and "iOSGuardIntegration" not in definition["copy_items"]
):
definition["copy_items"].append("iOSGuardIntegration")
definition["git"] = {**defaults["git"], **(definition.get("git") or {})}
definition["xcode"] = {**defaults["xcode"], **(definition.get("xcode") or {})}
definition["dingtalk_at"] = {**defaults["dingtalk_at"], **(definition.get("dingtalk_at") or {})}
if definition["patcher"] not in PATCHERS:
definition["patcher"] = PATCHER_GENERIC
if definition["patcher"] == PATCHER_READOOR:
keys = {field.get("key") for field in definition["server_fields"]}
definition["server_fields"] = list(definition["server_fields"]) + [
dict(field) for field in READOOR_DICT_SERVER_FIELDS if field["key"] not in keys
]
return definition
def public_definition(project_id: str, project: dict) -> dict:
"""打包页和配置页渲染所需的项目信息,不含 Git 凭据。"""
definition = get_definition(project_id, project)
return {
"id": project_id,
"name": definition["name"],
"patcher": definition["patcher"],
"obfuscation": bool(definition["obfuscation"]),
"app_fields": definition["app_fields"],
"server_fields": definition["server_fields"],
}
class ProjectSource:
"""项目源码的获取方式。"""
def __init__(self, remote_url: str, remote_url_with_credentials: str,
shared_dir: Path, manual_base: Path = None):
self.remote_url = remote_url
self.remote_url_with_credentials = remote_url_with_credentials
# 配置远程仓库时,所有分支共用的源码工作目录
self.shared_dir = shared_dir
# 未配置远程仓库时,按 <manual_base>/<branch> 读取人工维护的分支目录(仅旧项目支持)
self.manual_base = manual_base
def get_project_source(project_id: str, project: dict) -> ProjectSource:
definition = get_definition(project_id, project)
git = definition["git"]
use_env = bool(git.get("use_env"))
remote_url = (git.get("remote_url") or "").strip() or (GIT_REMOTE_URL if use_env else "")
username = git.get("username") or (GIT_USERNAME if use_env else "")
password = git.get("password") or (GIT_PASSWORD if use_env else "")
source_dir = (definition.get("source_dir") or "").strip()
if source_dir:
shared_dir = Path(source_dir).expanduser()
if not shared_dir.is_absolute():
shared_dir = SERVER_ROOT / shared_dir
elif use_env:
shared_dir = GIT_SOURCE_DIR
else:
shared_dir = SERVER_ROOT / "sources" / project_id
return ProjectSource(
remote_url=remote_url,
remote_url_with_credentials=inject_git_credentials(remote_url, username, password),
shared_dir=shared_dir,
manual_base=GIT_SOURCE_BASE if use_env else None,
)