Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jenkinsfile and Quality gate SonarQube
stage("checkout") {
node("usine") {
deleteDir()
git changelog: false, poll: false, url: 'ssh://git@git.groupe.generali.fr:7999/sandbox/gs-spring-boot-docker.git'
// checkout scm
stash name: 'sources', includes: '**/**'
stash name: 'dockerfile', includes: 'Dockerfile'
}
}
stage("build & QA Scan") {
parallel 'build':{
node("usine") {
deleteDir()
unstash 'sources'
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
sh "mvn clean package"
}
stash includes: "target/app.jar", name: 'binary'
}
}, 'QA Scan':{
node("sonar") {
deleteDir()
unstash 'sources'
withSonarQubeEnv('sonarqube-rec') {
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
// requires SonarQube Scanner for Maven 3.2+
sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
def props = getProperties("target/sonar/report-task.txt")
env.SONAR_CE_TASK_URL = props.getProperty('ceTaskUrl')
}
}
}
}
}
stage("Quality Gate"){
node("sonar") {
withSonarQubeEnv('sonarqube-rec') {
def ceTask
timeout(time: 1, unit: 'MINUTES') {
waitUntil {
sh 'curl -u $SONAR_AUTH_TOKEN $SONAR_CE_TASK_URL -o ceTask.json'
ceTask = jsonParse(readFile('ceTask.json'))
echo ceTask.toString()
return "SUCCESS".equals(ceTask["task"]["status"])
}
}
def qualityGateUrl = env.SONAR_HOST_URL + "/api/qualitygates/project_status?analysisId=" + ceTask["task"]["analysisId"]
sh "curl -u $SONAR_AUTH_TOKEN $qualityGateUrl -o qualityGate.json"
def qualitygate = jsonParse(readFile('qualityGate.json'))
echo qualitygate.toString()
if ("ERROR".equals(qualitygate["projectStatus"]["status"])) {
error "Quality Gate failure"
}
echo "Quality Gate success"
}
}
}
stage('approve') {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to continue?'
}
}
node("docker") {
deleteDir()
docker.withServer('tcp://127.0.0.1:2375') {
stage("Build docker image") {
unstash 'dockerfile'
unstash 'binary'
def myEnv = docker.build 'spring-boot-demo:snapshot'
}
stage("Run container") {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to test image?'
}
docker.image('spring-boot-demo:snapshot').withRun('-p 8080:8080') {c ->
sh "docker logs ${c.id}"
timeout(1) {
input 'Stop container?'
}
}
}
}
}
def Properties getProperties(filename) {
def properties = new Properties()
properties.load(new StringReader(readFile(filename)))
return properties
}
@NonCPS
def jsonParse(text) {
return new groovy.json.JsonSlurperClassic().parseText(text);
}
@NonCPS
def jsonParse(URL url, String basicAuth) {
def conn = url.openConnection()
conn.setRequestProperty( "Authorization", "Basic " + basicAuth )
InputStream is = conn.getInputStream();
def json = new groovy.json.JsonSlurperClassic().parseText(is.text);
conn.disconnect();
return json
}
def url
def sonarBasicAuth
def sonarServerUrl
stage("checkout") {
node("usine") {
deleteDir()
git changelog: false, poll: false, url: 'ssh://git@git.groupe.generali.fr:7999/sandbox/gs-spring-boot-docker.git'
// checkout scm
stash name: 'sources', includes: '**/**'
stash name: 'dockerfile', includes: 'Dockerfile'
}
}
stage("build & QA Scan") {
parallel 'build':{
node("usine") {
deleteDir()
unstash 'sources'
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
sh "mvn clean package"
}
stash includes: "target/app.jar", name: 'binary'
}
}, 'QA Scan':{
node("sonar") {
deleteDir()
unstash 'sources'
withSonarQubeEnv('sonarqube-rec') {
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
// requires SonarQube Scanner for Maven 3.2+
sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
def props = getProperties("target/sonar/report-task.txt")
sonarServerUrl=props.getProperty('serverUrl')
url = new URL(props.getProperty('ceTaskUrl'))
sonarBasicAuth = SONAR_AUTH_TOKEN + ":"
sonarBasicAuth = sonarBasicAuth.getBytes().encodeBase64().toString()
}
}
}
}
}
stage("Quality Gate"){
def ceTask
timeout(time: 1, unit: 'MINUTES') {
waitUntil {
ceTask = jsonParse(url, sonarBasicAuth)
echo ceTask.toString()
return "SUCCESS".equals(ceTask["task"]["status"])
}
}
url = new URL(sonarServerUrl + "/api/qualitygates/project_status?analysisId=" + ceTask["task"]["analysisId"])
def qualitygate = jsonParse(url, sonarBasicAuth)
echo qualitygate.toString()
if ("ERROR".equals(qualitygate["projectStatus"]["status"])) {
error "Quality Gate Failure"
}
echo "Quality Gate Success"
}
stage('approve') {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to continue?'
}
}
node("docker") {
deleteDir()
docker.withServer('tcp://127.0.0.1:2375') {
stage("Build docker image") {
unstash 'dockerfile'
unstash 'binary'
def myEnv = docker.build 'spring-boot-demo:snapshot'
}
stage("Run container") {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to test image?'
}
docker.image('spring-boot-demo:snapshot').withRun('-p 8080:8080') {c ->
sh "docker logs ${c.id}"
timeout(1) {
input 'Stop container?'
}
}
}
}
}
def Properties getProperties(filename) {
def properties = new Properties()
properties.load(new StringReader(readFile(filename)))
return properties
}
@NonCPS
def jsonParse(text) {
return new groovy.json.JsonSlurperClassic().parseText(text);
}
@NonCPS
def jsonParse(URL url, String basicAuth) {
def conn = url.openConnection()
conn.setRequestProperty( "Authorization", "Basic " + basicAuth )
InputStream is = conn.getInputStream();
def json = new groovy.json.JsonSlurperClassic().parseText(is.text);
conn.disconnect();
return json
}
stage("checkout") {
node("usine") {
deleteDir()
//git changelog: false, poll: false, url: 'ssh://git@git.groupe.generali.fr:7999/sandbox/gs-spring-boot-docker.git'
checkout scm
stash name: 'sources', includes: '**/**'
stash name: 'dockerfile', includes: 'Dockerfile'
}
}
stage("build & QA Scan") {
parallel 'build':{
node("usine") {
deleteDir()
unstash 'sources'
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
sh "mvn clean package"
}
stash includes: "target/app.jar", name: 'binary'
}
}, 'QA Scan':{
node("sonar") {
deleteDir()
unstash 'sources'
withSonarQubeEnv('sonarqube-rec') {
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
// requires SonarQube Scanner for Maven 3.2+ and auth token
sh '''
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar
echo "SONAR_AUTH_TOKEN=$SONAR_AUTH_TOKEN" >> target/sonar/report-task.txt
'''
stash includes: "target/sonar/report-task.txt", name: 'sonar-report-task'
}
}
}
}
}
stage("Quality Gate"){
node("sonar") {
deleteDir()
unstash 'sonar-report-task'
def props = getProperties("target/sonar/report-task.txt")
echo "properties=${props}"
def sonarServerUrl=props.getProperty('serverUrl')
def ceTaskUrl= props.getProperty('ceTaskUrl')
def ceTask
def sonarAuthToken = props.getProperty('SONAR_AUTH_TOKEN')
timeout(time: 1, unit: 'MINUTES') {
waitUntil {
sh "curl -u $sonarAuthToken $ceTaskUrl -o ceTask.json"
ceTask = jsonParse(readFile('ceTask.json'))
echo ceTask.toString()
return "SUCCESS".equals(ceTask["task"]["status"])
}
}
def qualityGateUrl = sonarServerUrl + "/api/qualitygates/project_status?analysisId=" + ceTask["task"]["analysisId"]
sh "curl -u $sonarAuthToken $qualityGateUrl -o qualityGate.json"
def qualitygate = jsonParse(readFile('qualityGate.json'))
echo qualitygate.toString()
if ("ERROR".equals(qualitygate["projectStatus"]["status"])) {
error "Quality Gate failure"
}
}
}
stage('approve') {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to continue?'
}
}
node("docker") {
deleteDir()
docker.withServer('tcp://127.0.0.1:2375') {
stage("Build docker image") {
unstash 'dockerfile'
unstash 'binary'
def myEnv = docker.build 'spring-boot-demo:snapshot'
}
stage("Run container") {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to test image?'
}
docker.image('spring-boot-demo:snapshot').withRun('-p 8080:8080') {c ->
sh "docker logs ${c.id}"
timeout(1) {
input 'Stop container?'
}
}
}
}
}
def Properties getProperties(filename) {
def properties = new Properties()
properties.load(new StringReader(readFile(filename)))
return properties
}
@NonCPS
def jsonParse(text) {
return new groovy.json.JsonSlurperClassic().parseText(text);
}
stage("checkout") {
node("usine") {
deleteDir()
//git changelog: false, poll: false, url: 'ssh://git@git.groupe.generali.fr:7999/sandbox/gs-spring-boot-docker.git'
checkout scm
stash name: 'sources', includes: '**/**'
stash name: 'dockerfile', includes: 'Dockerfile'
}
}
stage("build & QA Scan") {
parallel 'build':{
node("usine") {
deleteDir()
unstash 'sources'
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
sh "mvn clean package"
}
stash includes: "target/app.jar", name: 'binary'
}
}, 'QA Scan':{
node("sonar") {
deleteDir()
unstash 'sources'
//def scannerHome = tool 'sonar-scanner';
withSonarQubeEnv('sonarqube-rec') {
withEnv(["JAVA_HOME=${ tool 'JDK_8.0' }", "PATH+MAVEN=${tool 'M325'}/bin:${env.JAVA_HOME}/bin"]) {
// requires SonarQube Scanner for Maven 3.2+ and auth token
sh '''
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar
echo "SONAR_AUTH_TOKEN=$SONAR_AUTH_TOKEN" >> target/sonar/report-task.txt
'''
stash includes: "target/sonar/report-task.txt", name: 'sonar-report-task'
}
}
}
}
}
stage("Quality Gate"){
node("sonar") {
deleteDir()
unstash 'sonar-report-task'
def props = getProperties("target/sonar/report-task.txt")
echo "properties=${props}"
def sonarAuthToken = props.getProperty('SONAR_AUTH_TOKEN')
def sonarServerUrl=props.getProperty('serverUrl')
def ceTaskUrl= props.getProperty('ceTaskUrl')
def ceTask
def URL url = new URL(ceTaskUrl)
def sonarBasicAuth = props.getProperty('SONAR_AUTH_TOKEN') + ":"
sonarBasicAuth = sonarBasicAuth.getBytes().encodeBase64().toString()
timeout(time: 1, unit: 'MINUTES') {
waitUntil {
ceTask = jsonParse(url, sonarBasicAuth)
echo ceTask.toString()
return "SUCCESS".equals(ceTask["task"]["status"])
}
}
url = new URL(sonarServerUrl + "/api/qualitygates/project_status?analysisId=" + ceTask["task"]["analysisId"] )
def qualitygate = jsonParse(url, sonarBasicAuth)
echo qualitygate.toString()
if ("ERROR".equals(qualitygate["projectStatus"]["status"])) {
error "Quality Gate failure"
}
}
}
stage('approve') {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to continue?'
}
}
node("docker") {
deleteDir()
docker.withServer('tcp://127.0.0.1:2375') {
stage("Build docker image") {
unstash 'dockerfile'
unstash 'binary'
def myEnv = docker.build 'spring-boot-demo:snapshot'
}
stage("Run container") {
timeout(time: 1, unit: 'MINUTES') {
input message: 'Do you want to test image?'
}
docker.image('spring-boot-demo:snapshot').withRun('-p 8080:8080') {c ->
sh "docker logs ${c.id}"
timeout(1) {
input 'Stop container?'
}
}
}
}
}
def Properties getProperties(filename) {
def properties = new Properties()
properties.load(new StringReader(readFile(filename)))
return properties
}
@NonCPS
def jsonParse(URL url, String basicAuth) {
def conn = url.openConnection()
conn.setRequestProperty( "Authorization", "Basic " + basicAuth )
//new groovy.json.JsonSlurperClassic().parse(url)
InputStream is = conn.getInputStream();
def json = new groovy.json.JsonSlurperClassic().parseText(is.text);
conn.disconnect();
return json
}
@Ecosmob

This comment has been minimized.

Copy link

commented Jan 28, 2017

It gives error "Unable to process url" while retrieving quality gate results.

@vdupain

This comment has been minimized.

Copy link
Owner Author

commented Jan 30, 2017

Requires one of the following permissions on Sonarqube project:
'Administer System'
'Administer' rights on the specified project
'Browse' on the specified project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.