DEV@cloud » Build and Deploy a Grails application

Build and Deploy a Grails application

Last modified by Robert Wallström on 2011/12/05 20:09
Acknowledgement

This article was contributed by courtesy of Stephen Fisher http://stevef1uk.blogspot.com/. As a CloudBees addict, you're welcome to contribute content to our wiki, please contact support@cloudbees.com

A 101 on how to set-up CI for a Grails applications using Cloudbees

In this article I explain how I was able to set-up a Git repository on GitHub.com, populate it with a sample Grails application and then set-up Jenkins on CloudBees.com to poll the GitHub repository for changes, build the application and deploy the application to the Cloud on CloudBees. After reading this article you should be able to repeat the process in less  than 30 minutes yourself.

Prerequisites:

I assume that you have:

  • Downloaded Grails and have a JVM installed from http://grails.org/
  • Are comfortable using a Unix like terminal shell (I am using Mac Os. I could have used Windows but at home I prefer to use my macbook)
1. Setup Master Git Repository 

For this tutorial, I have used a GitHub hosted repository. You can also use CloudBees forge or whatever repository hosting service you like.

Click on the Set up Git tab and download and install git for your machine if not already present. This page explains how to get up your Github user name and email address in your local git global configuration file and how to set-up and store your ssh key.

If you don't have one already, you will create a local ssh key for GitHub access using ssh-keygen, as follows:

http://4.bp.blogspot.com/-FfWfYHjiLVA/TnUFx2v1x4I/AAAAAAAAACI/e_frMZ-uBJ8/s400/Screen+Shot+2011-09-17+at+20.05.31.png

Follow the instruction on how to configure git to be able to access your newly created GitHub repository as follows:

