Sonar scanner run from gitlab-ci does not see the code

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    sonarqube community edition
  • what are you trying to achieve
    each commit should be analyzed by sonnar scanner
  • what have you tried so far to achieve this
    I have configured gitlab ci/cd pipeline for this but sonnar scanner doesn’t see my code, when I run command directly on gitlab-runner everything works fine.

this is my job declaration

sonarqube-analyze:
    stage: SonarQube Analysis
    services:
      - docker:dind
    image: docker:latest
    before_script:
      - docker version
    script:
      - ls -la $(pwd)
      - docker run --rm  -e SONAR_SCANNER_OPTS="-Dsonar_sources=. -Dsonar.projectKey=${SONARQUBE_PROJECTNAME}" -e SONAR_HOST_URL="${SONARQUBE_URL}" -e SONAR_LOGIN="${SONARQUBE_TOKEN}"     -v "$(pwd):/usr/src"     sonarsource/sonar-scanner-cli

this is my gitlab output

$ ls -la $(pwd)
total 80
drwxrwxrwx    9 root     root          4096 Dec  7 13:37 .
drwxrwxrwx    4 root     root          4096 Nov 23 09:41 ..
--- the rest of my files which should be analyzed ---
$ docker run --rm  -e SONAR_SCANNER_OPTS="-Dsonar_sources=. -Dsonar.projectKey=${SONARQUBE_PROJECTNAME}" -e SONAR_HOST_URL="${SONARQUBE_URL}" -e SONAR_LOGIN="${SONARQUBE_TOKEN}"     -v "$(pwd):/usr/src"     sonarsource/sonar-scanner-cli
INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.7.0.2747
INFO: Java 11.0.17 Alpine (64-bit)
INFO: Linux 5.15.0-52-generic amd64
INFO: SONAR_SCANNER_OPTS=-Dsonar_sources=. -Dsonar.projectKey=project-name
INFO: User cache: /opt/sonar-scanner/.sonar/cache
INFO: Scanner configuration file: /opt/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: Analyzing on SonarQube server 8.9.10
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=195ms
INFO: Server id: BF41A1F2-AYRgjHoX5KCCP-iE0nbp
INFO: User cache: /opt/sonar-scanner/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=112ms
INFO: Load/download plugins (done) | time=3812ms
INFO: Process project properties
INFO: Process project properties (done) | time=1ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=3ms
INFO: Project key: project-name
INFO: Base dir: /usr/src
INFO: Working dir: /usr/src/.scannerwork
INFO: Load project settings for component key: '---project-name---'
INFO: Load project settings for component key: '---project-name---' (done) | time=21ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=113ms
INFO: Load active rules
INFO: Load active rules (done) | time=1441ms
WARN: SCM provider autodetection failed. Please use "sonar.scm.provider" to define SCM of your project, or disable the SCM Sensor in the project settings.
INFO: Indexing files...
INFO: Project configuration:
INFO: 0 files indexed
INFO: ------------- Run sensors on module drivenbrands
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=56ms
INFO: Sensor CSS Rules [cssfamily]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor CSS Rules [cssfamily] (done) | time=9ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=5ms
INFO: Sensor C# Project Type Information [csharp]
INFO: Sensor C# Project Type Information [csharp] (done) | time=1ms
INFO: Sensor C# Properties [csharp]
INFO: Sensor C# Properties [csharp] (done) | time=2ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=2ms
INFO: Sensor VB.NET Project Type Information [vbnet]
INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=2ms
INFO: Sensor VB.NET Properties [vbnet]
INFO: Sensor VB.NET Properties [vbnet] (done) | time=2ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=9ms
INFO: SCM Publisher No SCM system was detected. You can use the 'sonar.scm.provider' property to explicitly specify it.
INFO: CPD Executor Calculating CPD for 0 files
INFO: CPD Executor CPD calculation finished (done) | time=0ms
INFO: Analysis report generated in 237ms, dir size=93 KB
INFO: Analysis report compressed in 44ms, zip size=11 KB
INFO: Analysis report uploaded in 34ms
INFO: ANALYSIS SUCCESSFUL, you can browse https://sonarqube.my.url/dashboard?id=project_name
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at https://sonarqube.my.url/api/ce/task?id=AYTszhxP6WfOEXC8BzOK
INFO: Analysis total time: 5.832 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 15.078s
INFO: Final Memory: 7M/27M
INFO: ------------------------------------------------------------------------
Saving cache for successful job
00:11
Creating cache default-protected...
.cache/pip: found 454 matching files and directories 
venv/: found 5286 matching files and directories   
No URL provided, cache will be not uploaded to shared cache server. Cache will be stored only locally. 
Created cache
Cleaning up project directory and file based variables
00:02
Job succeeded

Hi,

Welcome to the community!

Is your project actually checked out into /usr/src?

 
Ann

Thank you for your respond.

I managed to solve this problem, the problem was that I am using DIND and the sonnar-scanner-cli container was not getting the source code.

First, I had to configure gitlab-runner to mount /builds inside its container, this change is done in /etc/gitlab-runner/config.toml. After that the /builds directory was mounted so that the source code was delivered to the docker machine itself, only after this step the mount command to /usr/src made sense and the sonar-scanner code starts to see.

1 Like

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