Search code examples
jenkinssonarqube

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


i'm tring to use SonarQube inside my Jenkinsfile

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

this is my SonarQube server SonarQube

and this is SonarScanner

sonarScanner

what is wrong with withSonarQubeEnv step:

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

that I always got an error

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


Solution

  • I see two problems:

    1. you didn't add any installer to SonarQubeScanner tool (only checkbox is checked)
    2. the code is incorrect

    Single quotes are not evaluated (treat as is). It means that:

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

    prints ${value}/bin/sonar-scanner.bat. You have to use double quotes:

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

    prints ABC/bin/sonar-scanner.bat.

    The code should be equal to:

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