Overview

The final component of the Continuous Integration and Deployment process using Capistrano and Jenkins is … Jenkins. Jenkins is an open source Continuous Integration services tool, that allows users to define a set of tasks to be run either manually, or triggered by 3rd party applications.

This page presents how to integrate Jenkins and Capistrano 3; in order to trigger Capistrano recipes defined here. The assumptions are that Capistrano and Jenkins are installed on the same server, and recipes are stored in a git repository outside the application repository.

Prepare Jenkins for Git-Based Deployment

Prior to creating jobs, a few settings must be applied to Jenkins for cloning a Git repository.

Step 1: Open the Plugin Manager

Click Manage Jenkins on the left-hand-side menu, followed by Manage Plugins on the center view.

Step 2: Select the Git Plugins

Click the Available tab, and select the following:

Step 3: Install

Click Install without Restart at the bottom of the page:

Create a Jenkins Staging Deploy Job

This job facilitates deployment of code.

Step 1: Create a New Item

Navigate back to the main Jenkins page, and click Item on the left-hand-side menu.

Step 2: Fill in the Form

Fill in the form as per below, and click OK:

Start Editing This Job

Step 1: Configure Source Code Management

Configure source code management to fetch the Capistrano Recipe repository:

Note: Add repository credentials by clicking Add below Credentials.

Step 2: Add a Build Step

Scroll to the bottom of the page and select the following:

Input:

cd $WORKSPACE/citest/ && cap staging deploy

In the following field:

Step 4: Click Save

Test Your Staging Deployment Job

By clicking back to Dashboard, you should now see the newly created job:

Note: The image above depicts a last successful build, with a couple of failed builds. A new task would not display the two icons and time indicators.

Click staging-deploy, followed by Build Now. Progress can be checked here:

Console output can be viewed by clicking the link on the image above, followed by Console Output.

Create a Jenkins Staging Test Job

This job facilitates deployment and execution of tests.

Once a task has been created, it can be cloned and parameters changed.

Click New Item, and select the following:

Click OK, and add/edit the shell command to execute:

cd $WORKSPACE/citest/ && cap staging deploy:test

Click Save.

Create a Jenkins Staging Rollback Job

This facilitates the rollback of deployed code.

Similarly to above, clone a job into staging-rollback, and add the following code:

cd $WORKSPACE/citest/ && cap staging deploy:rollback

Create Jenkins Production Deploy and Rollback Jobs

This shows how to replicate the staging deployment jobs for a production environment.

Repeat the clone process for tasks production-deploy and production-rollback, and add one line for each:

cd $WORKSPACE/citest/ && cap production deploy
cd $WORKSPACE/citest/ && cap production deploy:rollback

Optional: Create a Jenkins Production / Staging Application Restart Job

Optionally, you can create jobs for restarting applications on production and staging environments.

Clone your production deploy job, and add the following:

cd $WORKSPACE/citest/ && cap production deploy:restart