Hi,

In this article we will discuss about Continuous Integration. What it is? Why to use it? and how to integrate the this model on OpenShift.

While working on a code change, a developer takes a copy of the current code base on which to work. As other developers submit changed code to the source code repository, this copy gradually does not reflect the repository code. There may be addition/removal of new/old libraries and resources, that the developer may not be aware of and can result in potential conflicts.

The longer a branch of code remains checked out, the greater the risk of multiple integration conflicts and failures becomes when it is reintegrated into the main line. When developers submit code to the repository they must first update their code to reflect the changes in the repository since they took their copy. The more changes the repository contains, the more work developers must do before submitting their own changes. In a worst-case scenario, developers may have to discard their changes and completely redo the work.

Continuous integration involves integrating early and often, so as to avoid above issues. The practice aims to reduce rework and thus reduce cost and time. With CI each programmer must do a complete build and run (and pass) all unit tests before submitting their work. Integration tests are usually run automatically on a CI server when it detects a new commit.

Lets see how we can use jenkins on Openshift :

Step-1 :

We will create an application using jbossas-7 web cartridge with jenkins enabled. This will eventually create two applications in your OpenShift account, one as jenkins and one as your app.

$ rhc app create -a $application name -t jbossas-7 –enable-jenkins

for example :

..
[userone@userone OpenShift]$ rhc app create -a testjenkins -t jbossas-7 --enable-jenkins
Password: ********

Creating application: jenkins in userone
Now your new domain name is being propagated worldwide (this might take a minute)...
Confirming application 'jenkins' is available:  Success!

jenkins published:  http://jenkins-userone.rhcloud.com/
git url:  ssh://0b514430c6c24688aa0475d34cfe8a95@jenkins-userone.rhcloud.com/~/git/jenkins.git/

Jenkins created successfully.  Please make note of these credentials:

   User: admin
   Password: CgyLzMVUzVg3

Note:  You can change your password at: https://jenkins-userone.rhcloud.com/me/configure

Creating application: testjenkins in userone
Now your new domain name is being propagated worldwide (this might take a minute)...
Warning: Permanently added the RSA host key for IP address '23.22.145.126' to the list of known hosts.
Now embedding the jenkins client into 'testjenkins'...
Confirming application 'testjenkins' is available:  Success!

testjenkins published:  http://testjenkins-userone.rhcloud.com/
git url:  ssh://2483a237b65e44cf932d1e6ecc49f047@testjenkins-userone.rhcloud.com/~/git/testjenkins.git/
Successfully created application: testjenkins
..

Note : Make sure to save the credentials for Jenkins server.

Step-2 :

Log in to Jenkins console by navigating to : https://jenkins-yourdomain name.rhcloud.com/

You will see a build with your application name.

jenkins_build

Step-3 :

Now open a terminal on your local machine and navigate to the directory from where you ran Step-1. There will be a directory with your application name.

$ cd testjenkins/deployments

Now put a sample application (war/ear) inside it. You can also put an exploded war/ear but just make sure to create an empty file as “yourapplication.war.dodeploy”.

for eg :

..
[userone@userone deployments]$ pwd
/home/userone/Studies/OpenShift/testjenkins/deployments
[userone@userone deployments]$ ls -ltra
total 12
-rw-rw-r--. 1 userone userone    0 Jul 21 12:47 .gitkeep
drwxr-xr-x. 6 userone userone 4096 Jul 21 12:47 ..
-rw-rw-r--. 1 userone userone    0 Jul 21 13:05 PostDataDemo.war.dodeploy
drwxrwxr-x. 3 userone userone 4096 Jul 21 13:05 .
drwxrwxr-x. 4 userone userone 4096 Jul 21 13:07 PostDataDemo.war
..

Step-4 :

Now push the changes to OpenShift repository :

$ cd $yourappdir

$ git add .

$ git commit -a -m “my first commit”

$ git push

Once we push the changes you will see a build is getting scheduled on Jenkins server and you can track it from Jenkis console (mentioned in Step-2)

for eg:

..
[userone@userone testjenkins]$ git add .


[userone@userone testjenkins]$ git commit -a -m "unexploded commit"
[master e08e8bc] unexploded commit
 6 files changed, 49 insertions(+), 0 deletions(-)
 delete mode 100644 deployments/PostDataDemo.war
 create mode 100644 deployments/PostDataDemo.war.dodeploy
 create mode 100644 deployments/PostDataDemo.war/META-INF/MANIFEST.MF
 create mode 100644 deployments/PostDataDemo.war/WEB-INF/web.xml
 create mode 100644 deployments/PostDataDemo.war/index.jsp
 create mode 100644 deployments/PostDataDemo.war/logout.jsp
 create mode 100644 deployments/PostDataDemo.war/test.jsp


[userone@userone testjenkins]$ git push
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (11/11), 1.25 KiB, done.
Total 11 (delta 1), reused 0 (delta 0)
remote: Executing Jenkins build.
remote: 
remote: You can track your build at https://jenkins-userone.rhcloud.com/job/testjenkins-build
remote: 
remote: Waiting for build to schedule....Done
remote: Waiting for job to complete.........................................................Done
remote: SUCCESS
remote: New build has been deployed.
To ssh://2483a237b65e44cf932d1e6ecc49f047@testjenkins-userone.rhcloud.com/~/git/testjenkins.git/
   8e4fe25..e08e8bc  master -> master
..

From console you can see :

The build is successful.

Step-5 :

Now try to access your application. Just make sure if you have given a context root for your application you need to add it in front of your openshift app url. for eg :

http://testjenkins-userone.rhcloud.com/PostDataDemo/

where “PostDataDemo” was context root of my application.

So from here you can make changes in any part of your code and just follow step-4. If by any chance there are any issues in code or build fails.. need not to worry as when build fails the content will not be deployed and hence won’t affect your running application.

In case of any issue, you can tail jenkins or application logs, using below command :

$ rhc app tail -a jenkins

$ rhc app tail -a $yourapplication name

Also you can check Jenkins build console output, by logging in Jenkins server from browser.

You can always tweak in your Jenkins configuration to have added functionalities. 🙂

Regards,
MiddlewareMagic Team 🙂

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.