42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""画个标 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(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 {}
|
|
if data.get('ref') == 'refs/heads/main':
|
|
subprocess.Popen(
|
|
['/bin/bash', DEPLOY_SCRIPT],
|
|
stdout=open(LOG, 'a'),
|
|
stderr=subprocess.STDOUT,
|
|
close_fds=True
|
|
)
|
|
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, fmt, *args):
|
|
pass
|
|
|
|
|
|
if __name__ == '__main__':
|
|
ThreadingHTTPServer(('0.0.0.0', 7890), Handler).serve_forever() |