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/example.com/ ${remote.user}@${remote.address}:/var/www/html/sites/ />
</exec>
</target>

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/id_rsa.pub

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/id_rsa.pub remote_user_name@remoteserver:remote_user_name.pub

remoteserver$ cat ~/remote_user_name.pub >> ~/.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.

3 comments:

greg said...

Thanks for this...works 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...

Hi,

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.

cheers,
Alastair

Ariel17 said...

Hi all,

You also can use 'ssh-copy-id' command http://linux.die.net/man/1/ssh-copy-id It makes all necessary tasks over keys for you (copying and setting correct properties).

Regards.