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.
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 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.
.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 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:
#!/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.
Now when all the above tasks are completed, lets test the application.
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) To https://firstname.lastname@example.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.