gitlab 实现自动部署(简单Python实现)

功能说明:

    当本地master分支执行push动作的时候,服务器端会自动执行master分支的pull操作(还可以执行一些自动化脚本)

原理:

    git hooks就是那些在git执行特定事件(如commit、push、receive等)后触发运行的脚本。gitlab的web hooks跟git hook类似。也是当项目发生提交代码、提交tag等动作会自动去调用url,这个url可以是更新代码。或者其他操作。

写一个最简单的Python web服务:

#--*--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端口是否被占用】)

设置webhook:(这是填入刚刚运行起来的Python web小站点的url,然后选择什么时候触发钩子)

本地push一个文件:

查看服务器结果

这样,文件就自动pull下来了。我们还可以自己写一些shell脚本,重启服务,或者一些其他什么的。

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

更多内容,可访问:http://rexyan.cn
原文地址:https://www.cnblogs.com/rexyan/p/7326581.html