Remote Deployment with Ant and Rsync

Remote Deployment with Ant and Rsync - this tutorial will teach us how to deploy projects on a remote server. This is very suitable especially for those who implements Continuous Integration where you will deploy projects upon every successful build or after passing all through unit tests, code analysis, and load testing.

Ant sample buildfile

Add this to your Ant's buidlfile (e.g. build.xml)

<target name="deploy">
<exec dir="." executable="rsync" failonerror="true">
<arg line="-aog -e ssh " /var/www/html/sites/ ${remote.user}@${remote.address}:/var/www/html/sites/ />

SSH Public Key

- This will prevent Ant from sending out an error message because your password couldn' be sent.

Check Your SSH Public Key

Check the public key on your computer by running this command.

$ ls -al ~/.ssh/

Create Your SSH Public Key

Run this command if you still don't have a public key.

$ ssh-keygen -t rsa

Copy SSH Public Key

Let's build the connection between your local and remote server by copying the public key from your local server to remoteserver and saving it in the authorized_keys file.

$ scp ~/.ssh/

remoteserver$ cat ~/ >> ~/.ssh/authorized_keys

Next time that you login by SSH from your computer to the server, it will stop asking for a password and the Ant task will stop complaining.

For added security, when you create your key, use a passphrase.

Don't forget to put the passphrase into the sshexec task.

Don't use the passphrase if you put the build.xml file in CVS or SVN, because your co-workers will see your passphrase and also they won't be able to use the sshexec task unless they use the same passphrase as you.


greg said...

Thanks for fine...except if I use a key with a passphrase I don't really understand how I can pass the exec rsync task the passphrase to use with the key. You say to add it to the sshexec task... I can use an sshexec task just fine with the passphrase/key but don't get how to have the rsync task leverage that login.

Alastair said...


Useful article, thanks. One thing I noticed was that the remoteServer ~/.ssh/authorized_keys file must have '600' permissions - use:

chmod 600 ~/.ssh/authorized_keys

to do this.


Ariel17 said...

Hi all,

You also can use 'ssh-copy-id' command It makes all necessary tasks over keys for you (copying and setting correct properties).