Automatically update server with the update on GitHub

GitHub SSH Authentication

To automatically execute git commands, we need to either store the login confidentials on server, or using ssh key to login. We can generate ssh key using ssh-keygen -t rsa -C "email@example.net" After generating ssh key, we need to add ssh public key on GitHub account Settings-SSH and GPG keys. Also, the private key should be deployed on ~\.ssh\id_rsa.

We can test the ssh connection using ssh -T git@github.com or ssh -T git@github.tamu.com (for school use).

For the repos that cloned previously, we need to update the upstream or origin using: git remote set-url [origin|upstream] [https://github.com/USERNAME/REPOSITORY.git|git@github.com:USERNAME/REPOSITORY.git], as shown in Changing a remote’s URL.

CSRF_EXEMPT

On views.py, we add

1
2
3
4
5
6
7
8
9
10
11
!pip3 install GitPython

import git
@csrf_exempt
def githook(request):
    repo = git.Repo('/home/smartscript/')
    for remote in repo.remotes:
        remote.fetch()
    repo.git.reset('--hard')
    return JsonResponse({'issues': "OK"})

csrf-error

Otherwise, there would be a csrf error when sending post request, and GitHub webhook will return with Transferred a partial file.