Streamlining Java (Spring Boot App)Development: Maven, SonarQube, and Docker on EC2 with GitLab CI

Sarat Chandra Motamarri
Stackademic
Published in
7 min readApr 2, 2024

--

Empowering Java (Spring Boot App) development workflow with an efficient CI pipeline utilizing Maven, SonarQube, and Docker. This comprehensive setup, orchestrated through GitLab CI, ensures seamless integration, robust code analysis, and efficient containerization, all hosted on a scalable EC2 instance. Accelerate your software delivery while maintaining code quality and reliability.

Note: If you wish to do this project there will be a charge. 
We will be using AWS EC2 Instance - Ubuntu- t2 medium
We will install Docker, Java, and Sonar Server on this Instance.

Importing code from GitHub to GitLab:

This is the code repo which we will be importing to GitLab

Now open GitLab and click “New Project”.

Then choose “Import Project”.

Now we have to choose the source from where we want to import our project and that is “GitHub”.

Now we have to choose which repo we want to import from GitHub to GitLab.

Type in the repo in the search bar and select “Import” to Import.

Wait for a couple of seconds the repo will be imported.

Once it shows “Complete”, it means the repo has been imported.

Now select the repo that was imported in my case it is “Jenkins-Zero-To-Hero.

Creating Sonar Server:

Now it’s time to create Sonar Server. For this let’s navigate to AWS Console to create an EC2 Instance, “Ubuntu”, “t2-medium”.

In this case, I have named it “Java-Maven-gitlab”.

These are the basic settings and click “Launch Instance”.

Now that the Instance is launched let’s configure the traffic rules. To achieve this select the instance>>Security>>Select the security group associated with Instance.

Select “Edit Inbound rules”. Now add “All Traffic” and “Any Network” and hit “Save rules”.

Now that we have configured “Traffic rules” to the EC2 Instance, let us “ssh” to this instance using the following command:

ssh -i keypair.pem ubuntu@publicip

Next, let’s update the “apt” package manager with the following command:

sudo apt update

Now we need to Install docker and the command is

sudo apt install docker.io -y

Now switch to the root user by “sudo su -” and add Sonarqube as a user.

adduser sonarqube

Now switch to the Sonarqube user with the following command:

sudo su - sonarqube

Let’s unzip using “unzip *”. This command “unzip *” would try to unzip all files, regardless of their names, in the current directory.

Now we have to give permissions using the following commands:

chmod -R 755 /home/sonarqube/sonarqube-9.4.0.54424
chown -R sonarqube:sonarqube /home/sonarqube/sonarqube-9.4.0.54424

Now navigate to the directory where the shell file for the sonar server is located using the following commands:

cd sonarqube-9.4.0.54424/bin/linux-x86-64/
./sonar.sh start

But before we start the sonar server we need to install “Java” as sonar is a Java-based application. To achieve this we need to logout from the current sonarqube user switch to the root user to install java and use the following command:

sudo apt install openjdk-11-jre

Once we are good, now switch to the sonarqube user and navigate to the folder where the “./sonar.sh” file.

Now use the following command:

./sonar.sh start

we can start the sonar server.

Let’s open this in a web browser using the public IP of the instance and the default port of sonarqube which is 9000.

The default Login is “admin” password is “admin”.

Now it will prompt you to create a new password.

Now click “Administrator”>> “My Account”.

Select the “Security” tab.

We need to create a token so that SonarQube can access the code repo which lives in the GitLab. Now “Enter some name” and hit “Generate”.

Once the token is generated save the token.

Penning the “.gitlab-ci.yml” file:

Now we need to write the “.gitlab-ci.yml” file as shown below. This is a configuration file used by GitLab CI/CD pipelines to define the steps and stages for building, testing, and deploying code automatically. It provides a structured way to automate the software development lifecycle within GitLab.

Instead of hardcoding sensitive information like passwords, we can add them as variables. For this let us add some variables. To achieve this we need to go to Settings >>CI/CD>>Variables>>Expand>>Add Variable.

Now we need to add the token that was generated previously in the SonarQube. Also, we need to add the Docker name and password as tokens as shown below.

Registering Runner on EC2:

Now we need to register runners on EC2, scroll down to “Runners”>> “New project runner”.

Now we need to install Gitlab runner on the EC2 Instance. Switch to the root user. For this, we can follow the instructions by simply copying the commands and pasting them into the terminal that we are connected with the EC2 Instance.

Let us check the status with the following command:

sudo gitlab-runner status

It should display “service is running”. The next step is to register the gitlab-runner. While registering we need to answer certain questions like “GitHub URL”, “token”, “executor”, and “Docker Image”. “GitHub URL” and “token” are necessary for authentication and accessing repository code, while “executor” and “Docker Image” specify the environment for running CI/CD tasks, ensuring compatibility and reproducibility in the pipeline setup. Once this information is given it displays “Runner registered successfully”.

Now commit the “.gitlab-ci.yml” file.

Now the Pipeline should display all three stages in the job:

Now that the pipeline is a success, let’s check our code analysis results in the URL mentioned.

Hooray, we have successfully, analyzed our code. Finally, make sure to clean up your resources to avoid charges.

You can reach me at 
LinkedIn:https://www.linkedin.com/in/sachmo/
GitHub: https://github.com/csarat424

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

--