How to run Grails Application with Jenkins on OpenShift

Yes, you can run Grails applications on OpenShift. Follow the steps mentioned below to deploy Grails apps via Jenkins on OpenShift.

Step 1 : Create Tomcat 7 application with Jenkins

$ rhc app-create grailsapp tomcat-7 --enable-jenkins

Step 2 : Delete template code

$ cd grailsapp
$ git rm -rf src/ pom.xml
$ git commit -am "deleted template code"

Step 3: Generate Grails app
Use grails command line or IDE to generate a Grails project.

Step 4: Copy the Grails app
Copy the source code of your grails app in the grailsapp folder. The grailsapp corresponds to OpenShift application.

Step 5: Create pre_build action hook
Create an OpenShift action hook

touch .openshift/action_hooks/pre_build
chmod +x .openshift/action_hooks/pre_build

Copy the following in pre_build hook

#!/bin/bash
# This is a simple script and will be executed on your CI system if
# available.  Otherwise it will execute while your application is stopped
# before the build step.  This script gets executed directly, so it
# could be python, php, ruby, etc.
set -x
if [ ! -d $OPENSHIFT_DATA_DIR/grails-2.3.4 ]
then
        mkdir $OPENSHIFT_DATA_DIR/.grails
        cd $OPENSHIFT_DATA_DIR
        wget http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.3.4.zip
        unzip grails-2.3.4.zip
        rm -f grails-2.3.4.zip
fi

Step 6: Create build action hook
Create an OpenShift action hook

touch .openshift/action_hooks/build
chmod +x .openshift/action_hooks/build

Copy the following in build hook

#!/bin/bash
# This is a simple script and will be executed on your CI system if
# available.  Otherwise it will execute while your application is stopped
# before the build step.  This script gets executed directly, so it
# could be python, php, ruby, etc.
set -x
export GRAILS_HOME=$OPENSHIFT_DATA_DIR/grails-2.3.4
export PATH=$GRAILS_HOME/bin:$PATH
cd $OPENSHIFT_REPO_DIR
export GRAILS_OPTS="-Xmx512m -Xms256m -XX:MaxPermSize=256m"
grails -Dgrails.work.dir=$OPENSHIFT_DATA_DIR.grails prod war

Step 7: Commit and push the changes

Commit and push the changes

$ git add .
$ git commit -am "app"
$ git push

Now watch your Jenkins build. If it fails, I guess it would be because of memory issues. Try and use bigger gear sizes. Grails is memory hungry.

Github repository with sample app code https://github.com/shekhargulati/grails-jenkins-openshift-example

Advertisements

Day 6 : Grails — Rapid JVM Web Development with Grails And OpenShift

On 6th day of 30 technologies in 30 days challenge, I have decided to learn Grails web framework. Grails is an open source web framework which uses Groovy and Java programming languages. The web applications written using Grails can be deployed in any of the existing web server like Jetty or Tomcat, etc. Grails is a JVM response to the Rails or Django web frameworks. In this blog, I will first talk how we can develop a simple web application using Grails Eclipse plugin, and then we will deploy the application to OpenShift.

Why should we care about Grails?

The two reasons why I considered learning Grails are :

  1. Rapid web development : It can help us achieve productivity benefits similar to Rails or Django but without leaving JVM and Java knowledge.
  2. Powered by proven technologies : Grails underneath uses Spring and Hibernate to power web applications. It can be considered as syntactic sugar over these technologies.Read More »