Real-Time CI CD Pipeline Project - java based - { Secrete Santa }

In these project, We are deploying the Java Based CI/CD Pipeline of Secrete Santa On the TomCat Server .

Perquisites -

Jenkins Docker Apache Tomcat SonarQube

https://github.com/Bhushan0151/Tools-Installation.git

for the Installation of the Jenkins , Docker And Apache Tomcat ,Please follow the Official Document.

To run this projects , the requirements of at least 2 GB ram & 2CPU server. for the installation of jenkins , Docker, Apache Tomcat and SonarQube.

Apache Tomcat & Jenkins Both run by Default on Port 8080 , so After installation to change the port one of them.

Create EC2 Instance and install the Jenkins , Docker & SonarQube on the Server.

after installing the Docker , For installing of the SonarQube , we can run the Container on the Docker.

SonarQube : docker run -d --name sonar -p 9000:9000 sonarqube:lts-community

SonarQube run by default on the 9000 port.

Required Plugin for these Project - install these plugin on Jenkins after installation.

Sonar Scanner OWASP Dependency Eclipse Temurin installer Docker docker-build-step Docker pipeline CloudBees Docker Build and Publish plugin maven

Tools Configuration : after Installing the above plugin - In the manage Jenkins.

Jdk - Version - 17 SonarQube - latest Version Maven - Version - 3.6.0 Dependency-Check - Version - 6.5.1 docker - latest Version

All Steps :

Step1 : On the Dashboard-> Select New Item -> enter the name of the project -> Select the Pipeline -> then ok

Step 2 - Write a description of the Project -> Click on the GitHub Project and the paste git repo link over there

Step 3 - Build Trigger -> Click on the GitHub Hook Trigger polling . GitHub Hook Trigger polling- Which is use for the , when we/anybody can changes the Done on the GitHub , Which will reflect on the pipeline it will trigger.

Step 4 - Create the Declarative Pipeline using Groovy Syntax

Using this Script we can run our pipeline-

Explanation of the script -

These are all the stages are agent of the pipeline. in these stages,

We need to set up the environment and mention the tools which we are using these in the pipeline .

Stage 1: Git Checkout : it is used for cloning the repository

Stage 2 : Code Compile : it is used for compiling the Code

stage 3 : Unit Test : It is sued for the testing the code

stage 4 : SonarQube analysis : We are using for , It provides extensive reporting capabilities to help teams identify and remediate code defects, vulnerabilities, and code smells & inspection of code quality .

stage 5 : OWASP Dependency : identifies project dependencies and checks if there are any known, publicly disclosed vulnerabilities.

Stage 6 : Build Artifact : The build artifacts we use for making the files, libraries, or packages executables & preparing it for deployment.

stage 7 : Docker Build - These stage is use for Building the code for making the Docker Image. using these Docker Image we can Deploy these code on the docker Container and after deploying it will accessible on the browser.

Stage 8 : Push to Docker Hub - Using these stage , after Building the code , we can push the image on the docker hub , for pushing the image, we need to login on the docker , and tagging the image and push on the docker hub, for all of these task we use these stage. Also need to add credentials on the manage Jenkins -> global tool configuration using adding the credentials .

Stage 9 : Deploy Container - These stage is use for the deploying the container , using the Docker image, which is we build in the previous stages

stage 10 : These stage is use for the deploy the file , using the Tomcat server. After installing the tomcat, we need to set the port and then and then run these command on the pipeline .

pipeline Script for this Project -

pipeline {
    
     agent any
     
     tools{
        jdk 'jdk11'
        maven 'maven3'
    }
    
    environment{
        SCANNER_HOME= tool 'sonar-scanner'
    }
    

    stages {
        stage('Git Checkout') {
            steps {
                git changelog: false, poll: false, url: 'https://github.com/Bhushan0151/secretsanta-generator.git'
            }
        }
        
        stage('Compile') {
            steps {
                sh 'mvn clean compile'
            }
        }
        
        stage('Unit Test') {
            steps {
                sh 'mvn test'
            }
        }
        
        stage('Sonarqube analysis') {
            steps {
                sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.url=http://20.168.56.157:9000/ -Dsonar.login=squ_cff042d64384afebb9bc36e21e39dcd53f36212e  \
                       -Dsonar.projectName=Secrete-santa \
                       -Dsonar.java.binaries=. \
                       -Dsonar.projectKey=Santa '''
            }
        }
        
         stage('OWASP Dependency') {
            steps {
                dependencyCheck additionalArguments: '--scan ./ ', odcInstallation: 'DP'
                dependencyCheckPublisher pattern: '**dependency-check-report.xml'
            }
        }
        stage('Build Artifact') {
            steps {
                sh 'mvn clean install'
            }
        }
        
        stage('Docker Build') {
            steps {
                sh 'docker build -t secrete-santa .'
            }
        }
        
        stage("Push to Docker Hub") {
            steps {
                echo "Pushing the Docker image to Docker Hub"
                withCredentials([usernamePassword(credentialsId: 'dockerHub', passwordVariable: 'dockerHubPass', usernameVariable: 'dockerHubUser')]) {
                sh "docker tag secrete-santa:latest ${env.dockerHubUser}/secrete-santa:santa-v1-bs"
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker push ${env.dockerHubUser}/secrete-santa:santa-v1-bs"
                }
            }
        }
        stage('Deply Using Docker') {
            steps {
                sh 'docker run -d --name Pet-01 -p 8084:80 bhushann11/secrete-santa:santa-v1-bs '
            }
        }
        stage('Deply Using Tomcat ') {
            steps {
                sh ' cp  target/*.jar /opt/apache-tomcat-9.0.65/webapps/ '
                
            }
        } 
    }
}

Stage View -

DockerHub Image

SonarQube -

Final OutPut -

Last updated