Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

Sonarqube Gitlab integration issue with sonar-scanner.properties file

I have a two projects in GitLab and I am trying to integrate SonarQube with my GitLab projects.

Project 1

I have added the 'sonar-scanner.properties' file to Project1 and it's as follows:

sonar-scanner.properties

# SonarQube server
# sonar.host.url & sonar.login are set by the Scanner CLI.
# See https://docs.sonarqube.org/latest/analysis/gitlab-cicd/.
# Project settings.
sonar.projectKey=Trojanwall
sonar.projectName=Trojanwall
sonar.projectDescription=My new interesting project.
sonar.links.ci=https://gitlab.com/rmesi/trojanwallg2-testing/-/pipelines
#sonar.links.issue=https://gitlab.com/rmesi/trojanwallg2-testing/

# Scan settings.
sonar.projectBaseDir=./
#sonar.sources=./
sonar.sources=./
sonar.sourceEncoding=UTF-8
sonar.host.url=http://sonarqube.southeastasia.cloudapp.azure.com:31000
sonar.login=4f4cbabd17914579beb605c3352349229b4fd57b

#sonar.exclusions=,**/coverage/**

# Fail CI pipeline if Sonar fails.
sonar.qualitygate.wait=true

Then I added the sonar scanner job in the gitlab-ci.yml file:

gitlab-ci.yml

sonar-scanner-trojanwall:
  stage: sonarqube:scan


  image:
    name: sonarsource/sonar-scanner-cli:4.5
    entrypoint: [""]
  variables:
    # Defines the location of the analysis task cache
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
    # Shallow cloning needs to be disabled.
    # See https://docs.sonarqube.org/latest/analysis/gitlab-cicd/.
    GIT_DEPTH: 0
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - sonar-scanner

  only:
    - Production
    - /^[d]+.[d]+.1$/  
  when: on_success    

After this, I configured the two variables: 'SONAR_HOST_URL' and 'SONAR_TOKEN' and then, ran the pipeline. It worked perfectly fine for the Project 1.

Project 2

Then, I needed to do the same for the Project 2 as well. I needed the sonar scanner to go into the Project 2, scan and analyze. For that, I created another project in SonarQube with a new token.

I needed to configure in such a way that when the pipeline for Project 1 is triggered, it scans both Project 1 and 2.

For that, I added another job in Project1's pipeline. It's as follows:

gitlab-ci.yml

sonar-scanner-test-repo:
  stage: sonarqube:scan
  trigger:
    include:
    - project: 'rmesi/test-repo'
      ref: master
      file: 'sonarscanner.gitlab-ci.yml'

  only:
    - Production
    - /^[d]+.[d]+.1$/  
  when: on_success

I tried to setup a downstream pipeline to trigger a yaml file in Project 2. So, when The pipeline in Project 1 is triggered and when the job 'sonar-scanner-test-repo' gets triggered, another yaml file in Project 2 is run as a down stream pipeline. That YAML file is as follows:

sonarscanner.gitlab-ci.yml

stages:
  - sonarqube:scan

variables: 
  CI_PROJECT_DIR: /builds/rmesi/test-repo


sonar-scanner:
  stage: sonarqube:scan
  image:
    name: sonarsource/sonar-scanner-cli:4.5
    entrypoint: [""]
  variables:
    # Defines the location of the analysis task cache
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
    # Shallow cloning needs to be disabled.
    # See https://docs.sonarqube.org/latest/analysis/gitlab-cicd/.
    GIT_DEPTH: 0
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  script:
    - cd /builds/rmesi/
    - git clone https://gitlab.com/rmesi/test-repo.git test-repo
    - sonar-scanner

Then I added the 'sonar-project.properties' file in Project2 which is as follows:

sonar-project.properties

# SonarQube server

# sonar.host.url & sonar.login are set by the Scanner CLI.
# See https://docs.sonarqube.org/latest/analysis/gitlab-cicd/.

# Project settings.
sonar.projectKey=test-repo
sonar.projectName=test-repo
sonar.projectDescription=My new interesting project.
sonar.links.ci=https://gitlab.com/rmesi/test-repo/-/pipelines
#sonar.links.issue=https://gitlab.com/rmesi/test-repo/

# Scan settings.
sonar.projectBaseDir=/builds/rmesi/test-repo/
sonar.sources=/builds/rmesi/test-repo/, ./
sonar.sourceEncoding=UTF-8
sonar.host.url=http://sonarqube.southeastasia.cloudapp.azure.com:31000
sonar.login=b0c40e44fd59155d27ee43ae375b9ad7bf39bbdb

#sonar.exclusions=,**/coverage/**

# Fail CI pipeline if Sonar fails.
sonar.qualitygate.wait=true

The issue is that, when the down stream pipeline is run, I am getting the following error message:

enter image description here

I figured out that the down stream pipeline is not locating the 'sonar-scanner.properties' in Project 2. (Lines 68 and 74)

enter image description here

Where as, on Project 1 while searching for this step, it shows:

INFO: Project root configuration file: /builds/rmesi/trojanwallg2-testing/sonar-project.properties

But in Project 2 it's not working.

Does anyone know how to fix this?

question from:https://stackoverflow.com/questions/65875806/sonarqube-gitlab-integration-issue-with-sonar-scanner-properties-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found the solution to this, myself.

Required to add

"- cd /build/rmesi/test-repo ; sonar-scanner"

in the script section in the job of the 'sonarscanner.gitlab-ci.yml' file.

That way, the runner maps directly to desired directory and execute the 'sonar-scanner' command there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...