Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup SonarQube scanner in jenkins for a js/ts/react project

I would like to have Sonar scanner running on my project when it builds in jenkins.

Something like this, Something like this

Most of the tutorials seem to only address this process from a Java perspective, So I am wondering how this can be done if at all.

I am doing some of the work out of a Jenkinsfile in my project:

stage('SonarQube') {
  environment {
    scannerHome = tool 'SonarQubeScanner'
  }
  steps {
    withSonarQubeEnv('SonarQubeScanner') {
      sh "${scannerHome}/bin/sonar-scanner"
    }
  }
}

I used the following link to get the project in SonarQube: https://nickkorbel.com/2020/02/05/configuring-sonar-with-a-create-react-app-in-typescript/

I get a couple different errors when the scan tries to run during the Jenkins Build:

Error 1

Could not find executable in "/opt/app-root/src/.sonar/native-sonar-scanner".

Proceed with download of the platform binaries for SonarScanner...
 Creating /opt/app-root/src/.sonar/native-sonar-scanner

Downloading from https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.4.0.2170-linux.zip

(executable will be saved in cache folder: /opt/app-root/src/.sonar/native-sonar-scanner)

ERROR: impossible to download and extract binary: connect ETIMEDOUT 

Error 2

ERROR: Failed to download https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.4.0.2170-linux.zip from agent; will retry from master

SonarQube installation defined in this job (sonarqube) does not match any configured installation. Number of installations that can be configured: 1.
like image 942
Jub10 Avatar asked Sep 05 '25 03:09

Jub10


1 Answers

Error 2 is about missing integration with sonarqube server.

Full install of sonarqube:

  1. Install SonarQube server
  2. Install the SonarQube Scanner plugin for Jenkins.
  3. Configure your SonarQube server(s):
  • Log into Jenkins as an administrator and go to Manage Jenkins > Configure System.
  • Scroll down to the SonarQube configuration section, click Add SonarQube, and add the values you're prompted for.
  • The server authentication token should be created as a 'Secret Text' credential.

withSonarQubeEnv('SonarQubeScanner') - "SonarQubeScanner" means the name of the Sonarqube server from step 3.

In the pipeline you should pass parameters for sonar-scanner tool, for example:

stage('SonarQube analysis') {
        environment {
            scannerHome = tool 'SonarQube_4.3.0'
        }
        steps {
            withSonarQubeEnv('Your Sonar Server Name here') {
                sh '''
                ${scannerHome}/bin/sonar-scanner \
                -D sonar.projectKey=YOUR_PROJECT_KEY_HERE \
                -D sonar.projectName=YOUR_PROJECT_NAME_HERE \
                -D sonar.projectVersion=YOUR_PROJECT_VERSION_HERE \
                -D sonar.languages=js,ts \  // DEPRECATED, do not use this option
                -D sonar.sources=./src \
                -D sonar.test.inclusions=YOUR_INCLUSIONS_HERE \
                -D sonar.exclusions=YOUR_EXCLUSIONS_HERE
                '''
            }
        }
    }

Suppose Error 1 will be fixed after you fix Error 2. Take a look at official documentation here

like image 128
Dmitriy Tarasevich Avatar answered Sep 07 '25 21:09

Dmitriy Tarasevich