功能說明:
當本地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的鈎子。其他分支是不行的