Continuous integration, continuous deployment and continuous delivery are increasingly popular topics among modern development teams. Together they enable a team to build, test and deploy the source code at any commit. The main benefit of these approaches is the ability to release more quality code more frequently through the means of automated pipelines. The tough part is building such pipelines. There is a myriad of tools available which we would need to choose, learn, install, integrate, and maintain.
Recently, I literally fell in love with GitLab! It offers a fully featured ecosystem of tools which enable us to create an automated pipeline in minutes! From source control to issue tracking and CI, we find everything under one roof, fully integrated and ready to use.
In this tutorial, we will create a Spring Boot application built, tested, and deployed with GitLab CI on a Kubernetes cluster.
What are Spring Boot and Kubernetes?
Spring Boot (sometimes called Java Spring Boot) is the leading microservice chassis for Java. It allows a developer to build a production-grade stand-alone application, like a typical CRUD application exposing a RESTful API, with minimal configuration, reducing the learning curve required for using the Spring Framework drastically.
Kubernetes is an open-source container orchestrator inspired by Google Borg that schedules, scales and manages containerized applications.
Create a GitLab project
Let's start by creating a new project in GitLab named actuator-sample
. Then we follow the command line instructions displayed in the project's home page to clone the repository on our machine and perform the first commit.
git clone [email protected]:marcolenzo/actuator-sample.git
cd actuator-sample
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
Always replace marcolenzo
with your own GitLab username whenever copying a snippet of code from this tutorial.
Create a Spring Boot application
To bootstrap the Spring Boot application we navigate to the Spring Initializr web page and generate a Maven Project with the pre-selected Spring Boot Version. Maven is a project management tool commonly used in Java projects to define dependencies and the build lifecycle. We leave com.example
as Group and set actuator-sample
as the Artifact name. We select the Web
dependency, which supports full stack web development with Tomcat and Spring MVC, and the Actuator
dependency which implements some production-grade features useful for monitoring and managing our application like health-checks and HTTP requests traces.
Finally, we generate the project and a Zip file named actuator-sample.zip
will be downloaded to our machine.
We can now unzip the archive and launch the application immediately. Spring Initializr has already created everything for us. We just need to have a Java JDK 1.7 or later installed on our machine and the JAVA_HOME
environment variable set accordingly. OpenJDK is the preferred option for most Linux distributions since it is readily available on their repositories. You can alternatively install Oracle JDK if it is a strict requirement for your team.
### Installing OpenJDK 8 on Debian, Ubuntu, etc.
sudo apt-get install openjdk-8-jre
### Installing OpenJDK 8 on Fedora, Oracle Linux, Red Hat Enteprise, CentOS, etc.
su -c "yum install java-1.8.0-openjdk"
### Setting the JAVA_HOME environment variable
export JAVA_HOME=/path/to/your/java/home # e.g. /usr/lib/jvm/java-8-openjdk-amd64/
### Extracting and launching the application
~/git/actuator-sample$ unzip ~/Downloads/actuator-sample.zip -d ../
~/git/actuator-sample$ ./mvnw spring-boot:run
[...]
2016-12-02 22:41:14.376 INFO 10882