Loess.ru

having fun

Git clone branch from one remote to another by webhook call

quick and (very) dirty:

1.

git clone repo.git
cd repo
git status
git remote -v
git remote set-url --push origin git@github.com:User/repo.git

this should set your remote.origin.pushurl to another path, check it with

git remote -v

2. check it with

git pull origin master && git push

3. make sure you set your private and public ssh keys for pulling and pushing seamlessly

4. write bash script for this, for example:

cd /home/lol/repo/
echo $(date +%Y-%m-%d-%H:%M) >> /home/lol/copy-repo.log
git pull origin master >> /home/lol/copy-repo.log 2>&1
git push >> /home/lol/copy-repo.log 2>&1

You can skip and do this directly from within Python flask server with https://gitpython.readthedocs.io/en/stable/tutorial.html

5. Set up webhook @ first webservice to call @ push event. Write flask-server for getting webhooks, open incoming port, for example:

from flask import Flask
from flask import request
from bitbucket_webhooks import event_schemas
from bitbucket_webhooks import hooks
from bitbucket_webhooks import router
import subprocess

app = Flask(__name__)

@app.route("/hooks", methods=["POST"])
def bb_webhooks_handler():
    router.route(request.headers["X-Event-Key"], request.json)
    return ("", 204)

@hooks.repo_push
def _handle_repo_push(event: event_schemas.RepoPush):
    print(f"One or more commits pushed to: {event.repository.name}")
    subprocess.call("/home/lol/copy-repo.sh")

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=617, debug=False)

or if you prefer bash+netcat-like way, consider trying this https://gist.github.com/orangeblock/15ee8e3cb304a4046082b422adaaf5fb

6. wrap it into waitress call (i don’t know why but flask says it sufficient 😀 ):

if __name__ == '__main__':
    from waitress import serve
    serve(app,host="0.0.0.0",port=617)

7. wrap it into Systemd unit (https://blog.miguelgrinberg.com/post/running-a-flask-application-as-a-service-with-systemd):

[Unit]
Description=Webhook monitor for copying repo from BB to GitHub
After=network.target

[Service]
User=lol
WorkingDirectory=/home/lol
ExecStart=/usr/bin/python3 /home/lol/clone-repo-webhook-server.py
Restart=always

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start clone-repo-webhook-server
systemctl enable clone-repo-webhook-server

p.s. and you could combine this with git full-mirroring capabilities as you like
p.s. heres bb man https://support.atlassian.com/bitbucket-cloud/docs/create-and-trigger-a-webhook-tutorial/ and webhook listener repo https://bitbucket.org/atlassianlabs/webhook-listener which I haven’t read

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Test your skill: *