Linux – How to automate website deployments to production including minifying CSS/JS

deploymentfabriclinuxrsyncssh

Currently I'm deploying my PHP website from staging to production using the following manual process:

  • Minify CSS and JavaScript files using online YUI compressor tool.
  • Move the original CSS and JavaScript files into another directory (to back them up) then replace them with the minified CSS and JavaScript CSS files.
  • Start copying all the files from my staging server to my Windows PC.
  • Delete staging .htaccess and index files so they don't get copied to the production server (these files have specific stuff for the staging environment).
  • Open WinSCP and go to the web root on the production server then switch out the main index file for an 'updates in progress' one so if any visitors come to the site then they get a message saying it's down temporarily.
  • Then copy from my PC to the production web server using WinSCP (this overwrites all existing files on the server) and takes about 4 minutes seeing there's lots of files.
  • Log into the production server with SSH and run about 5 commands to set the proper permissions for various writable directories and make sure everything is still under www-root group. I think the upload process with SSH changes all the existing permissions.
  • Switch out the 'updates in progress' index file back to normal index.

All up this process is pretty convoluted and takes about 5-10mins and I have to do it each time I make changes to the website so it has become a chore.

Now is there any way to automate this process using some scripting tool? Or is there a nice deployment tool that people use that can do all this? My staging and production machines are Ubuntu 12.04 server based so potentially I could use bash scripting to do some of the work.

Some improvements I can see that could be made so far would be:

  • Add some code to my website so it detects if it's in Staging or Production then uses the original or minified CSS/JavaScript files depending on the environment. This will save me swapping the files around manually.
  • Use the commandline YUI compressor and run that from a script file when needed.
  • Use something like rsync so it only copies the changed files from my staging server to the production server not overwriting every file

One company I worked for used the Fabric command line tool for automating the deployments and doing this kind of thing. However is this a good option or is there a better tool out there?

Best Answer

Fabric would be ideal for this kind of task. If you're familiar with python you have a lot of flexibility with what you can do.

Capistrano is another option that might be useful, I have used it for Rails deployments and had no issues there. Allows you to easily run shell commands on remote hosts.

Another option might be to use Ant (python way with fabric is much nicer imo).

Are you using source control like git? You could add some of these tasks as post-commit hooks when you push it to your "production" branch.

Rsync would be the quickest and safest option. Make sure you exclude any files which should not be in production (backup files, vim .swp files, etc).