将Github与Pythonanywhere同步

问题描述 投票:2回答:2

我想用github帐户同步pythonanywhere项目。就像我在github上的项目中进行更改一样,它会在pythonanywhere自动更新。原谅我,我是github的新手。

git github pythonanywhere
2个回答
4
投票

我刚刚为自己的Pythonanywhere项目解决了这个问题。我不想打扰SSH密钥,所以我使用了Github webhooks和一个在我的pythonanywhere帐户上运行的Python脚本。 Python脚本监听Github在更新源代码时发出的webhook,并在pythonanywhere上执行脚本以引入新文件。

这是场景:

  • 我在本地计算机上使用Visual Studio进行开发,并将我的代码推送到我的Github存储库
  • Github自动发出一个带有json文件的post-receive webhook,我在我的pythonanywhere服务器上监听
  • 在我的python脚本中,只要触发webhook URL,我就会执行一个pull命令。之后我在pythonanyhwere上的所有文件都是最新的

提示:

  • 如果您还没有在pythonanywhere项目上启动git,只需打开一个bash控制台,导航到您的根文件夹,例如“home / username”并输入git init,然后输入git remote add origin https://github.com/yourusername/yourreponame.git
  • 您可以在github存储库的设置页面中创建post-receive webhook
  • 我使用GitPython包来执行pull请求
  • 下面是我在我的烧瓶Web服务器中使用的python代码,等待webhook执行。它基本上执行一个预定义的bash命令,该命令在pythonanywhere文件结构中自动创建,位于.git/hooks/下。这个bash文件将执行一个简单的git pull origin master

我的flask_app.py文件的内容:

from flask import Flask, request
import git

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
    def webhook():
        if request.method == 'POST':
            repo = git.Repo('./myproject')
            origin = repo.remotes.origin
            repo.create_head('master', 
        origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
            origin.pull()
            return '', 200
        else:
            return '', 400

#
# Below here follows you python back-end code
#

如果您需要更多信息,请告诉我。


3
投票

你可以考虑:

如果你只想在pythonanywhere上开发,你需要生成一个SSH密钥,并将公共密钥添加到你的GitHub帐户,如“How to get your code in and out of PythonAnywhere”中所述。

© www.soinside.com 2019 - 2024. All rights reserved.