ERROR: Sonar server 'http://localhost:9000' can not be reached in GitLab Pipeline

Must-share information (formatted with Markdown):

  • which versions are you using: SonarQube with GitLab Integration
  • what are you trying to achieve: Trying to add sonarqube in my gitlab pipeline stages
  • what have you tried so far to achieve this: I tried to run sonarqube locally, deploy an instance of sonarqube, and add metadata in my sonar-project.properties file

I’m trying to integrate SonarQube into my CICD pipeline on GitLab but I am getting an error during the sonarqube stage on Gitlab. I followed the documentation for the GitLab integration so this is how my code looks like:

gitlab-ci.yml file:

sonarqube-check:
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner -Dsonar.qualitygate.wait=true
  allow_failure: true
  only:
    - sonarqube

In the root folder I have the sonar-project.properties file:

# must be unique in a given SonarQube instance
sonar.projectKey=<name>

# --- optional properties ---

# defaults to project key
sonar.projectName=application with SonarQube
# defaults to 'not provided'
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Defaults to .
sonar.sources=src

sonar.host.url=http://127.0.0.1:9000

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

I originally did localhost:9000 for the sonar.host.url but that still produces the following error:

ERROR: SonarQube server [http://127.0.0.1:9000] can not be reached
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 0.741s
ERROR: Error during SonarScanner execution
org.sonarsource.scanner.api.internal.ScannerException: Unable to execute SonarScanner analysis
INFO: Final Memory: 3M/14M
INFO: ------------------------------------------------------------------------
	at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.lambda$createLauncher$0(IsolatedLauncherFactory.java:85)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.createLauncher(IsolatedLauncherFactory.java:74)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.createLauncher(IsolatedLauncherFactory.java:70)
	at org.sonarsource.scanner.api.EmbeddedScanner.doStart(EmbeddedScanner.java:185)
	at org.sonarsource.scanner.api.EmbeddedScanner.start(EmbeddedScanner.java:123)
	at org.sonarsource.scanner.cli.Main.execute(Main.java:73)
	at org.sonarsource.scanner.cli.Main.main(Main.java:61)
Caused by: java.lang.IllegalStateException: Fail to get bootstrap index from server
	at org.sonarsource.scanner.api.internal.BootstrapIndexDownloader.getIndex(BootstrapIndexDownloader.java:42)
	at org.sonarsource.scanner.api.internal.JarDownloader.getScannerEngineFiles(JarDownloader.java:58)
	at org.sonarsource.scanner.api.internal.JarDownloader.download(JarDownloader.java:53)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherFactory.lambda$createLauncher$0(IsolatedLauncherFactory.java:76)
	... 7 more
Caused by: java.net.ConnectException: Failed to connect to /127.0.0.1:9000
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.RealConnection.connectSocket(RealConnection.java:265)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.RealConnection.connect(RealConnection.java:183)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ExchangeFinder.find(ExchangeFinder.java:88)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.Transmitter.newExchange(Transmitter.java:169)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.RealCall.getResponseWithInterceptorChain(RealCall.java:221)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.RealCall.execute(RealCall.java:81)
	at org.sonarsource.scanner.api.internal.ServerConnection.callUrl(ServerConnection.java:115)
	at org.sonarsource.scanner.api.internal.ServerConnection.downloadString(ServerConnection.java:99)
	at org.sonarsource.scanner.api.internal.BootstrapIndexDownloader.getIndex(BootstrapIndexDownloader.java:39)
	... 10 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
	at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
	at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source)
	at java.base/java.net.SocksSocketImpl.connect(Unknown Source)
	at java.base/java.net.Socket.connect(Unknown Source)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.platform.Platform.connectSocket(Platform.java:130)
	at org.sonarsource.scanner.api.internal.shaded.okhttp.internal.connection.RealConnection.connectSocket(RealConnection.java:263)
	... 31 more
ERROR: 
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.

I also created two environmental variables in GitLab like the documentation says: SONAR_HOST_URL and SONAR_TOKEN but I’m not sure if that does anything. I set SONAR_HOST_URL to localhost:9000 too… Any guidance or help is greatly appreciated!! I just want to have it so there is a sonarqube check during my CI/CD pipeline in GitLab…

Hi @ferily7,

when you are configuring the sonar.host.url to something like localhost and working with containers this will resolve to localhost in the scanner-cli container and not your sonarqube host. try to configure a public ip of the FQDN (Fully qualified domain name) of your sonarqube instance.

hope that helps

1 Like

Hi @Tobias_Trabelsi , thanks for the reply! How would I get the public IP of my sonarqube instance? The only sonarqube instance I spin up is locally but when running a GitLab pipeline, how would it know what the IP is?

Let me try to rephrase your situation just to make sure i understood your setup correctly.

you have a running sonarqube instance locally on your system and want to analyze something with gitlabCI on gitlab.com or your own instance of gitlab, which is running somewhere else. it this correct?

if yes then this is only possible if you expose your sonarqube instance to the outside world in some way and definitely not recommended for a production scenario. if you are currently working on a prove of concept, you could use some tool like ngrok to expose your local sonarqube instance to the outside world and use the generated URL as the sonar.host.url property.

1 Like

Ah, that makes sense! ngrok is the only way to expose my local sonarqube instance? Is there any way I could deploy the sonarqube instance to the cloud using Docker, Gitlab CI/CD and something like Kubernetes? Basically creating a CI/CD pipeline for the sonarqube instance, that way I can use the URL that the instance is deployed on.

We offer a helm chart that you might be interested in. with that you could deploy SQ in your k8s cluster and use the ingress as the host url for your scanner run in gitlab.

i am rather sure that there are other ways as well and SQ is not picky which one to choose. ngrok was just an idea to expose your local SQ instance, but if you want to move this installation somewhere else that is even better

A post was split to a new topic: Access Sonarqube in GitlabCI

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.