git 利用hook 实现服务器自动更新代码

如何利用git的hook实现提交代码后自动更新?

因为个人开发经常需要提交代码,每次都需要连接服务器去pull代码,重启服务器就显得十分繁琐,因此github提供了一个时间钩子,用户push代码后可以通知指定服务器进行操作

编写服务器脚本

脚本仅仅用于接受代码托管服务器的通知,因此应该尽量比较各种依赖问题,所以我们选择在linux自带的python2 上开发,使用原生的wsgiref模块

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脚本执行

cd '/website/YourProject'
git pull
uwsgi --reload /website/YourProject/uwsgi.pid
ps -ef | grep uwsgi

server的启动直接使用CMD:nohup python server.py &
最后就是设置git的webhook 然后push代码 服务器就会自动同步了

猜你喜欢

转载自www.cnblogs.com/tongchengbin/p/10364161.html