Configuring Integration of CI/CD Pipeline for a Java Maven Project with Sonarqube, Trivy, and Docker
Prerequisites
Before configuring the pipeline, ensure that the following prerequisites are met:
GitHub account
Docker Hub account
Aws account
Basic knowledge of GitHub , Maven, SonarQube, Docker, and Trivy, Jenkins
Step 1: Project Setup
Create an EC2 instance in your AWS account with the specified configurations: Ubuntu t2.medium instance type, 30GB EBS volume, in the us-east-1 region.
Now login as root user
Install Jenkins by using this Jenkins.sh
Change the security group of ec2 Instance
Sign into Jenkins console http://<EC2_PUBLIC_IP>:8080 , get the password using
cat /var/lib/jenkins/secrets/initialAdminPassword
and install all the suggested pluginscreate your first user and continue
Step 2 : Pipeline creation
- Repo url : mention the url of git where you stored your project source code
Also the Branch name should be */main and the Script path: Jenkinsfile should be same for everyone , and save the changes
Now we need to add Plugins , Go to Dashboard -> Manage Jenkins -> Plugins -> Available Plugins
Plugins for Sonar/Jfrog:
Sonar Gerrit
SonarQube Scanner
SonarQube Generic Coverage
Sonar Quality Gates
Quality Gates
Artifactory
Jfrog, But why are we adding plugins Because we have to run Sonarqube which increases the capability in Jenkins
Now we have to install docker also use this docker.sh to install docker
Now install sonarqube also, we installed docker and jenkins via commands but sonarqube we gonna install via docker command and hit http://<EC2_PUBLIC_IP>:9000 (9000 is the port for sonarqube)
Now login into the sonarqube dashboard, username and password are admin
create sonar token for jenkins, let's see how we can do it :
Sonar Dashboard -> Administration -> My Account -> Security -> Create token
-> Save the token to some text file
Note: Name of the Token can be anything, Make sure to save the token
We have to Integrate Jenkins with sonar to be able to communicate with eachother, Sonar Dashboard -> Administration -> Configuration -> webhooks -> Add name: Jenkins and url:http://:8080/sonarqube-webhook/ and save
Step 3 : Tools Integration with Jenkins
- Now go to Jenkins Dashboard -> Manage Jenkins -> configure system
Click on sonarqube servers -> add url and name -> Click on add token -> Select Secret text -> Add the sonar token (which earlier we saved ) -> Give name of token as sonarqube-api
Now lets add docker hub credentials: Jenkins dashboard -> Manage Jenkins-> Credentials -> System -> click on global credentials , add the credentials as mentioned ๐
step 4 : Add Jenkins shared Library
Go to Manage Jenkins -> Configure system -> Global pipeline library -> Add below data :
Name - my-shared-library
Default version - main
Git -
https://github.com/manogna-chinta/jenkins_shared_lib.git
If you observe the
Java_app_3.0
child Jenkins file matches with the name mentionedNow click on build now and ta-da, The pipelines are passed
Now, let's take a look at the image we uploaded to Docker Hub
Parameters which we're creating from Jenkins file is helping us to create build with parameters in jenkins , Upon reviewing the logs, we can observe that an API has been generated alongside the provided credentials ๐
Let's now navigate to SonarQube, where we can identify any code smells present.
Thank you for your time. If you found the information helpful, please consider giving it a like. Your feedback is appreciated!
Resources:
https://github.com/manogna-chinta/Java_app_3.0
https://github.com/manogna-chinta/jenkins_shared_lib
https://github.com/manogna-chinta/Tools_installation_scripts