Search code examples
gojenkinssonarqubesonarqube-scan

How to run SonarQube for Go code using Jenkins


I need to run SonarQube for Go code using Jenkins. Unfortunately, there is little information regarding this task.

I have found that the "sonar-project.properties" file should be created, for example:

  sonar.projectKey=com.company.projectkey1
  sonar.projectName=My Project Name

  sonar.sources=.
  sonar.exclusions=**/*_test.go,**/vendor/**

  sonar.tests=.
  sonar.test.inclusions=**/*_test.go
  sonar.test.exclusions=**/vendor/**

But how to configure a Jenkins pipeline correctly? I found the following example, but I'm not sure if this is what I need

node {
  stage('SCM') {
    git '<my_path>.git'
  }
  stage('SonarQube analysis') {
    def scannerHome = tool 'SonarScanner 4.0';
    withSonarQubeEnv('My SonarQube Server') { 
      sh "${scannerHome}/bin/sonar-scanner"
    }
  }
}


Solution

  • Yes, that is correct.

    In your Jenkins pipeline you just need to run sonar-scanner - that is no different from other languages (see https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/). Go language plugin comes with SonarQube so no need to install anything. Scanner will then pickup project configuration from sonar-project.properties in your project root and perform the analysis.

    Your sonar-project.properties file seems like a copied example from SonarQube for Go page - you might want to do some adjustments depending on project layout (e.g. add sonar.inclusions=**/.*go). SonarQube should detect files are in Go automatically but you can also add sonar.language=go to that file to force it.

    You don't mention if you run SonarQube locally or in cluster mode - but there should be little or no difference (probably adding server url and login to sonar-project.properties in case of cluster installation).

    Note that go needs to be installed on scanner machines.