Few days ago I started to play around with porting one simple web application to the Cloud, to see how quickly it can be done. Provider of choice was CloudBees. CloudBees is one among many platform-as-a-service products available on the market, best known by its focus on Java Developers. It offers a code repository (Git or Subversion), private Maven repositories, Jenkins server to set up full Continuous Integration and Continuous Deployment pipeline and environment for running application (Tomcat, JBoss, GlassFish, Jetty). List goes on, but this is pretty much enough for building and deploying one simple Java web application.
In this tutorial you will learn what are the basic steps to move application to CloudBees.
As an example, we are going to use RESTful web application written in Java, which is using Spring for creating RESTful web services, MongoDB for storing some data and Maven for building project. Sample application is called Fly to Cloud and it has one page which allows to enter few names and list them out. Simple enough.
At the end of this tutorial we will have application shared in Git repository, running on Tomcat 7, with Continuous Deployment set up in Jenkins, and all this in the Cloud.
- Install Git
- Download source code for the Fly to Cloud sample application here.
So, let’s get things going.
1. Step: Create an account on CloudBees (www.cloudbees.com)
Sign up with existing GitHub or Google account, or create CloudBees dedicated account. No credit card information are needed. After registration, it will take you to CloudBees web console. From here you can create new applications, databases, repositories and configure new builds.
CloudBees Web Console
2. Step: Add your SSH key to the account
This will be used to identify yourself to multiple CloudBees services.
If you don’t have ssh key generated already and you are not familiar with generating one, take a look on Git help page https://help.github.com/articles/generating-ssh-keys. Go to GitBash and run the command to read ssh public key:
Copy this key to the account settings, Security Key section.
3. Step: Create Git repository
Go to Repos tab on the CloudBees web console. Click the Add Repository button to launch the repository creation wizard. Type the name of your repository and hit Create button. In our example, name of the Git repo is flytocloud. After creating git repo, URL to Git repository will be displayed, like this:
4. Step: Commit downloaded application source code to Git repository
Open Git Bash on your computer and run these commands (assuming that a project folder is flytocloud):
git remote add origin ssh://firstname.lastname@example.org/hipp/flytocloud.git
git add --all # if you want to check everything in
git commit --message="Initial checkin"
git push --set-upstream origin master
Now all the code is in the cloud and ready to be built with Jenkins.
5. Step: Set up Continuous Integration with Jenkins
Go to Builds tab on the CloudBees web console. Create new Jenkins job. Enter flytocloud for a new Job Name and since this is a Maven project, select Build a maven2/3 project. After hitting OK button, new job is created and you are taken to job configuration page.
In the Source Code Management section select Git and enter ssh://email@example.com/hipp/flytocloud.git as Repository URL.
Make sure that in a Build Triggers section, Build when a change is pushed to CloudBees Forge is selected. This will trigger new build each time change is pushed to a Git repository, which is enabling Continuous Integration. When new build is done, new artifact will be ready in the Maven repository. In case of this application, this is a war package of the application. Maven repositories could also be checked on Repos tab of CloudBees web console. If your application is dependant on some project-specific private artifacts, then this Maven repository needs to be added in your project’s pom.
Open pom.xml and add following repository:
Commit and push this change to master branch. After git push is executed, you should see build process is running on Builds tab at CloudBees web console.
At this point, we have Git repository where you can share your code and Jenkins job configured to perform build each time change is pushed. Now, since this application is using MongoDB as its storage, we need to enable MongoDB to run in the cloud for us, and to configure connection settings in the application.
6.Step: Subscribe to MongoDB and configure connection settings
In CloudBees web console there is a Databases tab, but it only offers access to MySQL database instances. MongoDB is provided as CloudBees partner service, and needs to be subscribed to it separately.
Go to Ecosystem item, which is second item from the left in the top menu. List of various services will appear. From the Data Storage category select MongoHQ. It will offer to select type of subscription, and for the purpose of this tutorial, select Free one and hit Subscribe button. Now, when you are subscribed to MongoHQ click again on MongoHQ in Ecosystem panel. It will take you to the MongoHQ console from where you can create new database.
Click on Add Resource button in the top left corner. Add a MongoDB database dialog will appear. To keep it simple we will enter flytocloud in Name field. Choose Sandbox for Plan and EU-West Data Center. Click Continue to create database. After database creation is done, click on View MongoHQ Dashboard to see and manage your database instance.
This funny name like A2Fzqmx6j9kosFSon4iOTw is the name of a database. Next step is to create database user, which will be used by the application.
MongoDB Add User
Remember the password you entered, since plain password will not be displayed anymore in the Mongo dashboard, once it is saved.
Let’s see now what are database connection settings which will be used by the application.
On the Overview tab, MongoURI is displayed:
MongoDB Connection Settings
Now when all database connection settings are in place, enter them in application.properties file and push back to git remote branch.
7. Step: Continuous Deployment with Jenkins
Last step is to set up Tomcat container where application will run, and Continuous Deployment to that container.
Go to Apps tab on CloudBees web console. Click Add Application button and select Tomcat 7 from drop down menu. For the Application name enter flytocloud. It may take up to a few minutes to build and deploy. What might be confusing here is term of the Application. Application is not anyhow connected to the actual application code, but it is just a configured environment where app will run, like tomcat or jboss.
Now go to the Builds tab and go to the Jenkins job configuration page. In the Post Build Actions section click Add post-build action and select Deploy Applications. Enter flytocloud as Application ID. This will connect application instance you created and Jenkins job, and allow Jenkins to deploy every new stable build to Tomcat.
Now when Continuous Deployment is set up, push some minor code change to trigger build process. When change is pushed to remote git repository, it will trigger building war and deploy to Tomcat.
Go to Apps tab and click on the Location link to check out our fancy application.
For my account, it was http://flytocloud.hipp.cloudbees.net/.
If you can see this screen, you have successfully finished moving application to the Cloud.
Application in the Cloud
And if you can save your name here, it means it actually works!
Having your infrastructure in the cloud is a great way to speed up development process and focus on development tasks, instead of configuring all necessary servers and services on your own. It is especially convinient when you as a developer want to try out some new idea, but don’t want to spend hours on setting up the environment. Different platform-as-a-service providers offers many services out of the box, which at least enables you to cheap & easily try them out. I hope this tutorial helped you to quickly set up your project in the cloud and check out what are the benefits of it.
Thanks for reading!