A while ago Felix asked: What alternatives do we have for SVN/FTP deployment? I seconded the proposals for using Capistrano, an automated deployment solution that comes from the Ruby on Rails world. I’ve gotten it running for my cakePHP development work, specifically on the MediaTemple GridServer.
I decided I needed to get this running on a few of my applications. I’m building with cake so adding Capistrano’s deploy.rb recipe to the mix is easy, I just drop it into the /app/config directory and then edit it for that specific application and it’s servers. This recipe will deal with deploying to MediaTemple’s GridServer, which I’m doing for several applications right now.
set :application, "projectname" set :repository, "https://mysvnserver.com --username myusername --password mypassword"set :user, "serveradmin@mygridserverdomain.com"role :web, "mygridserverdomain.com"set :checkout, "export"desc "This will deploy the app" task :deploy do run "rm -rf #{shared_path}/images" run "mv /home/xxx/domains/mygridserverdomain.com/html/images #{shared_path}/images" run "mv /home/xxx/domains/mygridserverdomain.com #{shared_path}/revert_old" run "mv /home/xxx/domains/mygridserverdomain.com #{shared_path}/revert" run "rm -rf #{shared_path}/revert_old" run "svn --quiet #{checkout} #{repository} #{release_path}" run "rm -rf /home/xxx/domains/mygridserverdomain.com" run "mv #{release_path} /home/xxx/domains/mygridserverdomain.com" run "rm /home/xxx/domains/mygridserverdomain.com/html/cakeinfo.php" run "rm /home/xxx/domains/mygridserverdomain.com/html/logo-mini.gif" run "mv #{shared_path}/images /home/xxx/domains/mygridserverdomain.com/html/images" enddesc "This will roll back the deploy on this app" task :rollitback do run "rm -rf #{shared_path}/images" run "mv /home/xxx/domains/mygridserverdomain.com/html/images #{shared_path}/images" run "mv #{shared_path}/revert /home/xxx/domains/mygridserverdomain.com" run "rm -rf /home/xxx/domains/mygridserverdomain.com/html/images" run "mv #{shared_path}/images /home/xxx/domains/mygridserverdomain.com/html/images" end
In this recipe, “xxx” is my number on the grid server system and “mygridserverdomain.com” is, of course, the domain I am hosting on the grid server.
Also, a few weak points still exist in this whole plan so far. I could not find a reasonable way to work within the gridserver’s limitations, cakePHP’s need to know where it’s running from, and use Capistrano’s symlinking with auto-rollback. Thus, I am doing quite a bit of moving around of files in this script. I backup the images directory as that is the only thing this application changes within the file structure while it’s running live. I also know that I am not using the special Capistrano commands and instead just doing everything via the “run” command, but… “eh.”
To run this (I’m assuming you have capistrano running on your workstation), I open terminal:
cd /Path/to/app/config
Now I just have to run the deploy script.
cap deploy -f deploy.rb
Supposedly, I should be able to run this without the -f modifier as long as I’m in the same directory as the recipe, but it wasn’t working and I didn’t bother figuring out why not.
The first time I attempted to deploy, I was getting a timeout when I would run Capistrano without having first allowed my serveradmin account on the grid server permanent access to the uncertified ssl certificate on my SVN hosting account’s server. So I ssh’ed into my gridserver account manually and just did a quick connection to my SVN server via the command line and (p)ermanently accepted the connection to the SVN server when prompted. This created an ASCII_CERT file at ~/.subversion/svn.ssl.server. Now that this was completed I could run cap without any prompt to (t)emporarily accept, (d)eny, or (p)ermanently accept the ssl certificate’s authenticity.
This is not the approved manner of doing this for you security conscious folks, but it’s cool for me.
Now, when I run the deploy command from my workstation, it ssh’s to the server, prompts me for my password, backs up all images from the application’s webroot/images directory, backs up the app itself, exports a new version of my app from SVN and replaces the old version with it, then puts the images back where it found them.
If I ever need to roll back, I can only roll backwards one release automatically, but it’s usually enough as I don’t deploy constantly, so the last one was probably the one that worked right. I couldn’t use the auto rollback built into Capistrano, so I created one called “rollitback”.
Type “cap rollitback -f deploy.rb” in the command line and Capistrano does just that…it roll’s the application back to the version that was running just before the version currently running.
I am semi-aware of the fact that Rails has DB migrations and other trickery, but cakePHP doesn’t support most of that stuff yet AFAIK, so none of that is detailed here. If anyone wants to tell me how to make this a more powerful solution, or wants to tell me how to clean up my recipe, please do.
Sphere: Related Content