How to Install Jenkins CI on Cloud Foundry for Continuous Delivery

by Aliaksei MarydashviliJune 18, 2014
With real-life examples, this tutorial explains how to set up infrastructure, a remote system, an RVM, as well as configure Jenkins and MySQL.

Continuous integration (CI) allows for pushing regular enhancements and bug fixes to your application in an easy, fast, and safe way. When using this practice, every time a developer commits a change, the software has to pass a number of tests. This guarantees that every new release is safe and bug-free. Ultimately, continuous integration can help to automate the entire software delivery process.

Software delivery process based on the continuous integration strategy (Image credit)

In this blog post, we will explain in detail how to set up continuous delivery for a Ruby on Rails (RoR) application running on the Cloud Foundry PaaS with Jenkins CI, a popular continuous integration tool.

 

Infrastructure and setup

For this tutorial, we used a simple RoR application with MySQL as a database. Then, we pushed the code to a public GitHub repository. The local workstation had the following specs: Ubuntu 13.10 x86_64 with MySQL Server 5.5.35. The remote virtual machine ran Ubuntu 10.04 x86_64 with Cloud Foundry v2 (5.4.5), Jenkins 1.550, and a MySQL client (5.1.73).

Pushing a Jenkins CI app to Cloud Foundry

So, let’s begin.

 

Setting up a remote system

Before you start, install Openssh-server (optional).

Then, install the latest stable Git from a PPA (Personal Package Archive), since the default Git version (1.7.0.4 for Ubuntu 10.04) is a bit of outdated.

Configure Git.

Configure iptables. Under this step, you also need to:

  • enable an outbound connection on the remote machine to access the database on the workstation
  • save the updated iptables rules to a file
  • make sure the rules are restored at every boot

Install RVM pre-requisites under Jenkins on the remote machine.

Install Cloud Foundry. Under this step, proceed with the following instructions:

  • You can use the Cloud Foundry v2 Nise Installer.
  • Then, restart your devbox.
  • Launch Cloud Foundry.
  • After this command, wait a couple of minutes to allow all the processes to start.
  • Create an organization and space in Cloud Foundry, please follow the same order of commands.
  • Finally, add the .manifest.yml file for the application to your RoR project directory (see the “Examples” section).

Now, it’s time to install Jenkins. For this purpose, follow the next steps:

  • Install openjdk-6-jre and openjdk-6-jdk
  • Install the latest version of Jenkins
  • Install a MySQL client

 

Setting up an RVM

Switch to the jenkins account.

Install RVM for the jenkins user.

Important: DO NOT install RVM under the same account where you installed Cloud Foundry to avoid conflicting with RBenv.

Install Ruby 1.9.3 or 2.x.0 according to your project settings.

Create the cf2 gemset.

Download and install the Cloud Foundry CLI from this GitHub repo.

Note: Currently, the latest version of Cloud Foundry CLI utility is v6.1.2. This may change in the future.

Generate a new SSH key and add it to GitHub.

Copy the public key to the appropriate settings tab of your GitHub account.

Finally, verify that everything worked out as expected.

 

Configuring Jenkins CI

First, you need to add environment variables to Jenkins. To do this, go to: Manage Jenkins -> Configure System -> Global properties/Environment variables and fill out the following fields:

  • login: admin (provided in the Cloud Foundry installation tutorial, see the “References” section)
  • org: altoros (created previously in the Install Cloud Foundry step)
  • password: c1oudc0w (provided by in the Cloud Foundry installation tutorial, see the “References” section)
  • space: development (created previously in the Install Cloud Foundry step)
  • target: http://api.Address.xip.io (provided in the Cloud Foundry installation tutorial, see the “References” section)

Install the following Jenkins plugins: Git Client, GitHub, and Rake.

Configure Jenkins security by adding accounts with appropriate permissions.

Create a new Jenkins project. To do this, fill out the fields related to the GitHub project. Then, enter build steps into the Execute shell field (see the “Examples section”).

Add config files to the JENKINS_HOME/shared folder.

Since both Cloud Foundry and Jenkins were installed on the same machine in our setup, we had to assign free port 8081 to Jenkins to avoid conflict with Cloud Foundry. Edit the following line in /etc/default/jenkins.

 

MySQL configuration on a local machine

Enable an inbound connection on the workstation.

Create a new account in the MySQL database and grant necessary permissions to a new user to access the project database remotely.

 

Examples

Below, you will find a sample of manifest.yml.

Important: It is necessary to provide the DATABASE_URL variable in the manifest file; otherwise, the deployment will fail. The full list of available environmental variables can be found in official docs.

Jenkins build steps from Execute shell are as shown below.

We hope, this tutorial will help you to provide continuous integration on Cloud Foundry using Jenkins. If you have anything to add, just let us know in the comments.

 

Further reading

 

About the author

Aliaksei Marydashvili is a Cloud Foundry engineer at Altoros. Since 2005, he has acquired broad background in Java and Ruby on Rails. Currently, Aliaksei is improving experience with these technologies on Cloud Foundry, bringing his R&D expertise to integrate solutions around the PaaS and optimize software delivery processes.


This post was written by Aliaksei Marydashvili and edited by Alex Khizhniak.