Veranicus
Veranicus

Reputation: 27

Analyzing code with SonarQube from Jenkins pipeline while using docker container Sonar Scanner

I want to perform SonarQube analysis of a git repository code and I want to use SonarScanner from Docker Container, not from within Jenkins Configuration.

I've tried to create this pipeline:

pipeline {
    agent { docker { image 'emeraldsquad/sonar-scanner:latest' } }
    stages {
        stage('build && SonarQube analysis') {
            steps {
                withSonarQubeEnv('sonar.tools.devops.****') {
                    sh 'sonar-scanner \\ -Dsonar.projectKey=myProject \\ -Dsonar.sources=./src \\'
                }
            }
        }
        stage("Quality Gate") {
            steps {
                timeout(time: 1, unit: 'HOURS') {
                    // Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
                    // true = set pipeline to UNSTABLE, false = don't
                    // Requires SonarScanner for Jenkins 2.7+
                    waitForQualityGate abortPipeline: true
                }
            }
        }
    }
}

The build fails on stage build && SonarQube analysis, with build output:

Injecting SonarQube environment variables using the configuration: sonar.tools.devops.*****
[Pipeline] {
[Pipeline] sh
+ sonar-scanner ' -Dsonar.projectKey=myProject' ' -Dsonar.sources=./src' '\'
ERROR: Unrecognized option:  -Dsonar.sources=./src
INFO: 
INFO: usage: sonar-scanner [options]
INFO: 
INFO: Options:
INFO:  -D,--define <arg>     Define property
INFO:  -h,--help             Display help information
INFO:  -v,--version          Display version information
INFO:  -X,--debug            Produce execution debug output

Upvotes: 1

Views: 2360

Answers (1)

Neothorn
Neothorn

Reputation: 339

I would try to remove double backslashes between the arguments: sh 'sonar-scanner -Dsonar.projectKey=myProject -Dsonar.sources=./src'

The backslashes escape spaces which won't be trucated by the shell and are added to the argument's name.

Upvotes: 1

Related Questions