简体   繁体   中英

How to setup sonar scanner in Jenkins Declarative Pipeline

I'm facing a problem in implementing SonarQube scanner for my repository in Jenkinsfile. I don't know where should I add the properties of SonarQube scanner in the Jenkinsfile.

I've set Jenkins locally on my windows system. The projects are purely based on Python, Ruby & React.

  agent {label 'master'}
  triggers {
    GenricTrigger ([
    genricVariables: [
    key: 'pr_from_branch', value: '$.pullrequest.source.branch.name'],
    [
    expressionType: 'JsonPath',
    regexpFilter: '',
    defaultValue: ''],
    token: 'test'])
  }
  options {
    buildDiscarder (
      logRotator(numToKeepStr:'5'))
    }
   stages {
      stage ('Initialize & SonarQube Scan') {
        steps {
        def scannerHome = tool 'sonarScanner';
        withSonarQubeEnv('My SonarQube Server') {

          bat """
             ${scannerHome}/bin/sonar-runner.bat
             pip install -r requirements.txt
             """
             }
          }
      }
      stage('Quality Gate') {
      sleep time: 3000, unit: 'MILLISECONDS'
        timeout(time: 1, unit: 'MINUTES') { // Just in case something goes wrong, pipeline will be killed after a timeout
        def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
        if (qg.status != 'OK') {
        error "Pipeline aborted due to quality gate failure: ${qg.status}"
        }
       }
      }
      stage ('Smoke Test') {
        steps {
          bat """
             pytest -s -v tests/home/login_test.py
             currentBuild.result = 'SUCCESS'
             """
        }
      }
    }
}

The properties include:

 -----------------Sonarqube configuration........................

sonar.projectKey=<*****>
sonar.projectName=<project name>
sonar.projectVersion=1.0
sonar.login=<sonar-login-token>
sonar.sources=src
sonar.exclusions=**/*.doc,**/*.docx,**/*.ipch,/node_modules/,
sonar.host.url=http://<url>/

-----------------Sonar for bitbucket plugin configuration...................

sonar.bitbucket.repoSlug=<project name>
sonar.bitbucket.accountName=<name>
sonar.bitbucket.oauthClientKey=<OAuth_Key>
sonar.bitbucket.oauthClientSecret=<OAuth_secret>
sonar.analysis.mode=issues

I can manually add these properties in sonar-project.properties file and set this file in my project root directly but it will be running locally not on the server. So to avoid that I want to add these properties to Jenkinsfile

We run Sonar scanner as a Docker container but it should give you a fair idea of how to use your properties for the same in Jenkinsfile.

stage("Sonar Analysis"){
    sh "docker pull docker.artifactory.company.com/util-sonar-runner:latest"

    withSonarQubeEnv('sonarqube'){
        sh "docker run --rm -v ${workspace}:/opt/spring-service -w /opt/spring-service -e SONAR_HOST_URL=${SONAR_HOST_URL} -e SONAR_AUTH_TOKEN=${SONAR_AUTH_TOKEN} docker.artifactory.company.com/util-sonar-runner:latest /opt/sonar-scanner/bin/sonar-scanner -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_AUTH_TOKEN} -Dsonar.projectKey=spring-service -Dsonar.projectName=spring-service  -Dsonar.projectBaseDir=. -Dsonar.sources=./src -Dsonar.java.binaries=./build/classes -Dsonar.junit.reportPaths=./build/test-results/test -Dsonar.jacoco.reportPaths=./build/jacoco/test.exec -Dsonar.exclusions=src/test/java/**/* -Dsonar.fortify.reportPath=fortifyResults-${IMAGE_NAME}.fpr -Dsonar.password="
    }     
}

You run the pipeline step like this. The sonar server properties can be defined under the profile of the pom.xml file.

steps {
      withSonarQubeEnv('SonarQube') {
       sh 'mvn -Psonar -Dsonar.sourceEncoding=UTF-8 org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.2:sonar'
      }
 }

The SonarQube scanner needs to be defined on Jenkins Global tool Configuration section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM