本职是前端,最近想利用GitHub的webhook + Flask 搞个简单的自动化部署,需求很简单:
在有提交的时候通过webhook接口自动拉取代码,并重启uwsgi。
python新手,代码轻喷。
flask代码(目录结构参考的《FLASK WEB开发》中的目录,就不贴了);主要的代码如下:
# coding=utf-8 import os from flask import request, json, jsonify from threading import Thread from . import main def restart(): os.system('./reload.sh') @main.route('/') def welcome(): user_agent = request.headers.get('User-Agent') return "Welcome, %s" % user_agent @main.route('/webhook', methods=['POST']) def webhook(): print('---------------------begin----------------------') print(request.get_json()) print('---------------------end----------------------') push_info = request.get_json() commit_info = push_info['commits'] if push_info['ref'] == 'refs/heads/master': print('master分支有提交') print(push_info['commits']) os.system('git pull origin master') if push_info['commits'][0]['committer']['email'] == '**********@qq.com': print('确认是自己提交的') data = { 'hello': 'world', } js = json.dumps(data) resp = jsonify(data) resp.status_code = 200 t = Thread(target=restart, daemon=True) t.start() return resp
reload.sh脚本代码
#/usr/bin/sh sleep 10s killall -s INT /www/webhook/bin/uwsgi sleep 10s uwsgi uwsgi.ini
遇到的问题如下:
1.return语句不会执行,因为线程中把uwsgi杀死了 2.脚本关闭uwsgi报 `is taking too much time to die...NO MERCY !!!` 3.上一步时间太长导致uwsgi重启失败
有木有大神帮忙看下有什么问题,可以怎么改
重启服务器可以用: