Lgalan90
Lgalan90

Reputation: 615

Jenkins Pipeline and SonarQube setup

I'm having some trouble setting up SonarQube using Maven on a Jenkins Pipeline.

My pipeline pulls the git repo into the directory created for it and it goes through the rest of the steps successfully but I don't see the test results on SonarQube nor any output that tests are being ran.

Here is my code set up on the pipeline:

node('master'){

  stage('Git Clone') {
    dir('my-git-dir'){
    git branch: '$GIT_BRANCH'
      git url: '$GIT_REPO'
      credentialsId: '11111111-111-1111-1111-111111111111'
   }
  }

  stage('build & SonarQube Scan') {
      MVN="/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.3.9/bin/mvn"
      echo "running clean verify sonar"
      "$MVN clean verify sonar:sonar -Dsonar.host.url=http://111.11.1.111:9000 -Dsonar.java.binaries=/etc/sonarqube"
      echo "running clean install"
      "$MVN clean install deploy -DskipTests"
  }
}

The command runs just fine on a free style project:

#!/usr/bin/bash
MVN="/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.3.9/bin/mvn"
$MVN clean verify sonar:sonar -Dsonar.host.url=http://111.11.1.111:9000 -Dsonar.java.binaries=/etc/sonarqube
$MVN clean install deploy -DskipTests

It also has an "Invoke top-level Maven targets" Maven Version:

Maven 3.3.9

Goals:

test
-fn

Edit: Working Script

  stage('SonarQube analysis') {
     dir("$gitRepo"){
    withSonarQubeEnv('SonarQube') {
      sh "pwd"
      MVN="/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.3.9/bin/mvn"
      echo "Running JaCoCO stuff"
      sh "$MVN clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false"
      echo "running clean verify"
      sh "$MVN clean verify sonar:sonar -Dsonar.host.url=http://111.11.1.111:9000" //-Dsonar.java.binaries=/etc/sonarqube"
      echo "running clean install deploy"
      sh "$MVN clean install deploy -DskipTests"
}

P.S I am trying to create a job that you can select a repo and branch to pull from, create/use a file in that workspace to run SonarQube on

Upvotes: 2

Views: 5170

Answers (1)

G. Ann - SonarSource Team
G. Ann - SonarSource Team

Reputation: 22794

Your question demonstrates several misunderstandings.

First, SonarQube analysis will not execute your tests for you; you need to fire that off yourself. As noted in the docs the command for that would be something like

$MVN clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false

followed by

$MVN sonar:sonar -sonar.host.url=http://111.11.1.111:9000

Note that I've left off the -Dsonar.java.binaries=/etc/sonarqube parameter you used. That's because

  • This is the path to the compiled classes of your project. They should not be in /etc/sonarqube. (Move them if they are.)
  • Maven analysis already knows where your binaries are so you don't need to provide the path unless your build is configured to put them somewhere strange.

Upvotes: 3

Related Questions