Integrating Sonarqube with Gitlab

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    SonarQube version: 9.9.1
  • How is SonarQube deployed: zip, Docker, Helm
  • what are you trying to achieve
    Integrating SonarQube with Self-hosted Gitlab
  • what have you tried so far to achieve this
    I have configured DevOps Platform Integration. I created a new user on Gitlab with reporter permission and gave permission API to the token. Then I configured the DevOps platform integration where I provided the configuration name, Gitlab API URL, and Personal Access token, and it’s connected to my Gitlab because I can see the repositories here in SonarQube.

After that, I added the new project in the Sonarqube and chose the integration with GitLab CI, set up the project key for PHP, added the in the repository with values given by sonarqube, and then, saved the sonarqube token environment variable in the GitLab by following the screen instruction on SonarQube page, and then added the configuration file in the .gitlab-ci.yml file. After finishing this, when I checked the logs in Gitlab, I am facing the below error.
bash: line 143: sonar-scanner: command not found
Does anyone know why it is happening?

Do not share screenshots of logs – share the text itself (bonus points for being well-formatted)!
Gitlab logs

Running with gitlab-runner 16.0.1 (79704081)

on dev-server xUfb7Nzu, system ID: s_2193954a9440

Preparing the “shell” executor

Using Shell (bash) executor…

Preparing environment

Running on dev-server…

Getting source from Git repository

Fetching changes…

Reinitialized existing Git repository in /home/gitlab-runner/builds/xUfb7Nzu/0/project-name/repo-name/.git/

Checking out a368c059 as detached HEAD (ref is dev)…

Skipping Git submodules setup

Restoring cache

Checking cache for sonarqube-check-non_protected…

Runtime platform arch=amd64 os=linux pid=2014 revision=79704081 version=16.0.1

No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.

Successfully extracted cache

Executing “step_script” stage of the job script

$ sonar-scanner

bash: line 143: sonar-scanner: command not found

Cleaning up project directory and file based variables

ERROR: Job failed: exit status 1

The error means sonar-scanner is not installed on your CI machine (the one that runs script from gitlab-ci.yml).

You’ll find various ways to install sonar-scanner here: SonarScanner. Depending on your Gitlab-CI setup, you’ll need either one-time setup (bash executor) or add proper docker image to your gitlab-ci.yml (docker executor, Run your CI/CD jobs in Docker containers | GitLab)

Thanks janosvitok After deep digging into the configuration, the sonarqube configuration in .gitlab-ci.yml was using the dev server gitlab-runner, so the command was running on the dev server, but now I have registered the new gitlab-runner to the sonarqube server and the error has been changed now, which seems a bit progress. Now, my sonar scanner is executing but it is unable to connect to the sonarqube container. the error is “SonarQube server cannot be reached”, I think it is because the sonarqube server is running on another container. Do you have any idea how can I link my sonar scanner with the sonarqube server?

sonarscanner was sending request from the public IP where the sonarqube was setup in container, and the self server IP was not whitelisted, so I whitelisted its IP at the firewall and it worked, thanks.