giovedì 27 marzo 2014

Grails on Openshift

Openshift platform allows you to create an app very easily but i found quite difficult to achieve an obvious task: deploying a Grails application without having to redeploy every time the full war file.

I found a simple way to avoid committing the war file, any time.

A Grails application can be run on Openshift just installing a Tomcat7 cartridge and then deploying the war file on it.

You can put the war file on the openshift instance and commit the war file as if it was the only file in your git repository.

This works correctly, but it means that any time you want to deploy an app, you must upload an entire war file. This is unacceptable, so i tried to solve the problem.

I tried two different approaches:

  1. installing Grails on openshift: you can ssh to your application via rhc ssh command and then you can download Grails in your $OPENSHIFT_DATA_DIR and after setting some environment variables you can build your app
  2. configure Jenkins for CI to build automatically on any git push, by setting some "special files" to instruct Jenkins to download Grails and build your project with it
These two ways work, but with an unacceptable complexity level! But trying to use these approaches i learned a lot of things.

Here are two links to very useful approaches: run Grails on Openshift and deploy with Jenkins.
These two approaches let me learn a lot about Openshift but didn't work for me. And even if those worked, it seemed to me that they were too complex and difficult to mantain.

The good news is that a solution exists and it is super-easy and clean and do perfectly work!

How to deploy a Grails application on Openshift without redeploying a war file any time

Go create you application on Openshift: the best way to do it is to use rhc (for more informations have a look here on Openshift website).

Step 1 create app on Openshift

I will create an app called "grails" in a local folder called openshift so go to openshift folder and 

rhc create-app grails jbossews-2.0

Step 2 create a Grails project


here you can use your favourite tool to create a local Grails project (or clone it from somewhere) in a different folder

Step 3 remove defaults from openshift folder 

Now we remove some files from the grails folder automatically created by rhc create which is connected to git)

rm -fr pom.xml src


Step 4 perform some git operations:


git add -A
git commit -m "get rid of useless application"

Step 5 add exploded war configurations to your grails project's BuildConfig.groovy


grails.project.war.exploded.dir = "/yourlocalpathto/openshift/grails/webapps/ROOT"
grails.war.exploded=true

A note about this: this is the key of the process. We are saying Grails to create an exploded-war (which is a full war file that instead of being compressed as a single binary file, is exploded in a directory) so that the first time we will have to upload via git everything (even all the jars) but next git push will send only the differences, which is what we wanted to achieve

Step 6 create a dodeploy file


This is a file which instructs jboss that after the push it must take the ROOT folder and deploy it on Tomcat (which is emulated by JBoss) as if it was a war file

in /yourlocalpathto/openshift/grails/webapps execute

touch ROOT.war.dodeploy

Step 7 perform grails war

perform grails war and it will create the ROOT folder

Step 8 push the files to OpenShift


go to /yourlocalpathto/openshift/grails/ and execute:

git add -A
git commit -m "deploy application"
git push

That's all folks!

Now all your files will be deployed to Openshift. You can check operation log opening a new terminal and giving rhc tail.

Any time you want to deploy the app, execute step 7 and 8 and wait only for Tomcat redeploy time which is slooooow!

You will have to upload only the changed files. This approach have a secondary advantage: you can have your grails app commited on a different git repository. You can push files to the git without needing to deploy the app. Any time you want to deploy, you build a war file and manually push it from /yourlocalpathto/openshift/grails folder.


Now that you have read my article, i would like to show you another thing: i've developed an app to help increase customers registration and customers conversion.

You can find it at appromocodes.com

2 commenti:

  1. This simple recipe works fantastic - thank you for the post.

    Do you have any tips for getting Jenkins to work on OpenShift?

    RispondiElimina
    Risposte
    1. I tried Jenkins on Openshift (if i remember it was easy as installing a cartridge from Openshift web console), but i found it useless for my purposes as any time i committed something, obviously it would redeploy the app (with a slow redeploy time). I wanted to be able to commit but not to immediately redeploy (the approach i described in the post let me achieve this goal).
      If you want Jenkins to work with the exploded war, you should only implement the approach described in the article linked (http://whyjava.wordpress.com/2014/01/16/how-to-run-grails-application-with-jenkins-on-openshift/) and avoid the build phase, because you build the code on your local machine and commit the built code as an exploded war and then you have only to pull it on the Openshift instance and move to jboss folder

      Elimina