Friday, July 20, 2007

Use Ant to deploy to remote hosts

Ant is an awesome compile/package/deployment tool for Java code. But it's not limited to Java programs only. Currently, I'm working on a PHP site. We actually use ant to deploy our app. I don't really want to list the details here, but I can explain the basic steps.
1. Staging
During this step, you use ant filter feature to customize all the configuration and resources. For example, you might have different database for different environment. Developer's web server talks to Dev database, QA's web server talks to QA database, and prod's web server talks to Prod database. You use ant copy and filter feature to stage all the file from source location to a target location.
2. Packaging
Simple use ant tar and zip feature to package everything into a compressed file. You might want to add times stamp or version number to the tar file so you know which version you are deploying.
3. Deployment
To transfer the application package to a remote host, you should use ant scp task. Since we have multiple web servers, we also use ant for task which tokenize a list of host names and create a for loop so the embedded scp task will copy files to each remote host. On the remote hosts, you should have a directory for holding all the app packages. You can use sshexec to remotely create any directories.
4. Installation
You can easy write a shell script and deploy the shell script along with the application package or use ant sshexec to unpackage the application. However, I highly recommend the use of a separated shell script. Becuase you won't have to relay on ant to install your software which means you can install/rollback/rollforward your production server from anywhere.

No comments: