git webhook 失败 - 你知道为什么吗?

git webhook fails - do you know why?

背景:

我的本地机器上有一个 bitbucket repo called DOSTUFF that includes a python script do_stuff.py. I edit it using Eclipse pydev 并通过 git push origin master 将更改推送到 bitbucket。

我已将 DOSTUFF 克隆到 pythonanywhere 试用帐户,没有任何问题。

现在,每当我在本地编辑 do_stuff.py 然后 git commit -m 'foo'git push origin master 它们到 bitbucket 时,我之后手动需要从 pythonanywhere 中 git pull 才能看到pythonanywhere 中的编辑。这个is inefficient.

目标:

我希望我的本地 (Eclipse) 对 bitbucket 的提交在从本地推送到 bitbucket 后自动拉到 pythonanywhere。显然,webhooks 是正确的选择。

挑战:

为了做到这一点,我按照 this 的提示在 bitbucket 中指定了一个 webhook 到 pythonanywhere/user/me/webhook.py。不幸的是,这些说明是极简主义的,因为它们缺乏适当的导入并且没有阐明为什么需要烧瓶(我不是专家)。

webhook.py 看起来像这样:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*

import git
from flask import Flask, request

# Initiate flask instance
app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def webhook():
    if request.method == 'POST':
        repo = git.Repo('./DOSTUFF')
        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

if __name__ == '__main__':
    app.run(port=5000,debug=True)

现在,当我 git push 从 Eclipse 到 bitbucket 时,提交到达 bitbucket 但 pythonanywhere 中的代码保持不变。换句话说,webhook.py 失败了。

相比之下,当我从 pythonanywhere(bash 控制台)中 运行 webhook.py 时,我产生了以下错误:

 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
Traceback (most recent call last):
  File "/home/ME/webhook.py", line 21, in <module>
    app.run(port=5000,debug=True)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 795, in run_simple
    s.bind(get_sockaddr(hostname, port, address_family))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use

问题:

失败的根本原因是什么?

一旦从本地推送到 bitbucket,如何正确配置自动 git pull 更改 pythonanywhere 所必需且充分的 webhook?

您正在尝试在 PythonAnywhere 控制台中启动服务器,这将不起作用,因为流量未路由到控制台服务器。使用 Web 应用创建服务器以侦听 Web 挂钩。