Using Jenkins for More than Testing

Photo credit: Libreshot

Testing. I love it. It is crucial to good software development. Running those tests continually and providing quick feedback to developers is important. At work, we use Jenkins to provide our continuous integration.

Something is often overlooked. Jenkins can do a lot more than run tests.

Here are 3 cases where we use Jenkins in a way that might not fit with your normal expectations of how to leverage Jenkins

Facilitate Deployment

Imagine a client that has developed part of their own infrastructure. Now let’s suppose they haven’t automated deployment before. You want the servers you set up for this client to have a well controlled process. You bring your usual bag of tricks (in this case, Chef and Capistrano) and get everything flowing nicely.

But what happens when the client wants to update their component? Enter Jenkins (again because you are already using it for testing right?). Jenkins can do anything inside a build script. So why not create a job on Jenkins that runs some Capistrano commands? All you need to do is add the public key for your Jenkins user to the servers you are trying to manage.

Jenkins even lets you parameterize the build. You can choose which environment you are deploying to, if maintenance mode should be enabled, and which parts of the infrastructure you are updating.

Build Parameter Example

Now, anyone associated with your project can trigger a deploy. All they need is access to the deployment job on Jenkins.

Facilitate Maintenance

Do any of your clients have a training server they use during classes they teach about their own product? Do they need to reset the data in those training instances between classes? Are they currently asking you to do that as needed?

Sure, you could build something into their product’s admin interface to perform the reset. How about leveraging something you’ve already got?

Leverage a Rake task for resetting the data in the training server. Connecting the Rake task to Capistrano should be pretty straight forward. Provide a Jenkins job to allow the customer to fire off that Capistrano command. With the Jenkins job, your customer can reset their own data between classes without any intervention from you. And most importantly, anyone from their staff can be given access, no matter what their technical skill.

Automate Things

How about a demo site? Do you have a client that would like a demonstration copy of their product that they can give a potential customer access? Demo sites really need to be cleaned up on a regular basis. You want to keep that product shiny for potential customers.

You could setup something on the actual server with cron, but there are some roadblocks there. If you are doing a full database dump and restore from a known good state, then you need to make sure that all your processes are stopped and disconnected from the database. You’ll also want a maintenance message. Finally, you want to make sure everything comes back online before taking the product out of maintenance mode.

Capistrano already knows how to handle your maintenance mode and starting and stopping the application. Extending it to run the restore tasks isn’t a huge leap.

We do add a little logic to our Nginx configuration so that the scripts can make sure the site has come back online. Replace 8.8.8.8 with the external IP of your server. This addition to your Nginx configuration can allow the server to query its own site by DNS name and make sure it has come online. Then it will be sure the product is restored before disabling maintenance mode.

  if (-f $document_root/system/maintenance.html) {
    set $maint_mode 1;
  }

  if ($remote_addr = 8.8.8.8) {
    # disable maintenance mode for local requests
    set $maint_mode 0;
  }

  if ($maint_mode) {
    return 503;
  }

Once you have this working, you can set the Jenkins job to run automatically with cron syntax. You now have the benefit of Jenkins to know if the job fails, track its run success history, and be able to trigger the reset whenever you or the client needs to.

Jenkins Cron Build Trigger

Power

Jenkins can give you a simple ‘do this now’ interface for anything you would normally do on the command line. You can use it to empower a lot more than just good testing practices. Think outside the box!