fix: 优化远程部署构建缓存清理
This commit is contained in:
+66
-10
@@ -189,37 +189,69 @@ def ask_config() -> DeployConfig:
|
||||
)
|
||||
|
||||
|
||||
def run_cmd(args: list[str], cwd: Path | None = None, check: bool = True) -> int:
|
||||
log("$ " + " ".join(shlex.quote(str(item)) for item in args))
|
||||
def resolve_cmd(args: list[str]) -> list[str]:
|
||||
resolved_args = args[:]
|
||||
executable = shutil.which(resolved_args[0])
|
||||
if executable:
|
||||
resolved_args[0] = executable
|
||||
return resolved_args
|
||||
|
||||
|
||||
def run_cmd(args: list[str], cwd: Path | None = None, check: bool = True) -> int:
|
||||
log("$ " + " ".join(shlex.quote(str(item)) for item in args))
|
||||
resolved_args = resolve_cmd(args)
|
||||
result = subprocess.run(resolved_args, cwd=str(cwd or ROOT), check=check)
|
||||
return result.returncode
|
||||
|
||||
|
||||
def docker_build_api_image(api_image: str) -> None:
|
||||
def cmd_success(args: list[str], cwd: Path | None = None) -> bool:
|
||||
result = subprocess.run(
|
||||
resolve_cmd(args),
|
||||
cwd=str(cwd or ROOT),
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=False,
|
||||
)
|
||||
return result.returncode == 0
|
||||
|
||||
|
||||
def docker_image_exists(image: str) -> bool:
|
||||
return cmd_success(["docker", "image", "inspect", image])
|
||||
|
||||
|
||||
def build_api_image_with_base(api_image: str, node_image: str, check: bool = True) -> int:
|
||||
base_cmd = [
|
||||
"docker",
|
||||
"build",
|
||||
"--platform",
|
||||
"linux/amd64",
|
||||
"--build-arg",
|
||||
f"NODE_IMAGE={NODE_IMAGE}",
|
||||
f"NODE_IMAGE={node_image}",
|
||||
"-t",
|
||||
api_image,
|
||||
"-f",
|
||||
"docker/Dockerfile.api",
|
||||
".",
|
||||
]
|
||||
if run_cmd(base_cmd, check=False) == 0:
|
||||
return run_cmd(base_cmd, check=check)
|
||||
|
||||
|
||||
def docker_build_api_image(api_image: str) -> None:
|
||||
if docker_image_exists(NODE_IMAGE):
|
||||
log(f"检测到本地已有 {NODE_IMAGE},直接用于 API 镜像构建。")
|
||||
build_api_image_with_base(api_image, NODE_IMAGE)
|
||||
return
|
||||
|
||||
if docker_image_exists(NODE_1PANEL_IMAGE):
|
||||
log(f"检测到本地已有 {NODE_1PANEL_IMAGE},直接用于 API 镜像构建。")
|
||||
build_api_image_with_base(api_image, NODE_1PANEL_IMAGE)
|
||||
return
|
||||
|
||||
if build_api_image_with_base(api_image, NODE_IMAGE, check=False) == 0:
|
||||
return
|
||||
|
||||
log("Docker 官方 node:20-slim 拉取/构建失败,切换 1Panel 镜像代理后重试。")
|
||||
fallback_cmd = base_cmd[:]
|
||||
fallback_cmd[fallback_cmd.index(f"NODE_IMAGE={NODE_IMAGE}")] = f"NODE_IMAGE={NODE_1PANEL_IMAGE}"
|
||||
run_cmd(fallback_cmd)
|
||||
build_api_image_with_base(api_image, NODE_1PANEL_IMAGE)
|
||||
|
||||
|
||||
def docker_pull_with_fallback(image: str) -> None:
|
||||
@@ -239,6 +271,22 @@ def remove_dir(path: Path) -> None:
|
||||
shutil.rmtree(path)
|
||||
|
||||
|
||||
def cleanup_deploy_cache(*keep: Path) -> None:
|
||||
if not DEPLOY_ROOT.exists():
|
||||
return
|
||||
keep_set = {item.resolve() for item in keep}
|
||||
for path in DEPLOY_ROOT.glob("artveris-remote-*"):
|
||||
try:
|
||||
if path.resolve() in keep_set:
|
||||
continue
|
||||
if path.is_dir():
|
||||
shutil.rmtree(path)
|
||||
else:
|
||||
path.unlink()
|
||||
except OSError as exc:
|
||||
log(f"清理本地部署缓存失败,已跳过:{path}({exc})")
|
||||
|
||||
|
||||
def copy_tree(src: Path, dst: Path) -> None:
|
||||
remove_dir(dst)
|
||||
shutil.copytree(src, dst)
|
||||
@@ -687,11 +735,17 @@ def remote_connection_check_command(remote_dir: str) -> str:
|
||||
|
||||
|
||||
def remote_mysql_init_command(remote_dir: str) -> str:
|
||||
script = (
|
||||
'until MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysqladmin ping '
|
||||
'-h127.0.0.1 -uroot --silent; do sleep 2; done; '
|
||||
'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql '
|
||||
'-h127.0.0.1 -uroot --default-character-set=utf8mb4 < /artveris-init.sql'
|
||||
)
|
||||
return (
|
||||
f"cd {shlex.quote(remote_dir)} && "
|
||||
"docker compose --env-file .env -f docker-compose.remote.yml "
|
||||
"exec -T mysql sh -c "
|
||||
+ shlex.quote('MYSQL_PWD="$MYSQL_ROOT_PASSWORD" mysql -uroot --default-character-set=utf8mb4 < /artveris-init.sql')
|
||||
+ shlex.quote(script)
|
||||
)
|
||||
|
||||
|
||||
@@ -761,8 +815,10 @@ def main() -> None:
|
||||
log("当前脚本按 Windows 本机构建流程设计;Linux/macOS 也可尝试运行,但未作为主要路径。")
|
||||
cfg = ask_config()
|
||||
DEPLOY_ROOT.mkdir(exist_ok=True)
|
||||
_, archive_path, api_image = build_release(cfg)
|
||||
cleanup_deploy_cache()
|
||||
bundle_dir, archive_path, api_image = build_release(cfg)
|
||||
deploy_remote(cfg, archive_path, api_image)
|
||||
cleanup_deploy_cache(bundle_dir, archive_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user