Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
hosting pythonanywhere   0   11214
Automatically updating Django website hosted on PythonAnyWhere server with every git push

Until now this is how I use to develop and deploy (update) code on PythonAnyWhere server.

- Make changes in code on my local machine.
- Commit and push the code to remote repository.
- Login to PythonAnyWhere server and start bash terminal.
- Pull the code from remote repository.
- Reload the web app from web tab.


Steps 3 to 5 are time consuming, repetitive and boring. So I thought of eliminating these steps. In this article we will see how can you get rid of these steps and your web site is automatically updated with the code as soon as you push it to remote repository.



Steps:

Get Bitbucket ssh keys:
We are using Bitbucket for source code version control. Since every-time you pull or push the code to remote repository, it prompts for password.

Manually entering the password is one step which we need to remove to automate our process.

Login to your PythonAnyWhere account and open a bash terminal. Go to home directory.


To set ssh keys for your Bitbucket account, follow the steps in this article. Feel free to comment if you are facing any issue here. Once keys are generated add the public key to your account in settings.  


Changing remote URL:
Now to pull/push code on remote repository using these keys, change the git remote URL. Check your current remote URL by running command git remote -v.

If URLs are starting with http or https then you need to add another remote with SSH URL. SSH URL of your repository would look something like username@bitbucket.org:username/repositoryname.git. Add this URL as remote with different name git remote add upstream username@bitbucket.org:username/repositoryname.git.

Now whenever you pull or push the code to remote repository, you will not be asked for password.  


Post-Push git hook on local system:
Although this type of hook is not supported by git but we will create our own git alias which will do the job for us.

So after code is committed and pushed to git repository, we wants to execute step 4 and 5. For this we created a git alias.

Open the .git/config file and paste the below code in it.

[alias]
        xpush = !git push $1 $2 && /home/rana/project-dir/reload.sh


Now instead of running git push origin repo-name, you will run git xpush origin repo-name command.

Every-time you push your code, reload.sh file is executed. Now in reload.sh file we write our code which will pull the code on PythonAnyWhere server and reload the web app. Write below code in reload.sh file and save it. Make this file executable.

#!/bin/bash
sshpass -p paw-password-here ssh user@ssh.pythonanywhere.com '/home/user/project-dir/remote.sh'


For this to work you need to install sshpass in your system or you may enable passwordless login.

So above line in reload.sh file makes an ssh connection to PythonAnyWhere server and executes a shell script located on remote server.  


Shell Script on PAW server:

#!/bin/bash
echo "Starting git pull. Author - Anurag Rana"
cd /home/user/mysite
git pull upstream remote-repo
touch /var/www/www_mysite_com_wsgi.py


Create a file at location /home/user/project-dir/remote.sh. Make this file executable.

Remember in first step above we added one more git remote named upstream. In the 4th line of above script we are pulling code from same remote URL without using password (because we are using ssh keys and this remote is created with SSH URL).

Now reloading the web app is done in 5th line of above script. This works because the server process that handles your web application is watching that file and restarts itself whenever it is modified.  



Testing:
 Now when all the above tasks are completed, lets test the application.

  • Change a file in your code at local machine.
  • Git add and git commit this file.
  • Git push. Remember to use xpush command this time. You will see that after push is done, code is pulled on PythonAnyWhere server and output is printed in terminal on your local.
  • Reload the website and see the changes instantly.


Below is the output when I edited, git added, committed and pushed the file to remote repository.

rana@Brahma: mysite$ git xpush origin mysite 
Password for 'https://anurag8989@bitbucket.org': 
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 593 bytes | 0 bytes/s, done.
Total 8 (delta 6), reused 0 (delta 0)
To https://anurag8989@bitbucket.org/anurag8989/mysite.git
   53e8a3a..aeaa9fe  mysite -> mysite
<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess
Starting git pull. Author - Anurag Rana
From bitbucket.org:anurag8989/mysite
 * branch            mysite -> FETCH_HEAD
   53e8a3a..aeaa9fe  mysite -> upstream/mysite
Updating 53e8a3a..aeaa9fe
Fast-forward
 mysite/app1/templates/app1/header.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
 

If something is not working, check if your shell script files are executable and are placed at right path.

If you are stuck at any step, feel free to comment.  


Host your Django project for free on PythonAnyWhere.

hosting pythonanywhere   0   11214
0 comments on 'Automatically Updating Django Website Hosted On Pythonanywhere Server With Every Git Push'
Login to comment


Related Articles:
How to host Django application on DigitalOcean server using Gunicorn, Supervisor, and NGINX
hosting Django application on DigitalOcean Server with custom Domain. Using WSGI server to host the Django application. Hosting Django application with Gunicorn, Supervisor, NGINX. Service static and media files of Django application using NGINX. Using Godaddy Domain to server traffic of Django application. Production deployment of Django applications....
Using a custom domain for Django app hosted on AWS EC2
Using a custom domain for Django app hosted on AWS EC2, GoDaddy DNS with EC2 instance, Django App on EC2 with GoDaddy DNS, Using domain name in Nginx and EC2, Elastic IP and DNS on EC2...
Hosting Django app for free on Amazon (AWS) EC2 with Gunicorn and Nginx
Step by step guide on hosting Django application on AWS ec2 instance, How to host the Django app on AWS ec2 instance from scratch, Django on EC2, Django app hosting on AWS, Free hosting of Django App...
Preventing cross-site scripting attack on your Django website
XSS attack in Django, preventing cross-site scripting attack in Django website, avoid XSS in Django application, Enabling SECURE_BROWSER_XSS_FILTER in Django website, blocking cross-site scripting attach on Django website...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap