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 this – firstname.lastname@example.org:username/repositoryname.git .
Add this URL as remote with different name.
git remote add upstream email@example.com: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.
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.
sshpass -p paw-password-here ssh firstname.lastname@example.org '/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:
echo "Starting git pull. Author - Anurag Rana"
git pull upstream remote-repo
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.
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://email@example.com':
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 @ https://help.pythonanywhere.com/pages/SSHAccess
Starting git pull. Author - Anurag Rana
* branch mysite -> FETCH_HEAD
53e8a3a..aeaa9fe mysite -> upstream/mysite
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.