Skip to content

10_Declaritive_Pipeline

bhupender rawat edited this page Jun 20, 2021 · 5 revisions

Declarative Pipeline

In this lab we will create a Jenkins Declarative Pipeline for CI :

  • Create a Jenkins Pipeline that will kick start the CI process if a commit is made.
  • Add a Stage in the pipeline to perform to Code stability.
  • Add a Stage in the pipeline to perform code quality analysis.
  • Add a Stage in the pipeline to perform code coverage analysis.
  • Add a Stage in the pipeline to generate reports for code quality & analysis.
  • Add a Stage in the pipeline to send Slack and Email notifications.

Pipeline with Commit

  • Create a pipeline Jenkins Job with "Declarative Pipeline CI" and add this snippet

pipeline {
    agent any
    stages {
        stage("Clonning the codebase") {
            steps {
               git 'https://github.com/OT-TRAINING/Jenkins'
            }
        }
    }
}

Code Stability check-in Pipeline

  • Configure the pipeline with this code

pipeline {
    agent any
    stages {
        stage("Clonning the codebase") {
            steps {
               git 'https://github.com/OT-TRAINING/Jenkins'
            }
        }
        stage("Code Stability") {
            steps {
               sh "cd SampleJavaWebApp; mvn clean package"
            }
        }
    }
}

Code Quality check in Pipeline

  • Configure the pipeline with this code

pipeline {
    agent any
    stages {
        stage("Clonning the codebase") {
            steps {
               git 'https://github.com/OT-TRAINING/Jenkins'
            }
        }
        stage("Code Stability") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B clean package"
            }
        }
        stage("Code Quality") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B checkstyle:checkstyle"
               recordIssues(tools: [checkStyle()])
            }
        }
    }
}

Code Coverage check-in Pipeline

  • Configure the pipeline with this code

pipeline {
    agent any
    stages {
        stage("Clonning the codebase") {
            steps {
               git 'https://github.com/OT-TRAINING/Jenkins'
            }
        }
        stage("Code Stability") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B clean package"
            }
        }
        stage("Code Quality") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B checkstyle:checkstyle"
               recordIssues(tools: [checkStyle()])
            }
        }
        stage("Code Coverage") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B cobertura:cobertura"
               cobertura autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/target/site/cobertura/coverage.xml', conditionalCoverageTargets: '70, 0, 0', failUnhealthy: false, failUnstable: false, lineCoverageTargets: '80, 0, 0', maxNumberOfBuilds: 0, methodCoverageTargets: '80, 0, 0', onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false
            }
        }
    }
}

Adding Email and Slack Notifications


pipeline {
    agent any
    stages {
        stage("Clonning the codebase") {
            steps {
               git 'https://github.com/OT-TRAINING/Jenkins'
            }
        }
        stage("Code Stability") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B clean package"
            }
        }
        stage("Code Quality") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B checkstyle:checkstyle"
               recordIssues(tools: [checkStyle()])
            }
        }
        stage("Code Coverage") {
            steps {
               sh "cd SampleJavaWebApp; mvn -B cobertura:cobertura"
               cobertura autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/target/site/cobertura/coverage.xml', conditionalCoverageTargets: '70, 0, 0', failUnhealthy: false, failUnstable: false, lineCoverageTargets: '80, 0, 0', maxNumberOfBuilds: 0, methodCoverageTargets: '80, 0, 0', onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false
            }
        }
        stage("Send Notitications") {
            steps {
                emailext body: '', subject: '', to: '[email protected]'
                slackSend message: 'Build is Successful'
            }
        }
    }
}



Previous: Pipeline Section                        Next: Scripted Pipeline