-
-
Save Yu-Jack/f7f03ca8dccdd04bed4a1428e48eb7af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | |
] | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sonarqube Scan Pipeline