From 9510d7509e3010c52cbb28e8048d5e1eac48a5e0 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 17 Sep 2026 10:52:00 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E6=96=B0=E5=A2=9E=20deploy.py=20?= =?UTF-8?q?=E4=B8=80=E9=94=AE=E9=83=A8=E7=BD=B2=E8=84=9A=E6=9C=AC=EF=BC=88?= =?UTF-8?q?=E5=9B=BA=E5=8C=96=E6=9C=AC=E5=9C=B0push=E2=86=92=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8pull=EF=BC=8C=E6=A0=B9=E6=B2=BBcommit?= =?UTF-8?q?=E4=BA=A4=E5=8F=89=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.py | 234 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 123 insertions(+), 111 deletions(-) diff --git a/deploy.py b/deploy.py index 8ac51f6..ac2bf07 100644 --- a/deploy.py +++ b/deploy.py @@ -1,111 +1,123 @@ -# -*- coding: utf-8 -*- -"""Step C: 一键部署 — 本地 commit+push → 服务器 git pull 同步 - -工作流(根治 git 交叉): - 本地 git add/commit/push → 服务器 git pull --ff-only - - 服务器只做 pull 不做 commit —— pull 只移动引用,不产生新 commit。 - 这是交叉问题的根源修复:旧流程是 SFTP 上传后服务器再 commit 一次, - 同一改动被提交两次,author/committer 时间戳不同 → 同 tree 两个 commit ID - → ahead 1 behind 1。 - -依赖(已配置,非交互): - 本地: credential.helper=store, ~/.git-credentials - 服务器: credential.helper=store, ~/.git-credentials, remote → 127.0.0.1:3000 - (Gitea 与服务器同机,走 loopback 避免外部网络波动) - -用法: python deploy.py "提交信息" - python deploy.py # 用默认信息 -""" -import os -import subprocess -import sys - -ROOT = os.path.dirname(os.path.abspath(__file__)) -os.chdir(ROOT) - -# .gitignore 已排除本机脚本与备份目录,git add -A 会自动跳过,无需额外 pathspec。 -# (pathspec 排除 ':!x.py' 在文件已被 .gitignore 忽略时会报错: -# "The following paths are ignored by one of your .gitignore files") - -DEFAULT_MSG = 'chore: 部署更新' -SSH_BASE = ("ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " - "-o ConnectTimeout=10 ubuntu@106.55.169.92") -REMOTE_DIR = "/var/www/qianshi" -TIMEOUT = 60 - -ENV = dict(os.environ, GIT_TERMINAL_PROMPT='0') # 禁凭据交互,缺凭据立即失败而非挂起 - - -def sh(args, timeout=TIMEOUT): - """跑命令,返回 (exit, stdout)""" - r = subprocess.run(args, capture_output=True, text=True, - timeout=timeout, env=ENV) - return r.returncode, (r.stdout + r.stderr).strip() - - -def log(msg): - print(' %s' % msg, flush=True) - - -def remote(cmd, timeout=TIMEOUT): - """在服务器上执行命令,返回 (exit, stdout)""" - return sh([SSH_BASE, '"cd %s && %s"' % (REMOTE_DIR, cmd)], timeout) - - -def deploy(msg=DEFAULT_MSG): - log('[1/4] 检查改动') - _, st = sh(['git', 'status', '--porcelain']) - if not st: - log('工作区干净,无需提交') - else: - log('[2/4] 提交') - # .gitignore 已排除本机脚本与备份目录,git add -A 无需额外 pathspec - c, out = sh(['git', 'add', '-A']) - if c: - log('add 失败: %s' % out) - return False - _, staged = sh(['git', 'diff', '--cached', '--name-only']) - if not staged: - log('排除项之外无改动') - else: - log(' %d 个文件' % len(staged.splitlines())) - c, out = sh(['git', 'commit', '-m', msg]) - if c: - log('commit 失败: %s' % out) - return False - _, commit = sh(['git', 'rev-parse', '--short', 'HEAD']) - log('本地 HEAD: %s' % commit) - - log('[3/4] push origin main') - c, out = sh(['git', 'push', 'origin', 'main']) - if c: - log('push 失败: %s' % out) - return False - log(' pushed') - - log('[4/4] 服务器 git pull --ff-only') - c, out = remote('GIT_TERMINAL_PROMPT=0 git pull --ff-only origin main') - if c: - log('pull 失败(若报 diverging,说明服务器产生过本地 commit,需人工处理)') - log(out) - return False - log(' %s' % out.splitlines()[-1] if out else ' ok') - - # 校验三端一致 - _, local = sh(['git', 'rev-parse', '--short', 'HEAD']) - _, srv = remote('git rev-parse --short HEAD', 30) - srv = srv.strip().splitlines()[-1] if srv else '?' - if local != srv: - log('!! 本地 %s != 服务器 %s,同步失败' % (local, srv)) - return False - log('') - log('=== 部署完成 ===') - log('提交: %s %s' % (local, msg)) - log('本地/服务器 已同步,工作流无 commit 交叉') - return True - - -if __name__ == '__main__': - msg = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_MSG - sys.exit(0 if deploy(msg) else 1) +# -*- coding: utf-8 -*- +"""Step C: 一键部署 — 本地 commit+push → 服务器 git pull 同步 + +工作流(根治 git 交叉): + 本地 git add/commit/push → 服务器 git pull --ff-only + + 服务器只做 pull 不做 commit —— pull 只移动引用,不产生新 commit。 + 这是交叉问题的根源修复:旧流程是 SFTP 上传后服务器再 commit 一次, + 同一改动被提交两次,author/committer 时间戳不同 → 同 tree 两个 commit ID + → ahead 1 behind 1。 + +依赖(已配置,非交互): + 本地: credential.helper=store, ~/.git-credentials + 服务器: credential.helper=store, ~/.git-credentials, remote → 127.0.0.1:3000 + (Gitea 与服务器同机,走 loopback 避免外部网络波动) + +用法: python deploy.py "提交信息" + python deploy.py # 用默认信息 +""" +import os +import subprocess +import sys + +ROOT = os.path.dirname(os.path.abspath(__file__)) +os.chdir(ROOT) + +# .gitignore 已排除本机脚本与备份目录,git add -A 会自动跳过,无需额外 pathspec。 +# (pathspec 排除 ':!x.py' 在文件已被 .gitignore 忽略时会报错: +# "The following paths are ignored by one of your .gitignore files") + +DEFAULT_MSG = 'chore: 部署更新' +SSH_BASE = ("ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null " + "-o ConnectTimeout=10 ubuntu@106.55.169.92") +REMOTE_DIR = "/var/www/qianshi" +TIMEOUT = 60 + +ENV = dict(os.environ, GIT_TERMINAL_PROMPT='0') # 禁凭据交互,缺凭据立即失败而非挂起 + + +def sh(args, timeout=TIMEOUT): + """跑本地命令,返回 (exit, stdout)。args 为 list,shell=False。 + + Windows 上 shell=True 交给 cmd.exe,引号语义与 POSIX 不同, + 提交信息等参数若用 shlex.quote 反而会出错 —— 统一用 list 直传。 + """ + r = subprocess.run(args, capture_output=True, text=True, + timeout=timeout, env=ENV) + return r.returncode, (r.stdout + r.stderr).strip() + + +def log(msg): + print(' %s' % msg, flush=True) + + +def remote(cmd, timeout=TIMEOUT): + """在服务器上执行命令,返回 (exit, stdout) + + ssh 远程命令必须作为单个 argv 元素传(shell=False), + 否则 Windows 的 cmd.exe 会先解析掉引号,ssh 拿不到完整命令。 + cd 与 cmd 的拼接在 Python 里完成,不走任何 shell 解析。 + """ + args = SSH_BASE.split() + ['cd %s && %s' % (REMOTE_DIR, cmd)] + r = subprocess.run(args, capture_output=True, text=True, + timeout=timeout, env=ENV) + return r.returncode, (r.stdout + r.stderr).strip() + + +def deploy(msg=DEFAULT_MSG): + log('[1/4] 检查改动') + _, st = sh(['git', 'status', '--porcelain']) + if not st: + log('工作区干净,无需提交') + else: + log('[2/4] 提交') + # .gitignore 已排除本机脚本与备份目录,git add -A 无需额外 pathspec + c, out = sh(['git', 'add', '-A']) + if c: + log('add 失败: %s' % out) + return False + _, staged = sh(['git', 'diff', '--cached', '--name-only']) + if not staged: + log('排除项之外无改动') + else: + log(' %d 个文件' % len(staged.splitlines())) + c, out = sh(['git', 'commit', '-m', msg]) + if c: + log('commit 失败: %s' % out) + return False + _, commit = sh(['git', 'rev-parse', '--short', 'HEAD']) + log('本地 HEAD: %s' % commit) + + log('[3/4] push origin main') + c, out = sh(['git', 'push', 'origin', 'main']) + if c: + log('push 失败: %s' % out) + return False + log(' pushed') + + log('[4/4] 服务器 git pull --ff-only') + c, out = remote('GIT_TERMINAL_PROMPT=0 git pull --ff-only origin main') + if c: + log('pull 失败(若报 diverging,说明服务器产生过本地 commit,需人工处理)') + log(out) + return False + log(' %s' % out.splitlines()[-1] if out else ' ok') + + # 校验三端一致 + _, local = sh(['git', 'rev-parse', '--short', 'HEAD']) + _, srv = remote('git rev-parse --short HEAD', 30) + srv = srv.strip().splitlines()[-1] if srv else '?' + if local != srv: + log('!! 本地 %s != 服务器 %s,同步失败' % (local, srv)) + return False + log('') + log('=== 部署完成 ===') + log('提交: %s %s' % (local, msg)) + log('本地/服务器 已同步,工作流无 commit 交叉') + return True + + +if __name__ == '__main__': + msg = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_MSG + sys.exit(0 if deploy(msg) else 1)