fix(deploy): webhook 改为多线程,git fetch 禁止交互等待,避免部署卡死
This commit is contained in:
+4
-1
@@ -2,6 +2,9 @@
|
|||||||
# 画个标自动部署脚本(由 Gitea webhook 触发或手动执行)
|
# 画个标自动部署脚本(由 Gitea webhook 触发或手动执行)
|
||||||
set -e
|
set -e
|
||||||
cd /opt/drawlogo
|
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 "$(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')"
|
echo "部署完成: $(git log -1 --format='%h %s')"
|
||||||
+23
-11
@@ -1,30 +1,42 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""画个标 Gitea webhook 监听(push main 时自动部署)"""
|
"""画个标 Gitea webhook 监听(push main 时自动部署)
|
||||||
import http.server, subprocess, json, os, datetime
|
|
||||||
|
要点:
|
||||||
|
- 使用 ThreadingHTTPServer:单个部署任务不会阻塞后续请求
|
||||||
|
- 部署在独立子进程中执行,立即返回 200,避免 Gitea 等待超时
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||||
|
|
||||||
DEPLOY_SCRIPT = '/opt/drawlogo/deploy/deploy.sh'
|
DEPLOY_SCRIPT = '/opt/drawlogo/deploy/deploy.sh'
|
||||||
|
LOG = '/opt/drawlogo/.deploy.log'
|
||||||
|
|
||||||
class Handler(http.server.BaseHTTPRequestHandler):
|
|
||||||
|
class Handler(BaseHTTPRequestHandler):
|
||||||
def do_POST(self):
|
def do_POST(self):
|
||||||
length = int(self.headers.get('Content-Length', 0))
|
length = int(self.headers.get('Content-Length', 0))
|
||||||
body = self.rfile.read(length)
|
body = self.rfile.read(length)
|
||||||
try:
|
try:
|
||||||
data = json.loads(body) if body else {}
|
data = json.loads(body) if body else {}
|
||||||
ref = data.get('ref', '')
|
if data.get('ref') == 'refs/heads/main':
|
||||||
if ref == 'refs/heads/main':
|
|
||||||
subprocess.Popen(
|
subprocess.Popen(
|
||||||
['/bin/bash', DEPLOY_SCRIPT],
|
['/bin/bash', DEPLOY_SCRIPT],
|
||||||
stdout=open('/opt/drawlogo/.deploy.log', 'a'),
|
stdout=open(LOG, 'a'),
|
||||||
stderr=subprocess.STDOUT
|
stderr=subprocess.STDOUT,
|
||||||
|
close_fds=True
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
pass
|
with open(LOG, 'a') as f:
|
||||||
|
f.write(f'[webhook] 处理失败: {e}\n')
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
|
self.send_header('Content-Type', 'text/plain')
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(b'ok')
|
self.wfile.write(b'ok')
|
||||||
|
|
||||||
def log_message(self, format, *args):
|
def log_message(self, fmt, *args):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
http.server.HTTPServer(('0.0.0.0', 7890), Handler).serve_forever()
|
ThreadingHTTPServer(('0.0.0.0', 7890), Handler).serve_forever()
|
||||||
Reference in New Issue
Block a user