Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gitlab 自动发布

自动更新代码

功能

该功能通过监听 git push 事件,触发A服务器指定目录 git pull。
比如 forumsbaozi 分支在任意环境下成功push后,会在A服务器的 /data/web/framework/trunk/ baozi/ forums 目录执行 git pull

部署

在A服务器运行 auto_update.py。
在 gitlab,添加 web hook: http://A服务器IP:8003

#!/usr/local/bin/python2.7
#coding=utf-8
########
# 自动发布
# nohup /data/sbin/AutoUpdate.py >/tmp/my_auto_update.log 2>&1 &
__author__ = 'baozi'
from BaseHTTPServer import HTTPServer
import os
import json
from sys import version as python_version
from cgi import parse_header, parse_multipart
if python_version.startswith('3'):
from urllib.parse import parse_qs
from http.server import BaseHTTPRequestHandler
else:
from urlparse import parse_qs
from BaseHTTPServer import BaseHTTPRequestHandler
class GitRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.wfile.write('method error.')
def do_POST(self):
self.process()
def process(self):
ctype, pdict = parse_header(self.headers['content-type'])
if ctype == 'application/json':
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
self.send_response(200)
self.end_headers()
else:
data = {}
repository = data.get('repository')
if repository:
project = repository.get('name')
branch = data.get('ref').split('/')[-1]
cmd = 'git -C /data/web/framework/trunk/%s/%s pull' % (branch, project)
#os.system('git -C pull')
os.system(cmd)
self.wfile.write('OK.')
server = HTTPServer(('', 8003), GitRequestHandler)
print 'started http server...'
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.