feat: 添加远程部署重置选项

This commit is contained in:
Nixevol
2026-07-05 18:31:04 +08:00
parent c132f32ef7
commit d0c78701c4
3 changed files with 38 additions and 0 deletions
+1
View File
@@ -73,3 +73,4 @@ HTTPS 与域名继续由 1Panel 反向代理配置,例如:
部署包会临时生成在 `.deploy/`,其中包含生产 `.env` 和镜像归档,已被 `.gitignore` 忽略,不能提交到仓库。
脚本启动时会清理 `.deploy/artveris-remote-*` 历史部署包,部署成功后也会删除本次生成的部署目录和压缩包,避免本机磁盘长期积压。
生产密码会保存到 `.deploy/remote-secrets.json` 并在下次部署时作为默认值复用,避免 MySQL/Redis 数据卷已存在时因为脚本重新生成随机密码而无法连接。该文件位于已忽略的 `.deploy/` 下,不要手动提交。
脚本会询问是否清理线上 Artveris 旧容器/卷/网络/API 镜像,默认 `N`;只有手动输入 `y` 才会删除 `artveris` Compose 项目资源和 `artveris-api:*` 镜像。该操作会清空 Artveris 自己的 MySQL/Redis/上传文件/备份卷,开发阶段重置环境可用,正式数据环境慎用。
+36
View File
@@ -65,6 +65,7 @@ class DeployConfig:
oss_access_key_secret: str
cdn_base_url: str
include_base_images: bool
clean_remote_artveris: bool
ssh_host: str
ssh_port: int
ssh_username: str
@@ -176,6 +177,10 @@ def ask_config() -> DeployConfig:
"是否同时打包 MySQL/Redis/Nginx 基础镜像(服务器拉取 Docker Hub 不稳定时可开启)",
False,
)
clean_remote_artveris = ask_yes_no(
"是否清理线上 Artveris 旧容器/卷/网络/API镜像后重新部署(会删除 Artveris 数据)",
False,
)
log("请填写 SSH 信息,用于上传并启动远程 Docker。")
ssh_host = ask("服务器 SSH IP/域名")
@@ -217,6 +222,7 @@ def ask_config() -> DeployConfig:
oss_access_key_secret=oss_access_key_secret,
cdn_base_url=cdn_base_url,
include_base_images=include_base_images,
clean_remote_artveris=clean_remote_artveris,
ssh_host=ssh_host,
ssh_port=ssh_port,
ssh_username=ssh_username,
@@ -789,6 +795,33 @@ def remote_mysql_init_command(remote_dir: str) -> str:
)
def remote_clean_artveris_command(remote_dir: str) -> str:
script = r"""
set -eu
if [ -f docker-compose.remote.yml ]; then
docker compose --env-file .env -f docker-compose.remote.yml down -v --remove-orphans || true
fi
containers="$(docker ps -aq --filter label=com.docker.compose.project=artveris || true)"
if [ -n "$containers" ]; then
docker rm -f $containers || true
fi
volumes="$(docker volume ls -q --filter label=com.docker.compose.project=artveris || true)"
if [ -n "$volumes" ]; then
docker volume rm -f $volumes || true
fi
networks="$(docker network ls -q --filter label=com.docker.compose.project=artveris || true)"
if [ -n "$networks" ]; then
docker network rm $networks || true
fi
images="$(docker images --format '{{.Repository}}:{{.Tag}}' | grep '^artveris-api:' || true)"
if [ -n "$images" ]; then
docker rmi -f $images || true
fi
rm -rf admin-dist client-dist images docker-compose.remote.yml nginx.conf mysql-init.sql .env manifest.json
"""
return f"cd {shlex.quote(remote_dir)} && sh -c {shlex.quote(script)}"
def deploy_remote(cfg: DeployConfig, archive_path: Path, api_image: str) -> None:
ssh = ssh_connect(cfg)
remote_archive = f"/tmp/{archive_path.name}"
@@ -798,6 +831,9 @@ def deploy_remote(cfg: DeployConfig, archive_path: Path, api_image: str) -> None
remote_exec(ssh, "command -v docker >/dev/null && docker compose version >/dev/null")
remote_exec(ssh, "uname -m")
remote_exec(ssh, f"mkdir -p {remote_dir_q}")
if cfg.clean_remote_artveris:
log("按用户选择清理线上 Artveris 旧容器/卷/网络/API镜像。")
remote_exec(ssh, remote_clean_artveris_command(cfg.remote_dir))
log(f"上传部署包到 {remote_archive}")
upload_file(ssh, archive_path, remote_archive)
remote_exec(