Good old Ant to the rescue
published: 2010-10-28
Picking up the work on this, the next version of my web page, I found a real
gem in the root of the source tree. One of those pieces of code that I've
almost organically developed with ideas and concepts over some years.
It was the build.xml file for
Ant.
So, let me make it perfectly clear - there nothing wrong with good old Ant. It's really not bad at all. Actually, it's a really good tool.
In this project I needed simple deployment, but I didn't want any IDE-bound solution or having to use a GUI tool for SCP/FTP.
The concat task in Ant provides a way to dynamically create a
script in the build file. The script can be set as executable, invoked and
finally deleted before next run. This way the project is kept clean from
script dependencies and everything is neatly packed into the buildfile.
Here are the concat, chmod, execute and delete parts of my simplified deploy task:
<target name="deploy">
<property name="deploy.file" value="deploy.sh" />
<concat destfile="${deploy.file}">
...
</concat>
<chmod file="${deploy.file}" perm="+x" />
<exec executable="sh">
<arg value="./${deploy.file}" />
</exec>
<delete file="${deploy.file}" />
</target>
To do the actual deployment I put together a script that uses tar
over ssh to pipe my built project to the hosting server. The
artifact in the ${build.dir} is a clean copy of my web in a
folder convieniently named: www.studiomediatech.com-LATEST.
...
#!/bin/sh
cd ${build.dir}
tar -zcf - ./ | ssh ${deploy.user}@${deploy.host} \
"cd ${deploy.dir}; \
tar -zxf -; \
test -d ${ant.project.name} && \
rm -rf ${ant.project.name}-PREVIOUS; \
mv -f ${ant.project.name} ${ant.project.name}-PREVIOUS; \
mv -f ${ant.project.name}-LATEST ${ant.project.name}"
exit
...
The script invokes a cleanup and some renaming command on the server, over
ssh. Any old previous backup is removed, the currently deployed
site is moved and renamed with a -PREVIOUS suffix and finally the
piped -LATEST release is renamed, to take the place of the actual
running web site. It's pretty simple, and it works on any machine with a
decent shell.
Being spurred by the success I also implemented a similar setup for a
revert-task. A simple and quick way to switch from the newly
deployed web back to the previous one.
Once again piping the commands for the remote shell over ssh
makes it simple to implement. For brevity I'll only show the script-part, but
the task-definition is the same as above.
...
#!/bin/sh
ssh ${deploy.user}@${deploy.host} \
"cd ${deploy.dir}; \
test -d ${ant.project.name}-PREVIOUS && \
rm -rf ${ant.project.name}; \
mv -f ${ant.project.name}-PREVIOUS ${ant.project.name};"
exit
...
So calling ant revert, switches the release previous release,
tagged with -PREVIOUS, back to being the current web site. The
release reverted from is also deleted, since it exists locally, and probably
needs fixing anyway.
Ant has proven it's worth with me once again, and I hope you can find it usefull too. Good luck!
blog comments powered by Disqus