How to Build and Deploy OpenShift Java Projects using Travis CI - Archived

Travis CI OpenShift Integration

Recently, Travis CI announced support for deploying OpenShift applications from Travis builds. Travis-CI.org is a free hosted continuous integration server for open source applications. Commercial non-opensource project developers can use the Travis CI commercial offering. This blog shows how developers can use Travis-CI.org to build and deploy OpenShift Java applications. These instructions also apply to commercial offerings Travis-CI.com

Please note that out-of-the-box OpenShift provides support for Jenkins based deployments. You can refer to my blog on Jenkins for more information.

Prerequisite

Before we can start building the application, we’ll have to do a few setup tasks :

  1. Sign up for an OpenShift Account. It is completely free and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.

  2. Install the rhc client tool on your machine. The rhc is a ruby gem so you need to have ruby 1.8.7 or above on your machine. To install rhc, just typesudo gem install rhc. If you already have one, make sure it is the latest one. To update your rhc, execute the command shown below.sudo gem update rhc. For additional assistance setting up the rhc command-line tool, see the following page: https://openshift.redhat.com/community/developers/rhc-client-tools-install.

  3. Setup your OpenShift account using rhc setup command. This command will help you create a namespace and upload your ssh keys to the OpenShift server.

Step 1 : Create a Tomcat 7 Application

We will start by creating an OpenShift Apache Tomcat 7 application. The application is a simple forum application built using the Spring framework and uses the PostgreSQL 9.2 database. OpenShift recently added support for PostgreSQL 9.2.

$ rhc app create forumapp tomcat-7 postgresql-9.2

This will create an application container for us, called a gear, and setup all of the required SELinux policies and cgroup configuration. OpenShift will also setup a private git repository for you and clone the repository to your local system. Finally, OpenShift will propagate the DNS to the outside world. The application will be accessible at http://forumapp-{domain-name}.rhcloud.com/. Please replace {domain-name} with your own unique domain name. This gear will already have Java 7 installed on it.

Step 2 : Pull the Source Code from Github

After OpenShift has finished creating the app, we will pull the source code from my public github repository.

$ git rm -rf src/ pom.xml 
$ git commit -am "deleted project"
$ git remote rename origin openshift
$ git remote add upstream -m master https://github.com/shekhargulati/forumapp.git
$ git pull -s recursive -X theirs upstream master

In the git commands above, we first deleted the default files created by OpenShift , then renamed the origin remote to openshift, and finally we added the upstream remote to the forumapp git repository and pulled the source code to our local machine. We renamed the remote so that when we push the source code, it should be pushed to github rather than to the OpenShift application gear.

Step 3: Push the Source Code to OpenShift

The normal way to deploy an application to OpenShift is to push the source code to your OpenShift gear running your application. OpenShift will first build the project and then deploy the application. Because we have renamed the remote repository to openshift, we will use the commands shown below.

$ git push openshift master

Step 4 : Sign up for a Travis CI Account

Rather than using OpenShift to build the project, we will use Travis to build the project and deploy it to our OpenShift application gear. Sign up for Travis at travis-ci using your Github account. Enter the details as shown below and press “Create repository” button.

Step 5 : Create .travis.yml

Create a new file .travis.yml in your application folder and add the following lines to it.

language: java
script: mvn package -Popenshift
jdk:
  - oraclejdk7
  - openjdk7
  - openjdk6

The lines mentioned above inform Travis that this is a Java project and it should use “mvn package -Popenshift” command to build the project. Finally, the project should be built on three JDK versions — oraclejdk7 , openjdk7, and openjdk6.

Step 6 : Create a Public Github Project

Travis-CI.org can build your project only when it is a public Github project. Create a new Github project as shown below.

New Github project

Step 7 : Activate Travis for the Newly Created Project

Log into Travis-CI.org and then go to https://travis-ci.org/profile. Press the “Sync now” button and it will sync with your github account. Next you will see a project as shown below.

To activate Travis, click the ‘off’ switch and change the state to ON. This will setup an action hook in your github repository. So every time you push the source code to github, the action hook will be invoked which will start the build and deploy the application to OpenShift.

Step 8 : Push the Source Code to the Github Repository

To test that Travis is successfully setup, we will push all the source code to github, which should invoke the Travis build. Execute the commands shown below changing the git@github.com:shekhargulati/forumapp-openshift-quickstart.git to your github project.

$ git remote add origin git@github.com:shekhargulati/forumapp-openshift-quickstart.git
$ git push -u origin master

After the code has been pushed, go to https://travis-ci.org/ and you will see your project build in progress as shown below.

Step 9 : Deploying to OpenShift from Travis

In the last step, we successfully tested that each time we push the code to Github, Travis-CI would build the project for us. But, we have not setup Travis to deploy the successful build to OpenShift.

To configure Travis to deploy successful builds to OpenShift install the travis ci gem on your local machine, you need to have ruby installed on your machine.

$ gem install travis

Setup OpenShift Account with Travis

Use the travis setup command to allow travis to use OpenShift. It will ask you a set of questions required to build and deploy an OpenShift application.

$ travis setup openshift
 
OpenShift user: <OpenShift Login>
OpenShift password: ******
OpenShift application name: |forumapp-openshift-quickstart| forumapp
OpenShift domain: <OpenShift Domain Name>
Deploy only from shekhargulati/forumapp-openshift-quickstart? |yes| yes
Encrypt password? |yes| yes

Commit .travis.yml and push to github

Commit the changes and push them to OpenShift

$ git commit -am "setup travis openshift"
# git push

Now each time you push the changes to github, Travis will first run your build and then deploy the application to OpenShift.

Step 10 : View your Build on the Travis Web Console

After making all the required changes, you can view your build running on travis. Go to https://travis-ci.org/[github-username]/[application-name]. The build of our forumapp is at https://travis-ci.org/shekhargulati/forumapp-openshift-quickstart.

Step 11 : Adding Build Status to the README.md

One of the cool features of travis is that you can view the build status from your github repository. Open the README.md file and add the following lines. Replace GITHUB_USERNAME with your github username and PROJECT_NAME with the name of your github project.

[![Build Status](https://travis-ci.org/[GITHUB_USERNAME]/[PROJECT_NAME].png)](https://travis-ci.org/[GITHUB_USERNAME]/[PROJECT_NAME])

Commit the README.md and push the changes.

$ git commit -am "updated README.md with build status"
$ git push

On your project github page you will see as shown below.

Conclusion

In this blog post, we looked at how easy it is to add Travis CI support to an OpenShift Java application.

What’s Next?

Categories
Java, OpenShift Online
Tags
,
Comments are closed.