SonarQube in GitLab CI pipeline: "SonarQube server ... can not be reached", but works locally

  • SonarQube Server Version: Community Edition 10.6
  • Sonar Scanner: sonar-maven-plugin:3.11.0.3922
  • Maven Version: Apache Maven 3.3.7
  • Java Version: 21
  • SonarQube deployment: Docker container running at https://sonarqube.COMPANY.com
  • CI Runner: GitLab Runner

What are you trying to achieve

I want to integrate SonarQube analysis into my GitLab CI pipeline using Maven. The goal is to have every MR and push to main, develop, or master branches automatically analyzed and results published to our internal SonarQube instance.

What have you tried so far to achieve this

I followed the instructions provided by SonarQube for Maven-based projects and did the following:

  • Created a project in SonarQube
  • Added the following CI variables in GitLab:
    • SONAR_TOKEN (secured)
    • SONAR_HOST_URL=https://sonarqube.COMPANY.com
  • Here’s my .gitlab-ci.yml setup:
include:
  project: '...'
  ref: main
  file: '/gitlab/base.yml'

test:
  extends: .test
  variables:
    NVD_API_KEY: $NVD_API_KEY
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
    GIT_DEPTH: "0"  # Tells git to fetch all branches (needed by the scanner)
  script:
    - mvn verify sonar:sonar
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_BRANCH == 'master'
    - if: $CI_COMMIT_BRANCH == 'main'
    - if: $CI_COMMIT_BRANCH == 'develop'

The problem

When the pipeline runs in GitLab CI, I receive the following error:

[ERROR] SonarQube server [https://sonarqube.COMPANY.com] can not be reached
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.11.0.3922:sonar (default-cli) on project MY_PROJECT: 
Unable to execute SonarScanner analysis: 
Fail to get bootstrap index from server: Connect timed out

However, when I run this command locally, it works perfectly:

mvn verify sonar:sonar \
  -Dsonar.host.url=https://sonarqube.COMPANY.com \
  -Dsonar.token=$SONAR_TOKEN

The results show up in the SonarQube UI.

Question

Why does the analysis work locally but fail in GitLab CI with a timeout when trying to connect to the SonarQube server?
Could this be related to networking/firewall/DNS from within the GitLab runner?
What should I check to debug this further?

Hi,

Welcome to the community!

Only the latest version of SonarQube Community Build is considered active, so you’ll need to update and see if the situation is still replicable before we can help you.

Your update path is:

10.6 → 24.12 → 25.6

You may find these resources helpful:

If you have questions about upgrading, feel free to open a new thread for that here.

Regarding your question, assuming you’ve correctly configured your sonar.host.url, this sounds like a question for your network folks.

 
HTH,
Ann