Adding SonarQube to a C++ build on Jenkins

  • ALM used: Bitbucket Cloud
  • CI system used: Jenkins
  • Languages of the repository: C++

I’m trying to add SonarQube integration to our Jenkins build server. I followed the instructions from our in-house server to setup Jenkins then downloaded sonarqube-build-wrapper-linux-x86.zip and installed the files on the build agent in the PATH. We’re using a multibranch pipeline build configured by Jenkinsfile in the source repo so I setup a test branch with:

        stage('SonarQube analysis') {
            def scannerHome = tool 'SonarScanner 4.0'
            withSonarQubeEnv() {
                sh "${scannerHome}/bin/sonar-scanner"
            }
        }

But there doesn’t seem to be a sonar-scanner executable installed. sonarqube-build-wrapper-linux-x86.zip contains:

  • build-wrapper-linux-x86-64
  • libinterceptor-haswell.so
  • libinterceptor-i686.so
  • libinterceptor-x86_64.so

and I can run build-wrapper-linux-x86-64 -h fine. Is sonar-scanner supposed to be included in the zip?
Also, this code doesn’t work in Jenkins - I get:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 29: Not a valid stage section definition: "def scannerHome = tool 'SonarScanner 4.0'". Some extra configuration is required. @ line 29, column 9.
           stage('SonarQube analysis') {
           ^

WorkflowScript: 29: Unknown stage section "withSonarQubeEnv". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. @ line 29, column 9.
           stage('SonarQube analysis') {
           ^

WorkflowScript: 29: Expected one of "steps", "stages", or "parallel" for stage "SonarQube analysis" @ line 29, column 9.
           stage('SonarQube analysis') {
           ^

3 errors

I tried wrapping it in steps so:

        stage('SonarQube analysis') {
            steps {
                def scannerHome = tool 'SonarScanner 4.0'
                withSonarQubeEnv() {
                    sh "${scannerHome}/bin/sonar-scanner"
                }
            }
        }

but this fails with:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 31: Expected a step @ line 31, column 17.
                   def scannerHome = tool 'SonarScanner 4.0'
                   ^

WorkflowScript: 32: Missing required parameter: "installationName" @ line 32, column 17.
                   withSonarQubeEnv() {
                   ^

2 errors

There’s a lot of contradictory advice on this, would be good to have a simple example to follow.

Looking around some of the previous questions here made me realise that build-wrapper-linux-x86-64 is literally a build wrapper, so should be used like this:

mkdir -p build_wrapper_output_directory
build-wrapper-linux-x86-64 --out-dir build_wrapper_output_directory ./build.sh

This is working for me now, and I’m seeing the build_wrapper_output_directory contain build-wrapper-dump.json and build-wrapper.log.

But I still can’t run sonar-scanner. Where is this supposed to come from?

1 Like

Hi,

Yes, the build-wrapper and the scanner are two separate pieces, and the build-wrapper is only needed for projects containing C, C++ or Objective-C.

The docs will get you started with the scanner.

 
HTH,
Ann

I’ve followed the docs:
I’m using Jenkins so Jenkins extension for SonarQube

  1. Install the Jenkins Extension for SonarQube via the Jenkins Update Center.
  2. Configure your SonarQube server.

Then Jenkins extension for SonarQube - Analyzing other project types

But sonar-scanner still isn’t present. What am I missing?

I tried adding the example to my Jenkinsfile:

        stage('SonarQube analysis') {
            steps {
                withSonarQubeEnv('My SonarQube Server', envOnly: true) {
                    // This expands the evironment variables SONAR_CONFIG_NAME, SONAR_HOST_URL, SONAR_AUTH_TOKEN that can be used by any script.
                    println ${ env.SONAR_HOST_URL }
                }
            }
        }

but I get:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 32: ${ env.SONAR_HOST_URL } cannot be used as a value directly. Did you mean "${ env.SONAR_HOST_URL }"? (add quotes) @ line 32, column 29.
                       println ${ env.SONAR_HOST_URL }
                               ^

WorkflowScript: 30: Arguments to "withSonarQubeEnv" must be explicitly named. @ line 30, column 17.
                   withSonarQubeEnv('My SonarQube Server', envOnly: true) {
                   ^

2 errors

Hi,

That sends you to the SonarQube docs. Did you follow this part of that documentation, where it tells you to

Scroll down to the SonarScanner configuration section and click on Add SonarScanner.

?

 
Ann

Yes.

I assume “2. Add the SonarScanner build step to your build.” is adding the step to the Jenkinsfile?

Hi,

You mean this part?

  1. Scroll down to the SonarScanner configuration section and click on Add SonarScanner. It is based on the typical Jenkins tool auto-installation.

That’s the part where you use Jenkins to install the scanner on your build agent.

Or this?

  1. Add the SonarScanner build step to your build.

Yes, once you’ve installed a scanner on your build agent, then that’s the part where you configure its use in your pipeline.

 
Ann

For the installation “Install from Maven Central” has been automatically selected. Is that wrong?

Hi,

…Good point.

I think maybe the vanilla scanner - the one you need - used to be distributed to Maven Central, but probably not any more. I’m going to raise this to the docs team; we probably need to take a look at that.

In the meantime, you can download the scanner from here.

 
HTH,
Ann

I’ve changed Jenkins to install from the .zip listed on that page but it’s still failing to find sonar-scanner. Does the install get triggered by something like withSonarQubeEnv in Jenkinsfile? In which case what’s the format for this, because I keep getting errors when I try to add it.

Should I be able to see the .zip being installed in the Jenkins log?