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.


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 Add this URL as remote with different name git remote add upstream

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.

        xpush = !git push $1 $2 && /home/rana/project-dir/

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

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

sshpass -p paw-password-here ssh '/home/user/project-dir/'

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

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

Shell Script on PAW server:

echo "Starting git pull. Author - Anurag Rana"
cd /home/user/mysite
git pull upstream remote-repo
touch /var/www/

Create a file at location /home/user/project-dir/ 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.  

 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 '': 
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)
   53e8a3a..aeaa9fe  mysite -> mysite
<<<<<<:>~ PythonAnywhere SSH. Help @
Starting git pull. Author - Anurag Rana
 * branch            mysite -> FETCH_HEAD
   53e8a3a..aeaa9fe  mysite -> upstream/mysite
Updating 53e8a3a..aeaa9fe
 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.

Related Articles:
How to backup database periodically on PythonAnyWhere server
Scheduling the database backup, script to backup database periodically. Delete old backup files periodically. PythonAnyWhere database backup....
Iterator and Generators in Python: Explained with example
understanding iterators and generators in python, creating python generators, using generators and iterators in python code...
How to create completely automated telegram channel with python
Creating a completely automated telegram channel to generate and post content using python code on regular basis. Automating the Telegram channel using python script...
Python Tip 1: Accessing localhost Django webserver over the Internet
how to generate public URL to expose your local Django webserver over the Internet. How to let everyone access your Django project running on localhost....

0 thoughts on 'Automatically Updating Django Website Hosted On Pythonanywhere Server With Every Git Push'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© 2018-2019