Skip to content

11_Scripted_Pipeline

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

Scripted Pipeline

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

  • Create a Jenkins Scripted 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.
  • Try to create a pipeline for another project with as minimum code changes as possible.

Pipeline with Commit

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

node("master") {
    codeCheckout(
        "https://github.com/OT-TRAINING/Jenkins",
        "master"
    )
}

def codeCheckout(gitUrl, gitBranch) {
    stage("Clonning codebase") {
        git branch: "${gitBranch}",
        url: "${gitUrl}"
    }
}

Code Stability Checks

  • Configure "Scripted Pipeline CI" and add this snippet

node("master") {
    codeCheckout(
        "https://github.com/OT-TRAINING/Jenkins",
        "master"
    )
    codeStability("SampleJavaWebApp")
}

def codeCheckout(gitUrl, gitBranch) {
    stage("Clonning codebase") {
        git branch: "${gitBranch}",
        url: "${gitUrl}"
    }
}

def codeStability(dir) {
    stage("Code stability") {
        sh "cd ${dir}; mvn clean package"
    }
}

Code Quality Checks

  • Configure "Scripted Pipeline CI" and add this snippet

node("master") {
    codeCheckout(
        "https://github.com/OT-TRAINING/Jenkins",
        "master"
    )
    codeStability("SampleJavaWebApp")
    codeQuality("SampleJavaWebApp")
}

def codeCheckout(gitUrl, gitBranch) {
    stage("Clonning codebase") {
        git branch: "${gitBranch}",
        url: "${gitUrl}"
    }
}

def codeStability(dir) {
    stage("Code stability") {
        sh "cd ${dir}; mvn clean package"
    }
}

def codeQuality(dir) {
    stage("Code quality") {
        sh "cd ${dir}; mvn checkstyle:checkstyle"
        recordIssues(tools: [checkStyle()])
    }
}

Code Coverage Checks

  • Configure "Scripted Pipeline CI" and add this snippet

node("master") {
    codeCheckout(
        "https://github.com/OT-TRAINING/Jenkins",
        "master"
    )
    codeStability("SampleJavaWebApp")
    codeQuality("SampleJavaWebApp")
    codeCoverage("SampleJavaWebApp")
}

def codeCheckout(gitUrl, gitBranch) {
    stage("Clonning codebase") {
        git branch: "${gitBranch}",
        url: "${gitUrl}"
    }
}

def codeStability(dir) {
    stage("Code stability") {
        sh "cd ${dir}; mvn clean package"
    }
}

def codeQuality(dir) {
    stage("Code quality") {
        sh "cd ${dir}; mvn checkstyle:checkstyle"
        recordIssues(tools: [checkStyle()])
    }
}

def codeCoverage(dir) {
    stage("Code coverage") {
        sh "cd ${dir}; mvn 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
    }
}

Main Function

  • Configure "Scripted Pipeline CI" and add this snippet

node("master") {
    executeCIPipeline(
        "https://github.com/OT-TRAINING/Jenkins",
        "master",
        "SampleJavaWebApp"
    )
}

def executeCIPipeline(gitUrl, gitBranch, dir) {
    codeCheckout(
        "${gitUrl}",
        "${gitBranch}"
    )
    codeStability("${dir}")
    codeQuality("${dir}")
    codeCoverage("${dir}")
}

def codeCheckout(gitUrl, gitBranch) {
    stage("Clonning codebase") {
        git branch: "${gitBranch}",
        url: "${gitUrl}"
    }
}

def codeStability(dir) {
    stage("Code stability") {
        sh "cd ${dir}; mvn clean package"
    }
}

def codeQuality(dir) {
    stage("Code quality") {
        sh "cd ${dir}; mvn checkstyle:checkstyle"
        recordIssues(tools: [checkStyle()])
    }
}

def codeCoverage(dir) {
    stage("Code coverage") {
        sh "cd ${dir}; mvn 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
    }
}