How do I roll back a bad build?

Our scripts take a back-up the instant before taking the site offline, then check out/clone the latest code into a *new* directory and update a symbolic link to point to that new code. So rolling back means simply drop database, restore from back-up and re-point symbolic link to the previous build, and you’re right back where you were before the build happened. Also, if Jenkins detects any problems with the current build it will roll back to the previous known good build automatically.

To roll back from the command line manually you'll need to execute a string of commands something like this:

# Move to application directory.
cd /var/www/live.myproject.mybranch/www
# Drop bad database and create a new, blank one.
drush sql-query "DROP DATABASE mydatabase"
drush sql-query "CREATE DATABASE mydatabase"
# Deflate the backup of the last successful build.
sudo gzip -d /home/jenkins/dbbackups/myproject_prior_to_build_XX.sql.gz
# Pipe the database dump into the blank database you created.
sudo drush sql-cli < /home/jenkins/dbbackups/myproject_prior_to_build_XX.sql
# Update the symbolic link to point back to the correct build.
sudo ln -s /var/www/myproject_mybranch_build_XX /var/www/live.myproject.mybranch

Note, XX is the number of the previous known good build. Obviously, the values in the above commands will be subject to change, but you should be able to substitute the appropriate values for your specific project, branch and build in to sucessfully roll back.