Skip to content

Instantly share code, notes, and snippets.

@Yu-Jack
Last active January 17, 2021 08:24
Show Gist options
  • Save Yu-Jack/f7f03ca8dccdd04bed4a1428e48eb7af to your computer and use it in GitHub Desktop.
Save Yu-Jack/f7f03ca8dccdd04bed4a1428e48eb7af to your computer and use it in GitHub Desktop.
def has_project_in_sonarqube
pipeline {
agent any
environment {
base_branch = 'develop'
NODEJS_HOME = "${tool 'node14'}"
PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
}
stages {
stage("Sonarqube check project exist") {
steps {
script {
def result = build(
propagate: false,
job: 'SonarqubeJob',
parameters: [
string(name: 'project_path', value: "${WORKSPACE}"),
string(name: 'action', value: 'check'),
string(name: 'branch', value: "${branch}"),
string(name: 'git_repository_name', value: "${git_repository_name}")
]
)
has_project_in_sonarqube = "${result.getResult()}"
sh """
echo "Checking Result: ${has_project_in_sonarqube}"
"""
}
}
}
stage("Fetch base branch code to initilize") {
steps {
script {
if (has_project_in_sonarqube == "FAILURE") {
git url: "https://github.com/Yu-Jack/${git_repository_name}.git", branch: "${base_branch}"
} else {
echo "Sonarqube project exist"
}
}
}
}
stage("Install base branch dependency") {
steps {
script {
if (has_project_in_sonarqube == "FAILURE") {
if (project_type == "java") {
sh """
./gradlew build
"""
}
if (project_type == "nodejs") {
sh """
npm install
"""
}
} else {
echo "Sonarqube project exist"
}
}
}
}
stage("Unit test for base branch") {
steps {
script {
if (has_project_in_sonarqube == "FAILURE") {
if (project_type == "java") {
sh """
./gradlew test
"""
}
if (project_type == "nodejs") {
sh """
npm run test
"""
}
} else {
echo "Sonarqube project exist"
}
}
}
}
stage("Sonarqube scan base branch") {
steps {
script {
if (has_project_in_sonarqube == "FAILURE") {
build(
job: 'SonarqubeJob',
parameters: [
string(name: 'project_path', value: "${WORKSPACE}"),
string(name: 'project_type', value: "${project_type}"),
string(name: 'action', value: 'scan'),
string(name: 'branch', value: "${branch}"),
string(name: 'git_repository_name', value: "${git_repository_name}")
]
)
} else {
echo "Sonarqube project exist"
}
}
}
}
stage('Fetch lastest code') {
steps {
git url: "https://github.com/Yu-Jack/${git_repository_name}.git", branch: "${branch}"
}
}
stage('Install dependency') {
steps {
script {
if (project_type == "java") {
sh "./gradlew build"
}
if (project_type == "nodejs") {
sh "npm install"
}
}
}
}
stage('Unit test') {
steps {
script {
if (project_type == "java") {
sh "./gradlew test"
}
if (project_type == "nodejs") {
sh "npm run test"
}
}
}
}
stage("Sonarqube scanning") {
steps {
build(
job: 'SonarqubeJob',
parameters: [
string(name: 'project_path', value: "${WORKSPACE}"),
string(name: 'project_type', value: "${project_type}"),
string(name: 'action', value: 'scan'),
string(name: 'branch', value: "${branch}"),
string(name: 'git_repository_name', value: "${git_repository_name}")
]
)
}
}
}
}
@Yu-Jack
Copy link
Author

Yu-Jack commented Jan 12, 2021

Sonarqube Scan Pipeline

def full_name
pipeline {
    agent any
    
    environment {
        scannerHome = tool name: 'sonarqube-scanner'
        sonarqube_server = 'http://sonarqube:9000'
        NODEJS_HOME = "${tool 'node14'}"
        PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
    }
    
    stages {
        stage("Sonarqube Checking") {
            steps {
                script {
                    String temp = "${branch}"
                    sonarqube_branch_name = temp.replaceAll("/", "-")
                    full_name = "${git_repository_name}:${sonarqube_branch_name}"
                    
                    if (action == "check") {
                        final def (String response, String code) =
                            sh(script: "curl -u 00f31133302ea8d7fdec5e3ff72fbb67d3b7632d: -s -w '\\n%{response_code}' ${sonarqube_server}/api/components/show?component=${full_name}", returnStdout: true)
                                .trim()
                                .tokenize("\n")
                        sh """
                        if [ "$code" != "200" ]; then
                            echo "Project doesn't exist"
                            exit 1
                        fi;
                        """
                    }
                }
            }
        }
        
        stage("Sonarqube Scanning") {
            steps {
                script {
                    if (action == "scan") {
                        withSonarQubeEnv('local-sonarqube') {
                            sh """
                                cd $project_path
                                if [ "$project_type" = "nodejs" ]; then
                                    ${scannerHome}/bin/sonar-scanner \
                                        -Dsonar.projectKey=${full_name} \
                                        -Dsonar.testExecutionReportPaths=xunit.xml \
                                        -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info \
                                        -Dsonar.tests=test -Dsonar.test.inclusions=test/**/*.test.js \
                                        -Dsonar.sources=. \
                                        -Dsonar.exclusions=".gradle/**/*.*, .scannerwork/**/*.*, build/**/*.*"
                                fi;
                                if [ "$project_type" = "java" ]; then
                                    ${scannerHome}/bin/sonar-scanner \
                                        -Dsonar.projectKey=${full_name} \
                                        -Dsonar.java.binaries=build/classes \
                                        -Dsonar.sourceEncoding=UTF-8 \
                                        -Dsonar.tests=src/test \
                                        -Dsonar.test.inclusions="src/test/**/*.java" \
                                        -Dsonar.sources="src" \
                                        -Dsonar.junit.reportPaths=build/test-results/test
                                fi;
                            """
                        }
                    }
                }
            }
        }
        
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment