SonarScanner 已在 Jenkins 工具中配置,但“${scannerHome}”未被识别为内部或外部命令

SonarScanner is configured in Jenkins Tools, but '${scannerHome}' is not recognized as an internal or external command

我想在我的 Jenkinsfile 中使用 SonarQube

pipeline{
    agent any 
    stages{
        stage('build'){
            steps{
                // invoke command to build with maven
                bat 'mvn clean install'
            }
        }

        stage('SonarQube') {
            environment {
                scannerHome = tool 'SonarQubeScanner'
            }
            steps {
                withSonarQubeEnv('SonarQube') { 
                    bat '${scannerHome}/bin/sonar-scanner.bat'
                }
            }
         }
    }
}

这是我的 SonarQube 服务器

这是 SonarScanner

withSonarQubeEnv 步骤有什么问题:

withSonarQubeEnv('SonarQube') { 
    bat '${scannerHome}/bin/sonar-scanner.bat'
}

我总是出错

'${scannerHome}' is not recognized as an internal or external command,

我看到两个问题:

  1. 您没有向 SonarQubeScanner 工具添加任何安装程序(仅选中复选框)
  2. 代码不正确

不评估单引号(按原样处理)。这意味着:

def value = 'ABC'
println '${value}/bin/sonar-scanner.bat'

打印 ${value}/bin/sonar-scanner.bat。你必须使用双引号:

def value = 'ABC'
println "${value}/bin/sonar-scanner.bat"

打印 ABC/bin/sonar-scanner.bat.

代码应等于:

withSonarQubeEnv('SonarQube') { 
    bat "${scannerHome}/bin/sonar-scanner.bat"
}