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

12 thoughts on “How to run Grails Application with Jenkins on OpenShift”

  1. Hi Shekhar,

    Some comments:
    – both chmod commands in instructions contain typo: actions_hooks should be action_hooks
    – I had 3 gears in use, Grails app, Jenkins and another app. When doing first push this causes an issue as Jenkins seemed to need a “transient” gear to build. Same issue as discussed in https://www.openshift.com/forums/openshift/jenkins-build-not-starting. Note that ap %appname%bldr gear was created as well.
    – When pushing the Jenkins console output contains:
    + export ‘GRAILS_OPTS=-Xmx512m -Xms256m -XX:MaxPermSize=256m’
    + GRAILS_OPTS=’-Xmx512m -Xms256m -XX:MaxPermSize=256m’
    + grails -Dgrails.work.dir=/var/lib/openshift/52d8dac8e0b8cd1b6800052a/app-root/data/.grails prod war
    mkdir: cannot create directory `/var/lib/openshift/52d8dac8e0b8cd1b6800052a//.grails’: Permission denied
    Don’t know if this is a problem but just wanted to check with you.
    – Deployment seems successfull (in Jenkins and message after push) but when navigating to app I just get the JBossEWS homescreen and not my app. Any idea?

    Also note that the Twitter login screen to leave comments on your blog is not working.

    \M

  2. Hello Marcel,

    I wrote the blog at 3am so made typos :). Really sorry.

    1) OpenShift Jenkins follows master slave paradigm. So, when you add Jenkins to your application it actually consumes two gears. One for the Jenkins application and another for the builder. The builder dies after 15 minutes of inactivity. But you need to have a free gear available for Jenkins to work.

    2) The error that your are mentioning indicate that you are not using this command “grails -Dgrails.work.dir=$OPENSHIFT_DATA_DIR/.grails prod war” to build the project. This command should be added to build hook as mentioned in blog. By default, grails create a working directory in the USER_HOME directory. In OpenShift you are not allowed to write to user home but to $OPENSHIFT_DATA_DIR.

  3. I have the following in my build_hook:
    grails -Dgrails.work.dir=$OPENSHIFT_DATA_DIR.grails prod war

    ButI think there is another typo in the pre_build hook 🙂

    $OPENSHIFT_DATA_DIR/.grails should be $OPENSHIFT_DATA_DIR.grails
    $OPENSHIFT_DATA_DIR already contains a slash at the end.
    I’m trying this out now…

  4. Hmm didn’t make any difference. Jenkins output still gives:
    mkdir: cannot create directory `/var/lib/openshift/52d96f484382ec546f0006bf//.grails’: Permission denied

    Note that I also had to manually delete the grailsbldr gear…

    1. I think I got the error.. you have to first do cd $OPENSHIFT_REPO_DIR in build action hook. I can also work with you via Google Hangout if you want..

  5. Hi, first of all let me thank you, your tutorial is one of the most valuable resources for grails on openshift.

    I have the same problem of Marcel (…mkdir: cannot create directory `/var/lib/openshift/52d96f484382ec546f0006bf//.grails’: Permission denied…)

    I’m using your code at github which is already performing the cd command.

    I found this link http://blog.lfsolutions.net/running-grails-on-openshift/ and it explain the problem related to that error, which is the default directory for grails…but i can’t access that file to change it so, how is it possible to solve the error?

  6. Hello,

    I added the following line to the build hook; then the ‘permission denied’ exception disappeared:

    export GRAILS_AGENT_CACHE_DIR=$OPENSHIFT_DATA_DIR/.grails/2.3.4

    However, then I have the same problem as Marcel. The Grails application is built somewhere, but it’s never put into the grailsapp/webapps folder. It seems the build process still needs to be adapted to make that work.
    Apart from that, the build is really slow. The Jenkins slave is an arbitrary instance just for the build job which is probably cleaned after each run. Therefore Grails is downloaded and initialized on each build.

    Thanks anyway for the post, Shekhar! For me it’s a good starting point.

    Björn

Leave a comment