无法让Sonarqube在Jenkins声明管道中运行

时间:2018-05-29 08:58:24

标签: jenkins sonarqube jenkins-pipeline

我按照以下文档提供: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins

然而,我无法让它发挥作用。 首先,似乎文档需要更新,因为示例中的语法是错误的。在最新版本的声明性管道中,steps - 标记内部必须stage。 此外,def - 关键字只有在script - 标记内时才会被解析。

除此之外,在下面运行Jenkinsfile时,我会收到一个NPE:

java.lang.NullPointerException
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:150)
    at org.jenkinsci.plugins.workflow.steps.ToolStep$Execution.run(ToolStep.java:133)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:49)
    at hudson.security.ACL.impersonate(ACL.java:290)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:46)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Jenkinsfile

pipeline {
  environment {
    scannerHome = tool 'SonarQube Scanner 3.2.0.1227'
  }
  agent {
    node {
      label "master"
    }
  }

  options {
    buildDiscarder logRotator(daysToKeepStr: '7')
  }

  stages {
    stage("Sonarqube analysis") {
      steps {
        withSonarQubeEnv('SonarQube Scanner') {
          bat "${scannerHome}/bin/sonar-scanner"
        }
      }
    }
  • Jenkins:v2.107.3
  • 用于Jenkins插件的SonarQube扫描仪:v 2.7.1
  • SonarQube扫描仪:3.2.0.1227(设置为在全局工具配置中自动安装)
  • 在master(Windows Server 2012 R2)上运行作业

1 个答案:

答案 0 :(得分:0)

修正了以下内容:

pipeline {
  agent {
    node {
      label "master"
    }
  }

  stages {
    stage("SonarQube analysis") {
       steps {
          script {
              def sonarScanner = tool name: 'SonarQube', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
              bat "${sonarScanner}/bin/sonar-scanner -e -Dsonar.host.url=xxx"
            }
         }
      }
    }
  }