diff --git a/apps/core/deploy.py b/apps/core/deploy.py index 63d66bc..701c453 100644 --- a/apps/core/deploy.py +++ b/apps/core/deploy.py @@ -50,7 +50,7 @@ def _venv_pip(): return [sys.executable, '-m', 'pip'] -def _run_step(log_lines, cmd, timeout=STEP_TIMEOUT): +def _run_step(log_lines, cmd, timeout=STEP_TIMEOUT, env=None): """执行一条命令,将输出追加到 log_lines,返回是否成功。""" if isinstance(cmd, str): display = cmd @@ -59,11 +59,21 @@ def _run_step(log_lines, cmd, timeout=STEP_TIMEOUT): display = ' '.join(str(c) for c in cmd) args = [str(c) for c in cmd] + # 为 git 命令自动加上 safe.directory 配置,避免 "dubious ownership" 报错 + # (Django 通常以 www-data 运行,但项目目录所有者可能是 root) + if args and args[0] == 'git': + args = ['git', '-c', f'safe.directory={BASE_DIR}'] + args[1:] + display = ' '.join(args) + log_lines.append(f'\n$ {display}') try: + run_env = None + if env: + run_env = {**os.environ, **env} result = subprocess.run( args, cwd=str(BASE_DIR), capture_output=True, text=True, timeout=timeout, + env=run_env, ) if result.stdout: log_lines.append(result.stdout.rstrip())