git - 将 Github 与 Pythonanywhere 同步

标签 git github pythonanywhere

我想将 pythonanywhere 项目与 github 帐户同步。 就像如果我在 github 上对我的项目进行更改,它会自动在 pythonanywhere 上更新。 原谅我,我是 github 的新手。

最佳答案

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

场景如下:

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

提示:

  • 如果您还没有在您的 pythonanywhere 项目上启动 git,只需打开一个 bash 控制台,导航到您的根文件夹,例如“home/username”并输入git init,然后git remote add origin https://github.com/yourusername/yourreponame.git
  • 您可以在 github 存储库的设置页面中创建接收后 webhook
  • 我使用 GitPython 包来执行 pull 请求
  • 下面是我在 Flask 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
#

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

关于git - 将 Github 与 Pythonanywhere 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48047123/

相关文章:

html - 有没有办法指定嵌入要点的宽度?

PythonAnywhere 试图上传我的网站

python - 如何通过代码重新启动pythonanywhere控制台

javascript - 在 PythonAnywhere 上托管基于 Tornado 的 Python 应用程序时出现 "Error running WSGI application"

git - 从 github 中完全删除旧的提交(不仅仅是从历史中)

git - 如何 "pull request"一个特定的提交

git - "git log -1"有什么作用?

java - 将现有 Java 项目放入 Github

linux - git --- 如何在 linux 上安排 `git push`?

ruby - 是否有使用 Rugged 将所有内容添加到 Git 的特定协议(protocol)?