Simple Web Applications Deployment via Git

Git is not only great version control tool,  but can be easily used for web application deployment to testing or production environmenst.  For more complex projects some continuous integration (CI), tools/services can be more appropriate  (like Jenkins), but for smaller project we can do just fine with Git , SSH and simple script installed as git hook. Below is the scenario I’m using for one Python Flask web application.

Flask application is deployed on Debian server (running in VM), in uwsgi container behind nginx frontend (nginx is also serving static files).   We have two VMs –  one production and one testing. If necessary production instance can be easily scaled horizontally  by adding more uwsgi instances behind nginx frontend.

Git Flow

We have two long running branches in Git:
devel – for latest development version
master – for production version

Developers work on feature branches, which they can run locally (with Flask embedded WSGI server),  when  features are ready, they are merged to devel branch and  devel branch is then pushed to test instance, where all new features are tested by users.  When we are fine with new functionality,  devel branch is merged to master branch and master branch is pushed to production instance.

Server Setup

This is setup for testing server,  same is for production server,  only that branch for production is master.

1. Install Git

2. Create bare repo

3. Add remote

4. Fetch branch and check it out

Now we are ready to run the Flask application, just need an usual setup for Flask:

Server setup – specific for Flask and Python

1. Install base dependencies

2. Create  uwsgi configuration for your application /etc/uwsgi/apps-available/app.ini

And symlink it to /etc/uwsgi/apps-enabled.  Indeed for production configuration you might need other parameters, especially workers and threads.

3. Create site configuration for nginx – /etc/nginx/sites-available/app-nginx

Remove default nginx site and symlink this to /etc/nginx/sites-enabled.

4. Install necessary python dependencies for you application

5. Restart servers

 Server Setup – Git hook

Create following file in /var/repo/app.git/hooks/post-receive

This script checks if correct branch is pushed. Eventually it can be easily extended to deploy different branches to different locations.

Client setup

Assuming you already have cloned devel branch to your computer and worked on it.

1. Assure that your SSH public key is set on server

2. Add remote repository to your cloned repo

3. When you finished you can just push changes to test server

and they will be automatically deployed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">