Skip to content
Ritesh edited this page Mar 26, 2024 · 1 revision

Assignment 4

Problem Statement

Create a Declarative CI pipeline for java based project that contains various stages like * Code checkout * Run below stages in parallel - Code stability. - Code quality analysis. - Code coverage analysis. * Generate a report for code quality & analysis. * Publish artifacts. * Send Slack and Email notifications. The user should have the option to skip various scans in the build execution. And before publish there should be an approval stage to be set in place to approve or deny the publish and if approved the step should execute and the user should be notified post successful/failed Repeat the same using Scripted Pipeline.

Solution

Declarative CI Pipeline for Java-based Project

This pipeline automates the process of building, testing, analysing, and deploying a Java-based project. The pipeline contains various stages that run in sequence to ensure code stability, quality, and coverage, generate reports, publish artifacts, and send notifications. The pipeline also provides the option to skip certain scans and requires approval before publishing the artifacts.

Prerequisites

  • Jenkins server
  • Jenkins plugins:
  • Pipeline
  • Slack Notification
  • Email Extension

Pipeline Overview

The pipeline contains the following stages: Code Checkout: This stage checks out the source code from the version control system.

Code Checkout ![image](/uploads/b0edd1c9fdbd26d90dbf9e763938761e/image.png)

Parallel Stages:

Parallel Stages ![image](/uploads/89285d34e37f461bcfe7860c8544777f/image.png)
Parallel Stages ![image](/uploads/f055666615ac664b5365d44b1f9a4e64/image.png)

Code Stability: This stage compiles the code, runs unit tests, and checks for code stability.

Code Stability ![image](/uploads/d9ee507b859fd215d1ae1b381f3fbafb/image.png)

Code Quality Analysis: This stage analyzes the code quality using tools like SonarQube, Checkstyle, and PMD.

Code Quality Analysis ![image](/uploads/25f5cb6cf14d8dc777eb74d18fb26f09/image.png)

Code Coverage Analysis: This stage checks the code coverage using tools like JaCoCo and Cobertura.

Code Coverage Analysis ![image](/uploads/1f82584e28fc531e1db1d55ea9ae52f1/image.png)

Generate Report: This stage generates a report for code quality and analysis using SonarQube.

Publish Artifacts: This stage publishes the artifacts to the artifact repository.

Publish Artifacts ![image](/uploads/e0603e14372204bf09ebba83d7203819/image.png)

Approval Stage: This stage requires manual approval before publishing the artifacts. If approved, the pipeline moves to the next stage.

Approval Stage ![image](/uploads/53f013fc999feb32bbc48407463e4dbc/image.png)

Notification Stage: This stage sends notifications via Slack and email to notify the user of the pipeline status.

Notification Stage ![image](/uploads/431b054780d981481c54f5edb29d3d1e/image.png)
Notification Stage ![image](/uploads/179872d527937140deadee5cf3b68edc/image.png)

agent {
  label { label 'node2'  } 
    } 
        options {
        skipDefaultCheckout()
    }
    stages {
        stage('Checkout') {
            steps {
                git 'https://github.com/opstree/spring3hibernate'
            }
        }
    
        stage('Build') {
          steps {
              sh ''' mvn clean install '''
                }
        }   
             
        stage('Parallel stages') {
            parallel {
                
        stage('Code stability') {
            steps {
              sh ''' mvn surefire:test '''
                }
        }
        stage('Code_quality_analysis') {
            steps {
               sh ''' mvn checkstyle:checkstyle '''
              }
        }
        stage('Code_coverage_analysis') {
            steps {
                sh '''mvn cobertura:cobertura'''
                }
            }
        }
      
      }
        stage('Publish_artifact') {
        input {
           message 'Do you want to publish?'
           ok 'Yes, we want to publish'
    }
            steps {
               sh ''' mvn deploy '''
                }
        }  
    }
     post{
        failure{
            slackSend( channel: "#mukul-jenkins", token: "LxNrJ7Srr4oEa0Fw7tdLxRPW", color: "good", message: "Job URL: ${JOB_DISPLAY_URL} || BuildNumber: ${BUILD_NUMBER} || Job: ${JOB_NAME}  Failed")
    }
        aborted{
            slackSend( channel: "#mukul-jenkins", token: "LxNrJ7Srr4oEa0Fw7tdLxRPW", color: "good", message: "Job URL: ${JOB_DISPLAY_URL} || BuildNumber: ${BUILD_NUMBER} || Job: ${JOB_NAME}  Aborted")
    }
        success{
            slackSend( channel: "#mukul-jenkins", token: "LxNrJ7Srr4oEa0Fw7tdLxRPW", color: "good", message: "Job URL: ${JOB_DISPLAY_URL} || BuildNumber: ${BUILD_NUMBER} || Job: ${JOB_NAME} Successfully run")
    }
    unstable{
            slackSend( channel: "#mukul-jenkins", token: "LxNrJ7Srr4oEa0Fw7tdLxRPW", color: "good", message: "Job URL: ${JOB_DISPLAY_URL} || BuildNumber: ${BUILD_NUMBER} || Job: ${JOB_NAME}  Unstable")
    }
      }
}