feat: enhance Spug deployment scripts with better error handling and auto-fix

This commit is contained in:
2026-02-28 16:01:59 +08:00
parent 13badac2dc
commit 5b45bf86a2
4 changed files with 511 additions and 13 deletions

View File

@@ -19,15 +19,25 @@ class SpugDeploy:
def __init__(self):
# 配置参数(从环境变量获取,可在 Spug 中设置)
# 处理 SPUG_DEPLOY_DIR 为空的情况
deploy_dir = os.getenv('SPUG_DEPLOY_DIR', '').strip()
if not deploy_dir:
deploy_dir = '/var/www/hospital-performance'
self.project_name = os.getenv('SPUG_APP_NAME', 'hospital-performance')
self.project_dir = Path(os.getenv('SPUG_DEPLOY_DIR', '/var/www/hospital-performance'))
self.project_dir = Path(deploy_dir)
self.backup_dir = self.project_dir / 'backups'
self.frontend_dir = self.project_dir / 'frontend'
self.backend_dir = self.project_dir / 'backend'
# Git 配置
self.git_repo = os.getenv('SPUG_GIT_REPO',
'https://gitea.gentronhealth.com/chenqi/hospital_performance.git')
# Git 配置(优先使用 Spug 的环境变量)
git_repo = os.getenv('SPUG_GIT_URL', '').strip()
if not git_repo:
git_repo = os.getenv('SPUG_GIT_REPO', '').strip()
if not git_repo:
git_repo = 'https://gitea.gentronhealth.com/chenqi/hospital_performance.git'
self.git_repo = git_repo
self.git_branch = os.getenv('SPUG_GIT_BRANCH', 'main')
# Python 配置
@@ -110,6 +120,11 @@ class SpugDeploy:
self.error(f"命令 {cmd} 未安装,请先安装")
sys.exit(1)
# 创建部署目录(如果不存在)
if not self.project_dir.exists():
self.info(f"创建部署目录:{self.project_dir}")
self.project_dir.mkdir(parents=True, exist_ok=True)
# 检查磁盘空间
try:
stat = shutil.disk_usage(self.project_dir)
@@ -131,6 +146,21 @@ class SpugDeploy:
git_dir = self.project_dir / '.git'
if not git_dir.exists():
self.info("首次部署,克隆仓库...")
# 检查目录是否为空
if any(self.project_dir.iterdir()):
self.info("目录非空,先备份现有文件...")
backup_non_git = self.project_dir / f"backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}_non_git"
backup_non_git.mkdir(exist_ok=True)
# 移动非 .git 相关文件
for item in self.project_dir.iterdir():
if item.name not in ['.git', 'backups', 'venv'] and not item.name.startswith('backup_'):
try:
shutil.move(str(item), str(backup_non_git / item.name))
except Exception as e:
self.info(f"跳过文件 {item.name}: {e}")
self.run_command(['git', 'clone', self.git_repo, '.'])
self.run_command(['git', 'checkout', self.git_branch])
else: