Continuous Integration for a Spring Boot App Using Codeship and Openshift

DZone 's Guide to

Continuous Integration for a Spring Boot App Using Codeship and Openshift

Learn how to deploy a Spring Boot application to Openshift with this step by step guide.

· DevOps Zone ·
Free Resource

I have already talked about CI using pair Codeship + Openshift and I like such a combination. Recently, I have started a new project based on spring boot and faced the issue that Openshift does not provide an out-of-box pattern to run Spring Boot applications.

Spring boot applications can be built in two ways: with or without embedded Tomcat. After reading some tutorials on how to build an application without Tomcat, I decided not to go this way. It requires some code changes and some settings tuning on the Openshift side. The solution with embedded Tomcat looks more consistent and reliable to me.

To set up a Spring Boot app, you need DIY gear with Maven and Java 8. I was lucky and found a repo on GitHub with a sample Spring Boot application on DYI openshift gear. So I used this code as the start of my application. The main magic is located at .openshift/action_hooks/deploy. It downloads Maven and sets up all required environment variables.

In the next step, you should build and test your Spring Boot project on Codeship. All technical details about setting up Codeship can be found here. I will notice that in this tutorial the build was created for a Grails project, so the test and deploy steps for Spring Boot will be slightly different.

First of all, we need to use Java 8 on the Codeship server for tests instead of default Java 7. To enable Java 8 in the setup section of Project settings -> Test, include this:

jdk_switcher home oraclejdk8
jdk_switcher use oraclejdk8

The test script has nothing special except Spring profile for CI execution:

mvn clean initialize
mvn clean test -Dspring.profiles.active=ci
mvn package -Dmaven.test.skip=true

The deployment script is the most interesting part here. Usually, when you use standard Tomcat Openshift gear, all you need is place a WAR file in a correct directory, and it will be automatically deployed. I want to use embedded Tomcat, so I get a packed JAR which should be started manually. Such a case was not what I wanted (take into account that before starting I should stop an old running version of the application). Performing all these operations from a deployment script located on Codeship was also not possible due to insufficient access rights to start and stop processes. All I can do remotely are file operations and controlling the Openshift gear lifecycle. It gives me a way to resolve deployment problems:

  1. Stop running gear.
  2. Delete old JAR.
  3. Copy freshly built JAR from Codeship to Openshift.
  4. Start gear.
  5. Openshift action hook that triggers by starting gear, which will start the application from a new JAR.

The first 4 steps of the plan are located in the Codeship deploy script:

ssh <some_hash>@project_name-acc_name.rhcloud.com 'gear stop'
scp -rp /home/rof/src/<codeship_account>/<codeship_project>/target/*.jar <some_hash>@project_name-acc_name.rhcloud.com:~/app-root/runtime/repo/target/
ssh <some_hash>@project_name-acc_name.rhcloud.com 'gear start'
wget --retry-connrefused --no-check-certificate -T 60 https://project_name-acc_name.rhcloud.com/somepath/to/ping

Actually, there is no removing the old JAR, because it always has the same name, so the new file will overwrite the old one. I add the ping action as the last command to be sure the application is up and running once I see that build is passed.

Now let's take a look at the 5th step of the plan — starting the application with an action hook. All Openshift hooks are placed at .openshift/action_hooks in the Git repository of your Openshift project. As I started with the sample of Spring boot layout for Openshift, I already have a script for starting a gear with action hooks: .openshift/action_hooks/start. The main part of this script is this line:

nohup java -Xms384m -Xmx412m -jar target/*.jar --server.address=${OPENSHIFT_DIY_IP} --spring.profiles.active=openshift &

So all I need here is to be sure that I have correct path and Spring profile.

codeship ,ci ,profile ,pattern ,deploy ,spring boot ,application ,openshift

Published at DZone with permission of Ivan Zerin , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}