Wiki source code of Remote deployment with Ant and ssh
                  Version 1.1 by Pascal Robert on 2008/01/25 21:36
              
      Show last authors
| author | version | line-number | content | 
|---|---|---|---|
| 1 | You can do many things with Ant, one of them is the ability to use tools like //[[tar>>http://ant.apache.org/manual/CoreTasks/tar.html]]//, //[[gzip>>http://ant.apache.org/manual/CoreTasks/pack.html]]//, //[[FTP>>http://ant.apache.org/manual/OptionalTasks/ftp.html]]// and //[[scp>>http://ant.apache.org/manual/OptionalTasks/scp.html]]// for remote deployment. At work, we use a shell script on our servers to deploy our apps, and we are using //rsync// to send the applications to the server before calling the shell script. Would be great to actually do this in one step ? It's quite easy!. | ||
| 2 | |||
| 3 | First, you need to install [[JSch>>http://sourceforge.net/projects/jsch/]]. We use the //jsch-0.1.29// release, we didn't try a later release. Copy the JAR into ///Developer/Java/Ant/lib///. | ||
| 4 | |||
| 5 | Next, you need to add this JAR to the Ant lib list inside Eclipse. Open Eclipse's preferences (Eclipse->Preferences), open the //Tab// triangle and click on //Runtime//. Select //Ant Home Entries// and click on //Add External JARs...// Browse up to ///Developer/Java/Ant/lib/// and select //jsch-0.X.XX.jar//. The JAR should now be part of the list of JARs available for Ant. | ||
| 6 | |||
| 7 | {{warning}} | ||
| 8 | |||
| 9 | If you use multiple workspaces, you have to add the JAR to each of your workspaces to the _Ant Home Entries_ list. | ||
| 10 | |||
| 11 | {{/warning}} | ||
| 12 | |||
| 13 | Ok, now it's time to actually create a Ant task for deployment. You can add a property like this to //build.properties//: | ||
| 14 | |||
| 15 | {{code}} | ||
| 16 | |||
| 17 | servers.production=my.server.address | ||
| 18 | |||
| 19 | {{/code}} | ||
| 20 | |||
| 21 | And you add this to your //build.xml// file. | ||
| 22 | |||
| 23 | {{code}} | ||
| 24 | |||
| 25 | <target depends="setProps,init.install,build.woapp" name="deployProduction"> | ||
| 26 | <echo message="Starting file transfer to ${user.name}@${servers.production}" /> | ||
| 27 | <exec dir="." executable="rsync" os="Mac OS X" failonerror="true"> | ||
| 28 | <arg value="-aog" /> | ||
| 29 | <arg value="-e ssh" /> | ||
| 30 | <arg value="${dest.dir}/${project.name}.woa" /> | ||
| 31 | <arg value="${user.name}@${servers.production}:~" /> | ||
| 32 | </exec> | ||
| 33 | <sshexec command="myshellscriptfordeployment -d ${project.name}.woa" host="${servers.production}" keyfile="${user.home}/.ssh/id_rsa" passphrase="" username="${user.name}"/> | ||
| 34 | </target> | ||
| 35 | |||
| 36 | {{/code}} | ||
| 37 | |||
| 38 | Wait a minute... Maybe //Ant// will complain because it can't send the password to the remote server. How to fix this ? By creating a SSH public key if you don't already have one. | ||
| 39 | |||
| 40 | First, check if you already have a public key on your computer: | ||
| 41 | |||
| 42 | {{code}} | ||
| 43 | |||
| 44 | $ ls -al ~/.ssh/id_rsa.pub | ||
| 45 | -rw-r--r-- 1 monuser monuser 230 Dec 5 2006 .ssh/id_rsa.pub | ||
| 46 | |||
| 47 | {{/code}} | ||
| 48 | |||
| 49 | No //id//rsa.pub// file ? Create one~:// | ||
| 50 | |||
| 51 | {{code}} | ||
| 52 | |||
| 53 | $ ssh-keygen -t rsa | ||
| 54 | |||
| 55 | {{/code}} | ||
| 56 | |||
| 57 | Now copy your SSH public key to your remote server: | ||
| 58 | |||
| 59 | {{code}} | ||
| 60 | |||
| 61 | $ scp ~/.ssh/id_rsa.pub mynuser@remoteserver:myuser.pub | ||
| 62 | |||
| 63 | {{/code}} | ||
| 64 | |||
| 65 | and put your public key in the //authorized//keys// file~:// | ||
| 66 | |||
| 67 | {{code}} | ||
| 68 | |||
| 69 | remoteserver$ cat ~/monuser.pub >> ~/.ssh/authorized_keys | ||
| 70 | |||
| 71 | {{/code}} | ||
| 72 | |||
| 73 | Now, 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. | 
