diff --git a/deploy/deploy.sh b/deploy/deploy.sh index fec6203..faa803c 100644 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -2,6 +2,9 @@ # 画个标自动部署脚本(由 Gitea webhook 触发或手动执行) set -e cd /opt/drawlogo -git fetch origin main && git reset --hard origin/main +# 禁止 git 在缺少凭据时等待交互输入(否则会挂死 webhook) +export GIT_TERMINAL_PROMPT=0 +timeout 120 git fetch origin main +git reset --hard origin/main echo "$(date '+%Y-%m-%d %H:%M:%S') DEPLOY ok $(git log -1 --format='%h %s')" >> /opt/drawlogo/.deploy.log echo "部署完成: $(git log -1 --format='%h %s')" \ No newline at end of file diff --git a/deploy/drawlogo-webhook.py b/deploy/drawlogo-webhook.py index 60ce7fc..0f79200 100644 --- a/deploy/drawlogo-webhook.py +++ b/deploy/drawlogo-webhook.py @@ -1,30 +1,42 @@ #!/usr/bin/env python3 -"""画个标 Gitea webhook 监听(push main 时自动部署)""" -import http.server, subprocess, json, os, datetime +"""画个标 Gitea webhook 监听(push main 时自动部署) + +要点: + - 使用 ThreadingHTTPServer:单个部署任务不会阻塞后续请求 + - 部署在独立子进程中执行,立即返回 200,避免 Gitea 等待超时 +""" +import json +import subprocess +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer DEPLOY_SCRIPT = '/opt/drawlogo/deploy/deploy.sh' +LOG = '/opt/drawlogo/.deploy.log' -class Handler(http.server.BaseHTTPRequestHandler): + +class Handler(BaseHTTPRequestHandler): def do_POST(self): length = int(self.headers.get('Content-Length', 0)) body = self.rfile.read(length) try: data = json.loads(body) if body else {} - ref = data.get('ref', '') - if ref == 'refs/heads/main': + if data.get('ref') == 'refs/heads/main': subprocess.Popen( ['/bin/bash', DEPLOY_SCRIPT], - stdout=open('/opt/drawlogo/.deploy.log', 'a'), - stderr=subprocess.STDOUT + stdout=open(LOG, 'a'), + stderr=subprocess.STDOUT, + close_fds=True ) - except Exception: - pass + except Exception as e: + with open(LOG, 'a') as f: + f.write(f'[webhook] 处理失败: {e}\n') self.send_response(200) + self.send_header('Content-Type', 'text/plain') self.end_headers() self.wfile.write(b'ok') - def log_message(self, format, *args): + def log_message(self, fmt, *args): pass + if __name__ == '__main__': - http.server.HTTPServer(('0.0.0.0', 7890), Handler).serve_forever() \ No newline at end of file + ThreadingHTTPServer(('0.0.0.0', 7890), Handler).serve_forever() \ No newline at end of file