This blog will go through a Continuous Integration scenario using Jenkins and the concept of Pipeline as a Code. The task involves Jenkins, Maven, SonarQube, and Nexus. Maven will be used for building and testing the code. SonarQube will be used for code analysis, bugs, and vulnerability checks while Nexus will store the build artifact in the repository.
Prerequisite:
GitHub account
AWS account and knowledge of EC2 Instances
Knowledge of Jenkins, Maven, SonarQube, and Nexus
Setup
The entire setup for all the individual services would be on AWS Cloud's EC2 instances. There will be 3 instances of 3 services - Jenkins, SonarQube, and Nexus.
The scripts for the services on the instances are available on this repository.
The security groups for all these services have to be configured so that all the services allow access to each other on their default port numbers.
Step 1: Configure Jenkins
Execute the script from the repository and check the status of the Jenkins service.
Setup Plugins
Go to Manage Jenkins, and select Manage Plugins. Under available plugins search for the below plugins and Install without restart
Nexus 2
Sonarqube
Git
Pipeline Maven Integration
Build Timestamp plugin
Step 2: Configure Nexus
Configure a new username and password for Nexus.
Login with these new credentials and create a new repository on Nexus.
Setup 3: Configure SonarQube
Execute the scripts from the repository to start the SonarQube service on an EC2 instance.
Login using "admin" as both username and password
Integrate SonarQube with Jenkins by adding a URL of the SonarQube service in the System configuration of Jenkins
Note: The IP address is the private IP of the SonarQube instance.
Create a quality gate on SonarQube as a threshold for the number of bugs.
Create a webhook that will pass the results of the report from SonarQube to Jenkins
Jenkinsfile
pipeline {
agent any
//tool name configured in Jenkins Global Tools section
tools {
maven "MAVEN3"
jdk "OracleJDK8"
}
stages{
stage('Fetch code') {
steps{
git branch: 'main', url:'https://github.com/Vedant-MAHAjan/Jenkins-CI.git'
}
}
stage('Build') {
steps {
sh 'mvn clean install -DskipTests'
}
post {
success {
echo "Now Archiving."
// archive all files with .war extension
archiveArtifacts artifacts: '**/*.war'
}
}
}
stage('Test'){
steps {
sh 'mvn test'
}
}
stage('Checkstyle Analysis'){
steps {
sh 'mvn checkstyle:checkstyle'
}
}
stage('Sonar Analysis') {
environment {
// tool name configured in Jenkins Configure Global Tools
scannerHome = tool 'sonar4.7'
}
steps {
withSonarQubeEnv('sonar') {
sh '''${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=vprofile \
-Dsonar.projectName=vprofile \
-Dsonar.projectVersion=1.0 \
-Dsonar.sources=src/ \
-Dsonar.java.binaries=target/test-classes/com/visualpathit/account/controllerTest/ \
-Dsonar.junit.reportsPath=target/surefire-reports/ \
-Dsonar.jacoco.reportsPath=target/jacoco.exec \
-Dsonar.java.checkstyle.reportPaths=target/checkstyle-result.xml'''
}
}
}
}
}
Results
Check the successful pipeline build on Jenkins
The artifact is stored in the Nexus repository with the appropriate timestamp.
Conclusion
In summary, this blog detailed a streamlined Continuous Integration setup using Jenkins, Maven, SonarQube, and Nexus on AWS. The process involved configuring three EC2 instances, setting up plugins in Jenkins for Nexus and SonarQube integration, and creating a Jenkinsfile for a comprehensive Pipeline as Code. The pipeline successfully fetched code, performed build and test stages, analyzed code with SonarQube, and archived artifacts in Nexus. This end-to-end integration ensures a smooth, automated workflow, empowering developers to collaborate efficiently and deliver high-quality software.