We'll show you how a few pieces of software can help speed up development and prevent deployment issues for web projects. Continuous Integration has helped us to produce reliable and solid software that we know works before handing it over to the client.
The Guts of a Good Continuous Integration Setup
Continuous Integration is most useful when you have several developers on a single project working on the same piece of code, and you need to automatically merge all of their changes as they happen, test and deploy it to some kind of staging environment.
Build Tools (ANT/Phing)
A build tool will allow you to do those very tedious tasks that need to be run before any kind of deployment to a local/staging/production server. It could be anything from running MySQL Delta's to make sure your database is in sync with a production/staging database, to replacing environment variables in your PHP source files. Build tools are most common in languages that need to be compiled, such as Java and Objective-C/C.
From a PHP point of view, you don't need to compile PHP like you have to with a Java or Objective-C based project. A build for PHP will allow you to do things such as:
- Minify and optimise your client side assets (CSS/JS/Images) automatically using YUI Compressor or ImageOptim, so you don't have to do it server side
- Compile your SASS/LESS CSS based files
- Run your PHPUnit, QUnit and QMock tests
- Run your Selenium browser based acceptance tests
- Deploy everything to a dev/staging environment if all of the above pass with flying colours!
Basically, anything that can be run from the command line, can be run from ANT. We have chosen ANT as our build tool. This is simply because it comes pre-installed with Snow Leopard and Snow Leopard Server and it plays well with Zend Studio. Phing is perfectly adequate and, is in-fact, a much better choice for PHP projects, as it's written in PHP so you can create your own extensions/plugins quite easily.
Testing (Selenium, QUnit, PHPUnit, QMock)
Testing is critical when building large scale applications. Unit testing allows you to test individual blocks of code whilst selenium will allow you to automatically poke and prod at your website in a browser and verify the results to your hearts content. Once you write your tests, you can then execute them when you change your code to ensure that everything still meets the business requirements and expectations.
SCM (SVN/Git/Mercurial and the rest)
SCM is the preferred choice when working on your own or extremely large development teams. SCM allows you to checkout your own copy of a project and commit the changes you make at will. You can work without having to worry about overwriting some one else's files, or more importantly, wait for files to become available for editing.
Having experience with SVN and Git, we've quickly adopted Git's simplicity over SVN's complexity when dealing with things such as submodules, branching, merging and tagging (we'll go in depth with these concepts in another post).
The gist is that an SCM solution allows you to work as a group on a project, and commit your code at will without worrying about overwriting other developers data. It also allows you to keep track of changes, rollback when things go wrong and branch off and merge back when doing experimental stuff with your codebase. It also helps when playing the blame game when things go wrong! More on that next :D
Continuous Integration Server (Bamboo, Hudson and Cruise Control)
At the heart of every continuous integration setup is a continuous integration server. A continuous integration server will constantly monitor commits to your preferred SCM and run what's called a build for every commit that caused a change in code.
Having played with Husdon, Cruise Control and Bamboo; we settled for Bamboo. We made our choice based on:
- The number of extensions and plugins that were available for each tool
- How easy each new project was to set up
- How well they played with our existing tools such as JIRA, Confluence and our LDAP server
- Cost of ownership (price)
Although Bamboo is the most expensive (with the others being free), we would spend less man hours setting up new projects using the GUI and far less time integrating it with Git, JIRA, Confluence and our Jabber server (yes Bamboo does talk to us) as it comes with support and easy setup out of the box.
Continuous integration servers will alert you when a build fails. This can either be done via email, or more elaborately, by electrocuting the person who broke the build. Yes that's right, continuous integration servers can point out who broke the build, what tests failed, and what the last commit message and code was and perform whatever you like, automatically to them.
What's the point of it all?
Putting the jokes aside, continuous integration is your first and last line of defence against accidental changes to your codebase that may break your application in production. We have restricted any developers from accessing our FTP servers directly and encourage them to commit early and commit often. This prevents last minute changes to files, and ensures that developers put real thought into their code before they commit it. There's nothing more embarrassing than showing up on the screen of shame for the day!
SCM also allows developers to branch off and add features to projects without affecting anybody else. Once a feature is complete, he/she can re-integrate it into the master branch so that it's ready for the next deployment. We can also safely make changes with the confidence that once it passes through our continuous integration workflow, everything should be working as expected. We can now spend less time manually testing things that should be automated to avoid human error and more time on producing added value to projects for our clients.