在gitlab上通过python服务钩子实现自动部署

在centos下使用git命令拉取代码,记住密码:

git config --global credential.helper store

gitlab进入项目管理界面,选择setting->Integrations

 最简单的python代码:

#--*--coding:utf-8--*--
from wsgiref.simple_server import make_server
from subprocess import call
import os

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    os.system('cd ~/file/tmp_gitlab_test/test ; git pull origin master') # 切换到项目的目录,并且执行pull操作
    print('git pull finish')
    return ['welcome']

httpd = make_server('', 8009, application)  # 监听8009端口
print('Serving HTTP on port 8009...')
httpd.serve_forever()

运行这个简单的web服务(将上面代码保存为webhook.py,上传服务器后执行python webhook.py 即可运行服务【注意:应该注意是否安装Python,以及8009端口是否被占用】)

用户只是往master分支push的时候才会触发webhook的钩子。其他分支是不行的

原文地址:https://www.cnblogs.com/cx850116/p/11910185.html