How to deploy dynamic sites with git

February 14, 2013 | Comments
Tags: HowTo Tips


Automation is everything. The more you can get to occur without any effort from you the more time you have available to do other more important work (and there is less chance of an important step being missed due to manual error).

So the other day I set myself the task of building a git hook script that would automatically update a Dropwizard application simply by means of a git push. Yes, this is the logical next step from the static site deployment that I covered in an earlier article. At this point you will probably want to refer to that article so that you have the necessary local configuration in place.

Assuming that you've followed the instructions there and you've got it all set up for static content. Here's what you need to put in the .git/hooks/post-receive script so that you get the same effect for, say, a Dropwizard project:

#!/bin/bash
@echo off

# This script will live in git repo as .git/hooks/post-receive
targetdir=/var/git/ExampleSite

# Target/working directory
cd $targetdir

# Stop the existing service (this may take some time so we do it before the Maven build)
echo Performing a soft kill of the existing process
pkill -f example-site-develop-SNAPSHOT

# Check out the local copy of the git repo
echo "Check out local copy"
export GIT_WORK_TREE=/var/git/ExampleSite
export GIT_DIR=/var/git/ExampleSite/.git
cd $GIT_WORK_TREE
git checkout -f

# Build with Maven
echo Maven build the new code
mvn clean package

# Start the new background process
echo Starting the new process under nohup
nohup java -jar target/example-site-develop-SNAPSHOT.jar server example.yml > /var/log/example/example-log.log 2>&1 &

echo Complete. Verify operation by visiting http://example.org/

The above script is a bare bones version and you'll probably want to make it more robust for a production server. However, we'll just plough ahead assuming that you're working on a test or development box that doesn't need to maintain 24/7 uptime. As an aside, you'll notice that the Maven version is "develop-SNAPSHOT" this is quite a handy way to represent the current development snapshot in the "master-develop" branching strategy.

On your local machine make some changes that would be visible, then issue this after the commit:

$ git push production develop:master

This will cause the following behaviour

Obviously there are a lot of improvements that could be made to the script, but this should be enough to get you started and productive.

Happy deploying!

Tweet

Related Posts

Useful? Consider a small donation: 1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH

Got an opinion? Comments are welcome!

Want more? Take a look at the following:

How to deploy static sites with git
How to recover your bitcoins from a failed hard drive
How to build a Builder
Simple Scaffolding
An nginx configuration file for Dropwizard with static content
A .gitignore file for Intellij and Eclipse with Maven
How to recover lost bitcoins from an Android wallet
7 refactorings I couldn't live without
9 top tips to understand new code
Avoiding duplicate test results with Hudson
How to create a deterministic JAR
Preventing Dependency Chain Attacks in Maven
Dropwizard with Ember Data demo
Dropwizard with OpenID
How to include Markdown with Freemarker in Dropwizard
MultiBit Merchant: Implementing HMAC authentication in Dropwizard
How to implement a RuntimeExceptionMapper for Dropwizard
How to deploy a Dropwizard project to Heroku
How to accept bitcoins on your blog with no code

comments powered by Disqus