11

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.

3 Answers 3

15

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.

Here is the scenario:

  • I develop in Visual Studio on my local computer and push my code to my Github repository
  • Github automatically emits a post-receive webhook with a json file that I listen to on my pythonanywhere server
  • In my python script I simply execute a pull command as soon as the webhook URL gets triggered. After that all my files on pythonanyhwere are up-to-date

Tips:

  • 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, then git 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 simple git pull origin master

The content of my flask_app.py file:

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Wanni would you mind including all import statements to your function above? 'import git' is clear, but where does 'request' and 'app' come from?
Can this exist in the same webapp that is supposed to update?
@aadibajpai yes, the piece of python code above is located in the same file (flask_app.py) that runs my flask server/back-end.
@Wanni how does it reload?
Okay, I looked into it—if you add a post-merge hook to touch the wsgi file then the webapp will reload if the pull goes successful :)
4

You could consider:

If you want to develop only on pythonanywhere, you would need to generate an SSH key, and add the public one to your GitHub account, as suggested in "How to get your code in and out of PythonAnywhere".

4 Comments

What would I need to do after I add the SSH key to GitHub? Is the idea that GitHub updates from PA?
@aadibajpai Yes, you could push from PA to GitHub.
that wouldn't be automated though as mentioned in OP's question.
@aadibajpai I suppose not, but the question is about automating the propagation of GitHub changes to PA, not the reverse.
1

For Django:

first you need to install gitpython: pip install gitpython

update views.py:

from django.http import HttpResponse
from git import Repo # 
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def webhook(request):
    if request.method == 'POST':
        repo = Repo('./django-schools')
        git = repo.git
        git.checkout('master')
        git.pull()
        return HttpResponse('pulled_success')
    return HttpResponse('get_request', status=400)

1 Comment

This is the closest solution I found, however, I'm getting the following error: "git.exc.GitCommandError: Cmd('git') failed due to: exit code(128) cmdline: git checkout master stderr: 'fatal: This operation must be run in a work tree' ". After looking into this it seems the solution is "Create another repository elsewhere, add a file in that repository and, push it to the bare repository." It makes little sense as to why I need to create another repository.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.