1、设置webhook

gitlab->setting->webhook:http://121.143.191.166:7000?token=23028-b396-12e5-9912-bae0483c18

2、设置django
注释掉下列所示一行,关闭CSRF

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

3、添加路由:

url(r'^$',"buServer.views.index",name='index'),

4、添加view:

import datetime
import time
import json
import os

def now():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")


def index(request):
    try:
        token=request.GET['token']
    except KeyError:
        #验证用户是否登录
        if request.session.get("user") is not None:
            return HttpResponseRedirect("/home")
        else:
            return render(request, 'login.html')

    if token !="230a1428-b396-12e5-9912-ba1be0483c18":
        return render(request, 'result.html', {'result': "token is not right."})


    if request.method == 'POST':
        json_data = json.loads(request.body)
        ver=json_data["ref"].split('/')[2]
        out=open("/tmp/tts.log",'a')
        out.write("post:"+ver+'\n')
        out.close()
        if ver=="devlop":
            val1 = os.popen("/data/release_docker.sh develop").read()
        out.write(now())
        return HttpResponse(json_data)
    else:
        val1 = os.popen("/data/release_docker.sh develop").read()
               return render(request, 'result.html', {'result': val1})