Continuing with the series of posts on the deployment of web applications on a Raspberry Pi server, we are going to take a look today at one of the more useful concepts that we will encounter in our programming life: Version control systems.
Specifically, we are going to focus on Git, a VCS developed for Linux Torvalds that is both widely used and very powerful.
One of the services that has helped git to become as popular as it is nowadays is Github. We, however, are not going to depend on any external services, but we will use an external HDD connected through USB to store our repositories. Also, at the end of this guide you will be able to update the source code of your running application through Git pushing to a remote directory.
The purpose of this guide is not to be a full blown reference manual, but to help experienced developers to start using
git in a practical way, keeping track of the source code changes as well as seamlessly pushing them to production.
Creating the repositories
We will be creating two git repositories, one with the source code of our application and another one naked, that we will use as a hook to update the running app.
First, we should log into our server, mount the required directories and
cd into them. I’m assuming by now that you already know how to do that.
Second, we will create a bare repository with the name of our application and a
.git suffix. This is by no means mandatory, but it will help us to find our git repositories later on.
git init --bare app.git
Now, for ease the management of our git directories, we can bind our git subdirectory to
/git. As root, test it with:
mkdir /git mount --bind /path/to/git/dir /git
If it work properly, you can add the following line to your
/etc/fstab, that you should change accordingly to suit your system:
/path/to/git/dir /git ext4 bind 0 0
Next, in our development machine (The one that have the source code of the application) we will initialize our git repository.
cd /path/to/app git init git add . git commit -m "Initial commit"
If you receive any errors asking for your name or email, just follow the instructions on the screen and try again.
Note that if you plan to upload your source code to public repositories such as github you shouldn’t commit files that contain sensitive information, such as passwords or system routes. Nevertheless, as we are in a controlled environment it’s ok to add just everything.
Our first push
You can test now to push the entire source code from our development machine to our git repository (In this case our Raspberry Pi).
git remote add origin user@iporhostname:/git/app.git git push origin master
You only need to do this step one time. Subsequently you can just issue
git push to push the changes to the remote repository.
We should now be able to clone the repository in any other machine executing the following command.
git clone user@iporhostname:/git/app.git
Deploying the application
If you followed the instructions of the previous post you should have a working virtual environment, that contains a directory
testie with the django application. If we want to update the app with git, we have to somehow redirect the working directory of our git repository to
Also, you have to make sure that
testie is writable by the user that handles git management in the server. I would recommend making a backup of
testie and delete it to start fresh and create an empty directory.
Then, we should go to
/path/to/git/app.git/hooks and create a new file,
post-update, that contains the following:
GIT_WORK_TREE=/path/to/virtualenv/testie git checkout -f master
Give it execution permissions
chmod +x post-update
And when you push changes from your development machine, these should be reflected in the deployment directory. Beware, however that all the changes that you commit in the master branch will be reflected, so you should learn to work with auxiliary branches and just merge them into master when you really want to do that.