【问题标题】:How to setup sonar scanner in Jenkins Declarative Pipeline如何在 Jenkins 声明式管道中设置声纳扫描仪
【发布时间】:2020-01-08 09:20:42
【问题描述】:

我在 Jenkinsfile 中为我的存储库实施 SonarQube 扫描器时遇到了问题。我不知道应该在 Jenkinsfile 中哪里添加 SonarQube 扫描仪的属性。

我已经在我的 Windows 系统上本地设置了 Jenkins。这些项目完全基于 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'
             """
        }
      }
    }
}

属性包括:

 -----------------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

我可以在 sonar-project.properties 文件中手动添加这些属性,并直接在我的项目根目录中设置此文件,但它将在本地而不是在服务器上运行。所以为了避免我想将这些属性添加到 Jenkinsfile

【问题讨论】:

标签: jenkins sonarqube jenkins-pipeline sonarqube-scan


【解决方案1】:

我们将 Sonar 扫描仪作为 Docker 容器运行,但它应该能让您大致了解如何在 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="
    }     
}

【讨论】:

    【解决方案2】:

    您像这样运行管道步骤。声纳服务器属性可以在 pom.xml 文件的配置文件下定义。

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

    SonarQube 扫描器需要在 Jenkins 全局工具配置部分定义。

    【讨论】:

    • 抱歉,我确实看到了上述问题的语言值。您可以将扫描仪定义为环境变量并启动扫描仪以启动项目。
    猜你喜欢
    • 2018-07-11
    • 2017-08-19
    • 2017-08-31
    • 2017-12-30
    • 2019-10-27
    • 1970-01-01
    • 2016-12-01
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多