build: 新增 deploy.py 一键部署脚本(固化本地push→服务器pull,根治commit交叉)
This commit is contained in:
@@ -38,7 +38,11 @@ ENV = dict(os.environ, GIT_TERMINAL_PROMPT='0') # 禁凭据交互,缺凭据
|
|||||||
|
|
||||||
|
|
||||||
def sh(args, timeout=TIMEOUT):
|
def sh(args, timeout=TIMEOUT):
|
||||||
"""跑命令,返回 (exit, stdout)"""
|
"""跑本地命令,返回 (exit, stdout)。args 为 list,shell=False。
|
||||||
|
|
||||||
|
Windows 上 shell=True 交给 cmd.exe,引号语义与 POSIX 不同,
|
||||||
|
提交信息等参数若用 shlex.quote 反而会出错 —— 统一用 list 直传。
|
||||||
|
"""
|
||||||
r = subprocess.run(args, capture_output=True, text=True,
|
r = subprocess.run(args, capture_output=True, text=True,
|
||||||
timeout=timeout, env=ENV)
|
timeout=timeout, env=ENV)
|
||||||
return r.returncode, (r.stdout + r.stderr).strip()
|
return r.returncode, (r.stdout + r.stderr).strip()
|
||||||
@@ -49,8 +53,16 @@ def log(msg):
|
|||||||
|
|
||||||
|
|
||||||
def remote(cmd, timeout=TIMEOUT):
|
def remote(cmd, timeout=TIMEOUT):
|
||||||
"""在服务器上执行命令,返回 (exit, stdout)"""
|
"""在服务器上执行命令,返回 (exit, stdout)
|
||||||
return sh([SSH_BASE, '"cd %s && %s"' % (REMOTE_DIR, cmd)], timeout)
|
|
||||||
|
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):
|
def deploy(msg=DEFAULT_MSG):
|
||||||
|
|||||||
Reference in New Issue
Block a user