将Github与Pythonanywhere同步 [英] syncing Github with Pythonanywhere
问题描述
我想将pythonanywhere项目与github帐户同步. 就像如果我在github上的项目中进行更改一样,它会在pythonanywhere自动更新. 请原谅我是github的新手.
I want to sync pythonanywhere project with github account. Like If I make changes in my project at github,it automatically gets updated at pythonanywhere. Forgive me I am new to github.
推荐答案
我刚刚为自己的Pythonanywhere项目解决了此问题.我不想打扰SSH密钥,所以我使用了Github Webhooks和在我的pythonanywhere帐户上运行的Python脚本.更新源代码后,Python脚本会监听Github发出的Webhook,并在pythonanywhere上执行脚本以提取新文件.
I just solved this issue for my own Pythonanywhere project. I did not want to bother with SSH keys, so I used Github webhooks and a Python script that runs on my pythonanywhere account. The Python script listens to the webhook that Github emits when the source code has been updated and executes a script on pythonanywhere to pull in the new files.
这是场景:
- 我在本地计算机上的Visual Studio中进行开发,并将代码推送到我的Github存储库中
- Github会自动发出一个带有json文件的接收后Webhook,我在pythonanywhere服务器上监听
- 在我的python脚本中,只要触发了Webhook URL,我就简单地执行pull命令.之后,我在pythonanyh上的所有文件都是最新的
提示:
- 如果您尚未在pythonanywhere项目上启动git,只需打开bash控制台,导航到您的根文件夹,例如家庭/用户名",然后输入
git init
,然后输入git remote add origin https://github.com/yourusername/yourreponame.git
- 您可以在github存储库的设置页面中创建接收后Webhook
- 我使用GitPython包执行拉取请求
- 以下是我在烧瓶Web服务器中使用的python代码,以等待webhook执行.它基本上执行一个预定义的bash命令,该命令在pythonanywhere文件结构中自动创建,并且位于
.git/hooks/
下.这个bash文件将执行一个简单的git pull origin master
- If you have not yet initiated git on your pythonanywhere project, just open a bash console, navigate to your root folder, e.g. "home/username" and enter
git init
, thengit remote add origin https://github.com/yourusername/yourreponame.git
- You can create the post-receive webhook in your github repository's settings page
- I use the GitPython package to execute the pull request
- Below is the python code I use in my flask web server to wait for the webhook to execute. It basically executes a predefined bash command that gets created automatically in your pythonanywhere file structure and that is located under
.git/hooks/
. This bash file will execute a simplegit pull origin master
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
#
如果您需要更多信息,请告诉我.
Let me know if you need more info.
这篇关于将Github与Pythonanywhere同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!