Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Follow publication

Press enter or click to view image in full size

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

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

Press enter or click to view image in full size

Now open GitLab and click “New Project”.

Press enter or click to view image in full size

Then choose “Import Project”.

Press enter or click to view image in full size

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

Press enter or click to view image in full size

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

Press enter or click to view image in full size

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

Press enter or click to view image in full size
Press enter or click to view image in full size

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

Press enter or click to view image in full size

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

Press enter or click to view image in full size

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”.

Press enter or click to view image in full size

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

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

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

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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

Press enter or click to view image in full size

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
Press enter or click to view image in full size

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

sudo apt update
Press enter or click to view image in full size

Now we need to Install docker and the command is

sudo apt install docker.io -y
Press enter or click to view image in full size

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

adduser sonarqube
Press enter or click to view image in full size

Now switch to the Sonarqube user with the following command:

sudo su - sonarqube
Press enter or click to view image in full size

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

Press enter or click to view image in full size

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
Press enter or click to view image in full size

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
Press enter or click to view image in full size

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

Press enter or click to view image in full size

Now use the following command:

./sonar.sh start

we can start the sonar server.

Press enter or click to view image in full size

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

Press enter or click to view image in full size

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

Now it will prompt you to create a new password.

Press enter or click to view image in full size
Press enter or click to view image in full size

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

Press enter or click to view image in full size

Select the “Security” tab.

Press enter or click to view image in full size

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”.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size
Press enter or click to view image in full size

Registering Runner on EC2:

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

Press enter or click to view image in full size

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.

Press enter or click to view image in full size
Press enter or click to view image in full size

Let us check the status with the following command:

sudo gitlab-runner status
Press enter or click to view image in full size

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”.

Press enter or click to view image in full size

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

Press enter or click to view image in full size

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

Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

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

Press enter or click to view image in full size

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:

--

--

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

No responses yet