From 9465c98859552df00d94f975cce6b2369ce7e122 Mon Sep 17 00:00:00 2001 From: shenlei Date: Thu, 8 Oct 2026 15:28:48 +0900 Subject: [PATCH] feat: configure WebDAV artifact destination path --- backend/routers/config.py | 2 +- backend/routers/tasks.py | 2 +- backend/services/distribution.py | 61 +++++++++++++++++------ frontend/src/views/ConfigView.vue | 3 +- outputs/WebDAV部署说明.md | 49 +++++++++++++++++++ outputs/nginx-webdav.conf.example | 49 +++++++++++++++++++ tests/test_distribution.py | 81 +++++++++++++++++++++++++++++++ 7 files changed, 228 insertions(+), 19 deletions(-) create mode 100644 outputs/WebDAV部署说明.md create mode 100644 outputs/nginx-webdav.conf.example diff --git a/backend/routers/config.py b/backend/routers/config.py index f3a66fb..05ed9d1 100644 --- a/backend/routers/config.py +++ b/backend/routers/config.py @@ -51,7 +51,7 @@ DEFAULT_UPLOAD = { "server_url": "", "username": "", "password": "", - "base_path": "/ios-builds", + "base_path": "/", "public_url": "", }, "dingtalk": { diff --git a/backend/routers/tasks.py b/backend/routers/tasks.py index 08fcd5f..dbf562d 100644 --- a/backend/routers/tasks.py +++ b/backend/routers/tasks.py @@ -225,7 +225,7 @@ async def delete_task(task_id: str, request: Request, user: dict = Depends(get_c build_config = json.loads(task.config_json) from .config import load_config from ..services.distribution import DistributionError, delete_published_dsym - delete_published_dsym(build_config, load_config().get("upload", {})) + delete_published_dsym(build_config, load_config().get("upload", {}), task.dsym_url) except (json.JSONDecodeError, DistributionError) as exc: raise HTTPException(status_code=502, detail=f"远端 dSYM 删除失败,记录未删除:{exc}") from exc diff --git a/backend/services/distribution.py b/backend/services/distribution.py index a7fe30d..9d02bc4 100644 --- a/backend/services/distribution.py +++ b/backend/services/distribution.py @@ -216,12 +216,10 @@ def _delete_webdav(config: dict, remote_paths: list[str]): if not server_url or not webdav.get("username"): raise DistributionError("WebDAV 配置不完整") - base_path = webdav.get("base_path", "/ios-builds").strip("/") auth = (webdav.get("username", ""), webdav.get("password", "")) with httpx.Client(auth=auth, timeout=120, follow_redirects=True) as client: for relative_path in remote_paths: - remote_path = posixpath.join(base_path, relative_path) - response = client.delete(f"{server_url}/{remote_path}") + response = client.delete(f"{server_url}/{quote(relative_path, safe='/')}") # 404 代表文件已不存在,可视为清理完成。 if response.status_code not in (200, 202, 204, 404): raise DistributionError(f"WebDAV 删除失败: {relative_path} ({response.status_code})") @@ -258,8 +256,14 @@ def _remote_paths_from_published_url( return None remote_path = unquote(parsed.path).lstrip("/") - # base_url 允许带路径前缀;该前缀是公开地址的一部分,不属于 OSS 对象键。 - base_url = upload_config.get("oss", {}).get("base_url", "") + if any(segment == ".." for segment in remote_path.split("/")): + return None + # 公开地址允许带路径前缀;该前缀是下载路由的一部分,不属于远端文件路径。 + mode = upload_config.get("mode") + if mode == "webdav": + base_url = upload_config.get("webdav", {}).get("public_url", "") + else: + base_url = upload_config.get("oss", {}).get("base_url", "") base = urlparse(base_url) if base_url and base.netloc == parsed.netloc: base_path = unquote(base.path).strip("/") @@ -267,6 +271,8 @@ def _remote_paths_from_published_url( remote_path = remote_path[len(base_path) + 1:] path = Path(remote_path) + if path.suffix == ".zip": + return [str(path)] if path.suffix not in {".ipa", ".plist", ".html", ".png"}: return None if build_config.get("BUILD_TYPE") == "App_Store": @@ -287,21 +293,25 @@ def delete_published_artifacts( raise DistributionError("请选择 OSS 或 WebDAV 上传方式") remote_paths = None - if mode == "oss" and published_url: + if published_url: remote_paths = _remote_paths_from_published_url( build_config, upload_config, published_url, ) if not remote_paths: ipa_remote, manifest_remote, html_remote, qr_remote = _remote_paths(build_config) - remote_paths = [ipa_remote] + paths = [ipa_remote] if build_config.get("BUILD_TYPE") != "App_Store": - remote_paths.extend([manifest_remote, html_remote, qr_remote]) + paths.extend([manifest_remote, html_remote, qr_remote]) + if mode == "webdav": + remote_paths = [_webdav_remote_path(upload_config, Path(path).name) for path in paths] + else: + remote_paths = paths deleter = _delete_oss if mode == "oss" else _delete_webdav deleter(upload_config, remote_paths) -def delete_published_dsym(build_config: dict, upload_config: dict): +def delete_published_dsym(build_config: dict, upload_config: dict, published_url: str = ""): """删除任务对应的远端 dSYM 压缩包。 dSYM 的远端对象键完全由构建配置(APPID/版本/分支/构建类型)决定, @@ -311,8 +321,15 @@ def delete_published_dsym(build_config: dict, upload_config: dict): if mode not in {"oss", "webdav"}: raise DistributionError("请选择 OSS 或 WebDAV 上传方式") + remote_paths = _remote_paths_from_published_url(build_config, upload_config, published_url) if published_url else None + if not remote_paths: + remote_path = _dsym_remote_path(build_config) + if mode == "webdav": + remote_path = _webdav_remote_path(upload_config, Path(remote_path).name) + remote_paths = [remote_path] + deleter = _delete_oss if mode == "oss" else _delete_webdav - deleter(upload_config, [_dsym_remote_path(build_config)]) + deleter(upload_config, remote_paths) def _sign_oss_url(upload_config: dict, remote_path: str) -> str: @@ -358,11 +375,23 @@ def _ensure_webdav_dirs(client: httpx.Client, server_url: str, remote_path: str) path = "" for segment in remote_path.strip("/").split("/")[:-1]: path = f"{path}/{segment}" - response = client.request("MKCOL", f"{server_url.rstrip('/')}{path}") + response = client.request("MKCOL", f"{server_url.rstrip('/')}{path}/") if response.status_code not in (200, 201, 204, 301, 405): raise DistributionError(f"创建 WebDAV 目录失败: {path} ({response.status_code})") +def _webdav_remote_path(upload_config: dict, filename: str) -> str: + """Return the exact WebDAV destination path configured in the admin UI.""" + webdav = upload_config.get("webdav", {}) + base_path = str(webdav.get("base_path") or "/").strip("/") + if any(segment in {".", ".."} for segment in base_path.split("/")): + raise DistributionError("WebDAV 远程路径不能包含 . 或 ..") + filename = Path(filename).name + if not filename or filename in {".", ".."}: + raise DistributionError("WebDAV 文件名无效") + return posixpath.join(base_path, filename) if base_path else filename + + def _upload_webdav(config: dict, files: list[tuple[Path, str]]) -> dict[str, str]: webdav = config.get("webdav", {}) server_url = webdav.get("server_url", "").rstrip("/") @@ -370,18 +399,18 @@ def _upload_webdav(config: dict, files: list[tuple[Path, str]]) -> dict[str, str if not server_url or not webdav.get("username"): raise DistributionError("WebDAV 配置不完整") - base_path = webdav.get("base_path", "/ios-builds").strip("/") auth = (webdav.get("username", ""), webdav.get("password", "")) urls = {} with httpx.Client(auth=auth, timeout=120, follow_redirects=True) as client: - for local_path, relative_path in files: - remote_path = posixpath.join(base_path, relative_path) + for local_path, _relative_path in files: + # WebDAV 的远程路径由后台配置;不沿用 OSS 的 ios-builds/iOS 对象键。 + remote_path = _webdav_remote_path(config, local_path.name) _ensure_webdav_dirs(client, server_url, remote_path) with local_path.open("rb") as f: - response = client.put(f"{server_url}/{remote_path}", content=f.read()) + response = client.put(f"{server_url}/{quote(remote_path, safe='/')}", content=f.read()) if response.status_code not in (200, 201, 204): raise DistributionError(f"WebDAV 上传失败: {local_path.name} ({response.status_code})") - urls[local_path.suffix] = f"{public_url}/{quote(remote_path)}" + urls[local_path.suffix] = f"{public_url}/{quote(remote_path, safe='/')}" return urls diff --git a/frontend/src/views/ConfigView.vue b/frontend/src/views/ConfigView.vue index f141b0f..c1db202 100644 --- a/frontend/src/views/ConfigView.vue +++ b/frontend/src/views/ConfigView.vue @@ -247,7 +247,8 @@
- + +
文件直接上传到此目录,不会自动追加 ios-builds/iOS;填 / 表示下载根目录
diff --git a/outputs/WebDAV部署说明.md b/outputs/WebDAV部署说明.md new file mode 100644 index 0000000..d19fe2b --- /dev/null +++ b/outputs/WebDAV部署说明.md @@ -0,0 +1,49 @@ +# 打包产物 WebDAV 部署说明 + +文件服务器上的下载目录为 `/home/appmgr/app-downloads`,已创建 `Android` 和 `iOS` 子目录。Nginx DAV 模块已编译启用;`httpd-tools` 尚未安装。当前 SSH 用户 `appmgr` 不在 sudoers 中,安装软件和创建密码文件需使用 root 或有 sudo 权限的账号。 + +## 安装并创建 WebDAV 账号 + +在文件服务器执行: + +```bash +dnf install -y httpd-tools +htpasswd -c /etc/nginx/app-downloads.htpasswd build-uploader +``` + +`htpasswd` 会交互式询问密码。`build-uploader` 是 WebDAV 账号,不是 Linux 登录账号。`-c` 仅在首次创建密码文件时使用;后续添加账号时去掉 `-c`,以免覆盖已有账号。 + +## Nginx 配置 + +将 `nginx-webdav.conf.example` 保存为 `/etc/nginx/conf.d/app-downloads.conf`。示例按现有文件服务器内网地址 `172.1.110.13` 和 iOS 打包服务器来源 `172.1.110.180` 编写。把 Android 打包服务器访问时的来源 IP 也加入 `allow`。外层 Nginx 的下载域名应反代到 `http://172.1.110.13:8081`,并保留原始 URI;防火墙应只允许外层 Nginx 访问 8081。 + +检查并重载: + +```bash +nginx -t +systemctl reload nginx +``` + +内网 WebDAV 使用 HTTP,Basic Auth 凭据会在内网链路上传输。8080 应通过防火墙限制为打包服务器来源地址。请确认 `172.1.110.13` 和构建服务器地址确实只在受控网络内路由;172 私有网段是 `172.16.0.0/12`。 + +## 打包服务后台配置 + +在 iOS 打包服务「上传配置」页选择 WebDAV: + +| 字段 | 值 | +| --- | --- | +| 服务器地址 | `http://172.1.110.13:8080` | +| 用户名 | `build-uploader` | +| 密码 | 创建账号时输入的密码 | +| 远程路径 | `/iOS` | +| 公开访问地址 | `https://你的外网下载域名` | + +当前 WebDAV 实现将文件直接放在配置的远程目录中,只使用产物文件名,不再附加 `ios-builds/iOS`。因此 `/iOS` 对应文件系统 `/home/appmgr/app-downloads/iOS`,公开链接为 `https://你的外网下载域名/iOS/文件名`。外层 Nginx 必须保留该 URI 并代理到 8081。 + +Android 打包服务应将 APK 上传到 `/Android`,公开链接形如 `https://你的外网下载域名/Android/文件名.apk`。Android 的上传和删除配置由对应的 Android 打包服务负责;当前 iOS 项目发布 IPA、plist、安装页和二维码。 + +## 验证 + +分别从打包服务器发起一次测试上传,使用外网 HTTPS 域名打开生成的链接,再从对应前台删除测试构建并确认文件已删除。Ad Hoc iOS 安装还需要有效的 HTTPS 下载域名、manifest、签名和设备 UDID。 + +参考:[Nginx DAV 模块文档](https://nginx.org/en/docs/http/ngx_http_dav_module.html) diff --git a/outputs/nginx-webdav.conf.example b/outputs/nginx-webdav.conf.example new file mode 100644 index 0000000..82a9e73 --- /dev/null +++ b/outputs/nginx-webdav.conf.example @@ -0,0 +1,49 @@ +# 保存为 /etc/nginx/conf.d/app-downloads.conf。 +# 文件服务器内网地址:172.1.110.13 +# 请将 Android 打包服务器来源 IP 也加入 WebDAV allow 列表。 + +# 内网 WebDAV:构建服务器通过 PUT 上传、DELETE 删除。 +server { + listen 172.1.110.13:8080; + server_name _; + + root /home/appmgr/app-downloads; + client_max_body_size 5g; + client_body_timeout 300s; + + auth_basic "Build Artifacts"; + auth_basic_user_file /etc/nginx/app-downloads.htpasswd; + + # iOS 打包服务器;将 Android 打包服务器来源 IP 也加入 allow 列表。 + allow 172.1.110.180; + deny all; + + location / { + dav_methods PUT DELETE MKCOL; + create_full_put_path on; + dav_access user:rw group:r; + autoindex off; + + # 兼容当前 iOS 打包服务 MKCOL 请求尾部斜杠。 + if ($request_method = MKCOL) { + rewrite ^(.*[^/])$ $1/ break; + } + } +} + +# 内层静态文件服务:由外层 Nginx 反向代理;不要对公网开放 8081。 +server { + listen 172.1.110.13:8081; + server_name _; + + root /home/appmgr/app-downloads; + autoindex off; + + location / { + dav_methods off; + limit_except GET HEAD { + deny all; + } + try_files $uri =404; + } +} diff --git a/tests/test_distribution.py b/tests/test_distribution.py index 37089d3..bd5dcbb 100644 --- a/tests/test_distribution.py +++ b/tests/test_distribution.py @@ -4,6 +4,7 @@ from unittest.mock import patch from backend.services.distribution import ( _artifact_stem, + _upload_webdav, delete_published_artifacts, _write_distribution_files, _write_download_page, @@ -122,6 +123,86 @@ def test_delete_uses_saved_url_for_legacy_artifact_name(): ] +def test_webdav_upload_uses_configured_directory_without_oss_path(tmp_path, monkeypatch): + ipa = tmp_path / "build.ipa" + ipa.write_bytes(b"ipa") + calls = {"mkcol": [], "put": []} + + class FakeResponse: + status_code = 201 + + class FakeClient: + def __init__(self, **_kwargs): pass + def __enter__(self): return self + def __exit__(self, *_args): pass + def request(self, method, url): + calls["mkcol"].append((method, url)) + return FakeResponse() + def put(self, url, content): + calls["put"].append((url, content)) + return FakeResponse() + + monkeypatch.setattr("backend.services.distribution.httpx.Client", FakeClient) + result = _upload_webdav( + { + "webdav": { + "server_url": "http://dav.example.com", + "username": "u", + "password": "p", + "base_path": "/iOS/", + "public_url": "https://download.example.com", + }, + }, + [(ipa, "ios-builds/iOS/legacy-path.ipa")], + ) + + assert calls["mkcol"] == [("MKCOL", "http://dav.example.com/iOS/")] + assert calls["put"] == [("http://dav.example.com/iOS/build.ipa", b"ipa")] + assert result[".ipa"] == "https://download.example.com/iOS/build.ipa" + + +def test_delete_webdav_artifacts_uses_path_from_published_url(): + config = { + "APPID": "100", "VERSION": "2.0.0.0", "SOURCE_BRANCH": "main", + "BUILD_TYPE": "Ad_Hoc", "OSS_FLODER": "legacy-oss-folder", + } + upload = { + "mode": "webdav", + "webdav": { + "base_path": "/Android", + "public_url": "https://download.example.com", + }, + } + with patch("backend.services.distribution._delete_webdav") as delete: + delete_published_artifacts( + config, upload, + "https://download.example.com/Android/build.html", + ) + + assert delete.call_args.args[1] == [ + "Android/build.ipa", + "Android/build.plist", + "Android/build.html", + "Android/build.png", + ] + + +def test_delete_webdav_dsym_uses_published_url(): + config = {"APPID": "100", "VERSION": "2.0.0.0", "SOURCE_BRANCH": "main"} + upload = { + "mode": "webdav", + "webdav": { + "base_path": "/iOS", + "public_url": "https://download.example.com", + }, + } + with patch("backend.services.distribution._delete_webdav") as delete: + from backend.services.distribution import delete_published_dsym + delete_published_dsym(config, upload, "https://download.example.com/iOS/build.dSYM.zip") + + assert delete.call_args.args[1] == ["iOS/build.dSYM.zip"] + + def test_oss_download_url_is_short_lived_and_uses_actual_object_key(monkeypatch): class FakeBucket: def __init__(self, *_args, **_kwargs): pass