Bash 如何从远程git存储库实时获取推送通知?
我有一个远程存储库(bitbucket)、一个服务器存储库(linux)和许多本地存储库(开发人员) 如果某个开发人员更改了远程服务器中的分支,我希望服务器得到通知 到目前为止,我尝试在服务器中使用cron中声明的bash脚本,但最小分辨率为1分钟,因此要使其适应实时性似乎非常棘手。然而,我的方法是:Bash 如何从远程git存储库实时获取推送通知?,bash,cron,bitbucket,githooks,Bash,Cron,Bitbucket,Githooks,我有一个远程存储库(bitbucket)、一个服务器存储库(linux)和许多本地存储库(开发人员) 如果某个开发人员更改了远程服务器中的分支,我希望服务器得到通知 到目前为止,我尝试在服务器中使用cron中声明的bash脚本,但最小分辨率为1分钟,因此要使其适应实时性似乎非常棘手。然而,我的方法是: path=/some/dir/ # something new in remote if git fetch origin && [ `git rev-list HEAD...o
path=/some/dir/
# something new in remote
if git fetch origin && [ `git rev-list HEAD...origin/master --count` != 0 ]
then
echo 'Remote repository has changed. Pull needed' &>>$path/git-backup.log
echo $d >> $path/git-backup.log
git merge origin/master &>>$path/git-backup.log
fi
我终于解决了
在比特桶侧
您只需声明一个指向服务器的Webhook
在服务器端
你需要一个机制,一直监听某个端口。这是始终运行的芹菜_listengit.py的内容:
import subprocess
import threading
from tasks import git_pull
from flask import *
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def listen_git4pull():
# Range of bitbucket IP Addresses that we know.
trusted = ['131.103.20', '165.254.145', '04.192.143']
incoming = '.'.join(str(request.remote_addr).split('.')[:-1])
if incoming in trusted:
git_pull.delay()
# OK
resp = Response(status=200, mimetype='application/json')
else:
# HTTP not authorized
resp = Response(status=403, mimetype='application/json')
return resp
class CeleryThread(threading.Thread):
def run(self):
error_message = ('Something went wrong when trying to '
'start celery. Make sure it is '
'accessible to this script')
command = ['celery',
'-A',
'tasks',
'worker',
'--loglevel=info']
try:
_ = subprocess.check_output(command)
except Exception:
raise Exception(error_message)
if __name__ == '__main__':
celery_thread = CeleryThread()
celery_thread.start()
app.run(host='0.0.0.0')
这是任务的内容,其中声明了git_pull()函数。这必须延迟,因为git.pull()可能需要很多时间,所以它必须在服务器以HTPP OK状态响应后运行:
from git import *
from celery.app.base import Celery
app = Celery('tasks',
backend='amqp://guest@localhost//',
broker='amqp://guest@localhost//')
app.conf.update(
CELERY_ACCEPT_CONTENT=['json'],
CELERY_TASK_SERIALIZER='json',
CELERY_RESULT_SERIALIZER='json',)
@app.task
def git_pull():
repo = Repo('.')
o = repo.remotes.origin
o.fetch()
o.pull()
您是否能够将签出后挂钩添加到bitbucket遥控器?在
rev list
测试之后,您有一个杂散的&&
。另外,四字git fetch
通常不是很有用,因为它不会更新任何本地引用。如果您只想告诉远程引用是否已更改,那么只需git fetch origin
和git rev list--count master..origin/master
就可以告诉您git diff——退出代码主源代码/master
也应该可以工作,并且可以在不使用[…]
包装的情况下使用。您也可以总是获取、合并和记录,不是吗?您是对的goatshepard,解决方案是git hooks()。在bitbucket中,这可以通过具有管理员权限的“Webhooks”进行控制。在我的例子中,我在服务器端安装了一个应用程序来监听特定端口,以便得到通知。完成后我会发布代码。