I’ve been developing a bunch of small Grails apps lately, and I end up deploying them to my production box fairly frequently. This is kind of annoying because the upload speed on my home internet connection is terrible - it sometimes takes 7 or 8 minutes to upload a new WAR and during that time, my internet connection is unusable for anything else. I wanted a simple way to push new code to production without uploading a new WAR every time, and I know a few other people were looking for something similar, so here it is. It is pretty boring but it does the job. In my case, I deploy all of mine as ROOT.war, because they are always the default app for their domain. If yours is setup different, just replace ROOT.war with whatever your war should be named.

1. Check your code into Subversion (or CVS or whatever SCM of your choice - this guide assumes Subversion, however).

2. Add a build directory on your prod box (I have mine at /home/build/) - then check out individual projects under it (ie, /home/build/wiifit/).

3. In each project, add a property to application.properties for the deployment directory (ie - deploy.dir=/home/tc6/server/wiifit).

4. In grails, you can add groovy scripts to the /scripts/ directory, and then run them with grails , ie, ProdPush.groovy can be called with ‘grails prod-push’. Let’s add a script in to do the following things:

- Do a subversion checkout to get our latest changes.
- Build the war
- Copy it to our directory

This is what I came up with:

grailsHome = Ant.project.properties.“environment.GRAILS_HOME”

includeTargets << new File ( “${grailsHome}/scripts/War.groovy”)

target(‘default’: “Push a war to prod”) {
       println “svn up”.execute().text
       war()
       Ant.copy(todir:Ant.antProject.properties.‘deploy.dir’, overwrite:true) {
               fileset(dir:“.”, includes:“ROOT.war”)
       }
}

Your script can have multiple targets. If you specify a ‘default’ one, like I have above, it is what will be run if you supply no target (which is what we want).

5. Now I have a simple shell script on my server to setup the environment variables and run the build.

#!/bin/bash

export GROOVY_HOME=/usr/share/groovy
export GRAIILS_HOME=/usr/share/grails
export JAVA_HOME=/usr/java/jdk
export GWT_HOME=/usr/share/gwt

cd $1

$GRAILS_HOME/bin/grails -Dgrails.env=production prod-push ROOT.war

This script can then be re-used for all projects on the server.

6. To update our app, we simply do the following:

- Check our changes into Subversion
- SSH into our server and go to our /build/ directory
- Run the build script (ie, ./build.sh wiifit)

Our new app is live in less than a minute and we don’t kill our home internet connection :)

This solution may not be very good for high-usage, critical apps - if I were building (and leaving code) on a production app server at my day job, I’d be fired (and deserve it!) - but for small apps like I build, it is a good solution. This solution is pretty simple, but I’m working on a full deployment plugin for grails - fewer setup steps, tomcat management, etc.. If you have any suggestions or ideas, let me know.