Deploy Django app to Python Anywhere through GitHub

86 Views Asked by At

I am currently trying to set up a deployment pipeline for my django app to python anywhere.

But the github webhook recieves a 301 and I don't understand why... "Last delivery was not successful. Invalid HTTP Response: 301"

I followed the instructions from this tutorial: https://digitalshyna.com/blog/how-to-deploy-django-apps-to-python-anywhere-through-github/

I added the update url:

path('update_server/', views.update, name='update'),

this is my update view:

@csrf_exempt
def update(request):
if request.method == "POST":
    try:
        # Log the incoming request
        logger = logging.getLogger('webhook_logger')
        logger.info("Webhook POST request received")

        # Perform the code update
        repo = git.Repo('mayapp.pythonanywhere.com')
        origin = repo.remotes.origin
        origin.pull()

        # Log a success message
        message = "Updated code on PythonAnywhere"
        logger.info(message)

        return HttpResponse(message)
    except GitCommandError as e:
        # Log a Git error and return an error response
        error_message = f"Git Error: {str(e)}"
        logger.error(error_message)
        return HttpResponseServerError(error_message, status=500)
else:
    return HttpResponse("Couldn't update the code on PythonAnywhere")

GitPython is installed in my Pythonanywhere virtualenv.

I created the deploy key and added it to my repository on github. I created a second ssh-key and added it to my github account, I added both keys to the ssh-agend on pythonanywhere. https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

When I access the route directly with Postman I see "Couldn't update the code on PythonAnywhere" Status Code 200 OK

My log files remain empty I am not sure whats the problem yet but I will post the logs once working...

EDIT:

here is the logging configuration from my settings.py

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'logs', 'error.log'),
        },
        'webhook_logger_file': { 
            'level': 'DEBUG', 
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'logs', 'webhook.log'), 
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'ERROR',
            'propagate': True,
        },
        'webhook_logger': {  
            'handlers': ['webhook_logger_file'],
            'level': 'DEBUG', 
            'propagate': False, 
        },
    },
}

the log files are getting created inside the logs directory but remain empty...

0

There are 0 best solutions below