Fail to scan projects with Jenkins and Sonarqube Scanner

SonarQube version:

  • SonarQube Community Build v25.1.0.102122
  • SonarQube Scanner 5.0.1.3006
    SonarQube deployed with Docker behind Caddy

Problem:
I use Jenkins scripted pipeline with SonarQube Scanner to scan my project. The token currently used does not expire, but I got this log

ERROR Failed to query server version: GET https://mysonarqube.server/api/v2/analysis/version failed with HTTP 401. Please check the property sonar.token or the environment variable SONAR_TOKEN.

I create a new token, but it’s still the same.

env | grep SONAR

SONAR_HOST_URL=https://mysonarqube.server
SONAR_CONFIG_NAME=sonarqube-iot
SONARQUBE_SCANNER_PARAMS={ "sonar.host.url" : "https:\/\/mysonarqube.server", "sonar.token" : "******"}
SONAR_AUTH_TOKEN=******
SONAR_MAVEN_GOAL=sonar:sonar

I am still able to log in via the dashboard UI. But keep failing on the scanning project.

Hi @suksest

It seems that you are facing an authentication issue when using SonarQube with Jenkins. Here’s how you can proceed to resolve this problem:

Steps to Follow

  1. Check SonarQube Configuration in Jenkins :
    1. Go to Jenkins administration, then navigate to “Manage Jenkins” > “Configure System” .
    2. Locate the SonarQube servers section and ensure the URL of your SonarQube server (SONAR_HOST_URL ) is correctly entered.
  2. Verify Credentials :
    3. In the same section, check the credentials associated with the SonarQube server. You should have a valid token configured.
    4. If necessary, create a new token in the SonarQube interface and update the credentials in Jenkins.
  3. Using withSonarQubeEnv() :
    5. Make sure you are using the withSonarQubeEnv() method correctly by specifying the correct configuration name. For example, use:
1withSonarQubeEnv('sonarqube-staging') {
2    // your analysis code here
3}
  1. Check Your Environment Variables :
    6. Ensure that environment variables do not contain empty values for SONAR_TOKEN or other relevant parameters.
    7. The SONARQUBE_SCANNER_PARAMS variable should include the token and must not be empty. Confirm that it is filled out correctly.

This indicates missing authentication, not invalid (or expired) authentication. So for some reason, your credentials are never making it to the scanner.

I echo @Bachri_Abdel’s suggestions – make sure you have configured the credentials and that you are wrapping the scanner with withSonarQubeEnv().

If you run into issues, I suggest sharing your complete scripted pipeline.

1 Like