如何利用git的hook实现提交代码后自动更新?
由于我的开发常常须要提交代码,每次都须要链接服务器去pull代码,重启服务器就显得十分繁琐,所以github提供了一个时间钩子,用户push代码后能够通知指定服务器进行操做html
编写服务器脚本
脚本仅仅用于接受代码托管服务器的通知,所以应该尽可能比较各类依赖问题,因此咱们选择在linux自带的python2 上开发,使用原生的wsgiref模块python
from wsgiref.simple_server import make_server
import os
def run(env,start_response):
url=env['PATH_INFO']
if url=="/githook/":
out=os.system('/website/Tadmin/pub.sh')
start_response('200 OK', [('Content-Type', 'text/html')])
return str(out)
start_response('200 OK', [('Content-Type', 'text/html')])
return "hello"
if __name__ == '__main__':
httpd = make_server('', 8080, run)
print "server running on 8080"
httpd.serve_forever() #allays running
当知道服务器有新的push,能够在客户端执行一系列的更新操做,在上面咱们直接调用了shell脚本执行linux
cd '/website/YourProject'
git pull
uwsgi --reload /website/YourProject/uwsgi.pid
ps -ef | grep uwsgi
server的启动直接使用CMD:nohup python server.py &
最后就是设置git的webhook 而后push代码 服务器就会自动同步了git