Running Sonar Scanner docker image in Gitlab

Hi, I am trying to run Sonar Scanner docker image in Gitlab runner as part of our pipeline
and get the error
ERROR: Error during SonarScanner execution
ERROR: You must define the following mandatory properties for ‘Unknown’: sonar.projectKey

My configuration:
In the root of the project I have 2 files

  1. sonar-project.properties
    sonar.projectKey=
    sonar.projectName=
    sonar.projectVersion=1.0

  2. sonar-scanner.properties which has these

sonar.host.url=""
sonar.login=""
sonar.project.settings=sonar-project.properties

In my gitlab-ci-yml file, my scan job is as follow
sonarqube-check:
stage: scan
variables:
SONAR_PROJECT_BASE_DIR: “${CI_PROJECT_DIR}”
allow_failure: true
script:
- pwd
- docker run --rm -e SONAR_HOST_URL="" -e SONAR_LOGIN="" -e SONAR_PROJECT_KEY="" -v “/home/gitlab-runner/builds/YzYRGXuG/0/rdit-common/sdcommon/backend/sdcommon-api:/usr/src” sonarsource/sonar-scanner-cli

I have tried docker run with and without the -e SONAR_PROJECT_KEY setting. Still same results.

Should the sonar-project.properties be available at the path specified in the mount -v option ?

Appreciate any help regarding this issue.

Hey there.

The SonarQube UI offers a tutorial for setting up analysis in a GitLab context.

I think you might be overcomplicating things when ultimately, this is all you need

  • a sonar-project.properties file that defines sonar.projectKey=foo
  • SONAR_TOKEN and SONAR_HOST_URL environment variables set
  • the following in your .gitlab-ci.yml
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
  allow_failure: true
  only:
    - merge_requests
    - master # or the name of your main branch
    - develop

Thanks for getting back. I am running gitlab runner with shell executor. I changed the
gitlab-ci.yml file to like what you have provided. However get the message

"sonar-scanner: command not found"

My gitlab job looks like below now
sonarqube-check:
stage: scan
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
variables:
SONAR_USER_HOME: “${CI_PROJECT_DIR}/.sonar”
GIT_DEPTH: “0”
SONAR_TOKEN:
SONAR_HOST_URL:
allow_failure: true
script:
- pwd
- sonar-scanner

Finally I reverted back to doing a docker run , added the project key to the docker run command and runs fine.

sonarqube-check:
stage: scan
allow_failure: true
script:
- pwd
- docker run --rm -e SONAR_HOST_URL= -e SONAR_LOGIN= -v sonarsource/sonar-scanner-cli -D sonar.projectKey=

1 Like

Hi Colin ,as mentionned by kavitharanga , this “official” configuration does not work, I also have “sonar-scanner: command not found”. Can you please check and provide a solution ?

Hi, I am also getting the same error- soanr-scanner command not found
can you please provide .yml file?

Hello,

I’m linking a thread that probably has the same root cause. The answer there can help people who try to troubleshoot this issue.