http://3.bp.blogspot.com/-75iT6QAG-Ko/TnUEks1wdnI/AAAAAAAAACE/o6_l-baP2vs/s640/Screen+Shot+2011-09-17+at+21.33.57.png

  • If you haven't already register for an account on http://github.com/.
  • Next create a repository on GitHub. I called my first one grailstest so this can later be accessed using the access string: git@github.com:<your-github-id>/<your_repo_name>.git (you can see my name on Github is seahope1 and the access string is shown clearly.

http://2.bp.blogspot.com/-A_KmP-U0uCI/TnUH-HNwlII/AAAAAAAAACc/mFyyowxeAmY/s640/repo.png

Next from your Github Account Settings tab access the SSH Public Keys tab and store your local ssh public key for your machine which you need to create as explained on the Set up Git tab. The default public key to store on GitHub will be stored locally in ~/.ssh/id_rsa.pub

http://4.bp.blogspot.com/-GzuulEmJA_Y/TnTZCaVmEZI/AAAAAAAAABs/8M6fl6Yqj9M/s320/Screen+Shot+2011-09-17+at+18.27.06.png

You will also need to add the CloudBees public key which you can find within the Jenkins job which you will create later. I will come back to this.

2. Create a local working git repository

In a local terminal window follow the instruction you get when you click on your empty repository on Github. I have added the 'grails add first' command to create a basic grails application to provide files to add to the repository:

git init
grails create-app first
git add first
git commit -m 'first commit'
git remote add origin git@github.com:<your-github-id>/grailstest.git
git push -u origin master

This will upload the default Grails application to your GitHub.com repository. You will be asked for your GitHub usename (not email address) and password when use use git push.

3. Set-up Cloudbees.com for Continuous Integration and deployment
  • If you haven't already, sign-up for an acount at http://www.cloudbees.com/
  • Make sure you subscribe for DEV@cloud & RUN@cloud services. I selected the free options.

http://1.bp.blogspot.com/-O3E7eunzf0s/TnUJ_g0BuaI/AAAAAAAAACg/VaHnXmOvgWU/s320/Screen+Shot+2011-09-17+at+21.57.52.png

  • From the Jenkins page select Manage Jenkins -> Manage Plugins and enable the Git plugin. Ensure you select Install when you will be asked to restart Jenkins.
  • You also need to enable grails and github support are listed under the Available tab.

http://2.bp.blogspot.com/-Cx0_o9181eA/TnUR0PLUL_I/AAAAAAAAACs/1u7kavM2hqQ/s400/Screen+Shot+2011-09-17+at+22.30.26.png

  • Again, you will need to restart Jenkins for these changes to take effect, which may take a few minutes.
  • From the Home screen -> Applications -> Add new application and call it: first grails

http://3.bp.blogspot.com/-BFvDmmRhCv8/TnUOX3yjV0I/AAAAAAAAACk/04gAOJL_EEc/s400/Screen+Shot+2011-09-17+at+22.16.13.png

  • From Jenkins screen create a New Job & select Build a free-style software project and name the job first. 

http://3.bp.blogspot.com/-0GoTAZdIy2E/TnUPMYzPDbI/AAAAAAAAACo/DOUJontS54g/s400/Screen+Shot+2011-09-17+at+22.19.40.png

Setup the following fields as follows:

  • Project name: first
  • GitHub project: git@github.com:<your-github-id>/<your-repo>.git
  • Source Code management -> Git repositories selected. URL of repository: git@github.com:<your-github-id>/<your-repo>.git
  • Branches to build Branch Specifier: **

http://1.bp.blogspot.com/-oIYs6qlFnD8/TnUUiBE1e2I/AAAAAAAAACw/u3iIExxkCBs/s640/Screen+Shot+2011-09-17+at+22.42.38.png

A while ago I mentioned the Cloudbees Public Key. This is located under the heading Cloudbees DEV@Cloud Authorization (towards the top of the screen). This is the key that needs to be entered in GitHub as described above as another SSH Public key. Once this key is added to GitHub for your account Jenkins will be authorised to access  the repository to get the source to build.

  • Build Triggers -> select Poll SCM and enter the pattern: * * * * *. 
  • Build Add Build Step and select Grails

http://4.bp.blogspot.com/-hK4YESJynJ0/TnUVTJiOYYI/AAAAAAAAAC0/v8q26PfHs3o/s320/Screen+Shot+2011-09-17+at+22.44.45.png

  • Build -> Build with Grails -> Grails Installation: Grails 1.3.7 (or your version)
  • Targets: "war target/first.war"
  • Project base directory: first

http://1.bp.blogspot.com/-4yUAhy1bwGM/TnUV57W_S-I/AAAAAAAAAC4/aLw75OciK08/s320/Screen+Shot+2011-09-17+at+22.48.49.png

  • Post-build actions -> Archive the artifacts Files to archive: first/target/first.war
  • Deploy to Cloudbees selected, Cloudbees site:<your-Cloudbees-id> (auto-populated)
  • Application Id: <your-Cloudbees-id>/firstgrails

http://3.bp.blogspot.com/-sxUzOpr2YXk/TnUWtKqGYvI/AAAAAAAAAC8/ZKbSFvfJYVw/s400/Screen+Shot+2011-09-17+at+22.52.19.png
You can safely ignore the error message as it is just telling you that the grails target has not been created through a build yet.

Save the job and select Build Now to test the job. At this point you will be asked to validate your account by clicking on a URL. The page displayed will ask for a telephone number where the automated call back service will provide a pin number which you need to enter to complete the validation process. Once you have successfully validated you will be able to run the job.

http://2.bp.blogspot.com/-NNFefcNZByY/TnUXbIZCvII/AAAAAAAAADE/KNsadtreAJM/s320/Screen+Shot+2011-09-17+at+22.54.15.png

The Build History window will show progress.  If you click on the build number (hopefully with a blue sucessful icon showing) you can use Console Output [raw] to check to see what happened and if successful you can run the deployed application by clicking the link: http://firstgrails.<your-cloudbees-id>.cloudbees.net to access the running sample application. You can find the link from the Application screen:

http://3.bp.blogspot.com/-pTlrzfX23kQ/TnUZMovbZGI/AAAAAAAAADI/jxWWukHDn1A/s640/Screen+Shot+2011-09-17+at+23.02.54.png

4. Make a change to the application and prove CI & CD works

In the Grails application change change the file: grailstest/first/grails-app/view/index.gsp e.g. change the text "Welcome to Grails" to "Welcome you to Grails" in both places. Then execute the commands:

git add index.gsp
git commit -m 'simple change to index.gsp'
git push -u origin master

You will notice on the Cloudbees Jenkins dashboard that after about one minute another build will be executed and once it completes successfully the application deployed will show the change.

Next Steps
  • You will notice that I haven’t tried to run the Grails tests and this step would have to be added before the application is deployed.
  • I have not had a chance to explore using the Cloudbees SQL database.
  • I have not explored how Grails plug-ins work with Grails on Cloudbees
Tags:
Created by Nicolas De Loof on 2011/09/19 11:32