SonarQube Scanner fails in a Jenkins pipeline command not found

I’d like to run SonarQube Scanner from a Jenkins pipeline and I followed the documentation.

Regarding the error, it seems that the scanner is present but some commands are not found. My jenkins instance runs in local

error message :

+ /usr/local/bin/sonar-scanner
/usr/local/Cellar/sonar-scanner/4.6.2.2472_1/libexec/bin/sonar-scanner: line 17: dirname: command not found
/usr/local/Cellar/sonar-scanner/4.6.2.2472_1/libexec/bin/sonar-scanner: line 18: basename: command not found
/usr/local/Cellar/sonar-scanner/4.6.2.2472_1/libexec/bin/sonar-scanner: line 28: dirname: command not found
File does not exist: //lib/sonar-scanner-cli-4.6.2.2472.jar
'/' does not point to a valid installation directory: /
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

script pipeline :

node {
stage(‘Preparation’) {
try{

    // Clone Project from bitbucket
    git url: 'https://test@bitbucket.org/test/test.git'
    
    }catch(err){
        echo err
    }
}

/* stage(“Build”) {
try{

    // copie .env
    sh 'cp .env.example .env'
    
    // Run `composer update` as a shell script
    sh 'composer update'
    
    // Clear Cache
    sh 'php artisan cache:clear'
    
    // clear config cache
    sh 'php artisan config:cache'
    
    }catch(err){
        echo err
    }
}
stage("phpunit") {
    try{
        
    // Run PHPUnit
    sh 'vendor/bin/phpunit'
    
    }catch(err){
        echo err
    }
}*/

stage(‘Sonarqube’) {

def scannerHome = tool 'sonarqube';
withSonarQubeEnv('sonarqube'){
    sh "${scannerHome}/bin/sonar-scanner \
            -D sonar.login=admin \
            -D sonar.password=amineleboss123 \
            -D sonar.projectKey=test \
            -D sonar.exclusions=vendor/**,resources/**,**/*.java \
            -D sonar.host.url=http://localhost:9000/"
            
}

}

stage('end') {  
 echo "Success" 
}

}

Hi @Outmal_Mohamed_Amine,

First of all, it’s better to use Jenkins configuration for SonarQube instead of running the analysis in command line. You can define SonarQube endpoint, token, SCM, etc. directly in Jenkins configuration.

Then regarding to your command, the property should be used like -Dsonar.projectKey=test without space after -D.

Best regards,
Fan

1 Like

Hi dear , thank you for you answer :slight_smile:
Im already using Jenkins configuration for sonarqube and add thé sonarqube server also configuration the auto installation for sonar scanner . I dont know where IS the problem exactly ans how i Can solve this error .
Same ERROR show when i use -Dsonar instead of -D sonar :frowning:

Hi @Outmal_Mohamed_Amine,

How do you configure SonarQube Scanner in Global Tool Configuration on Jenkins?

Best regards,
Fan