Sonarqube Gitlab 与 sonar-scanner.properties 文件的集成问题

Sonarqube Gitlab integration issue with sonar-scanner.properties file

我在 GitLab 中有两个项目,我正在尝试将 SonarQube 与我的 GitLab 项目集成。

项目 1

我已经将 'sonar-scanner.properties' 文件添加到 Project1 中,如下所示:

声纳-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

然后我在 gitlab-ci.yml 文件中添加了声纳扫描仪作业:

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    

在此之后,我配置了两个变量:'SONAR_HOST_URL' 和 'SONAR_TOKEN',然后是 运行 管道。它在项目 1 中运行得非常好。

项目 2

然后,我也需要为项目 2 做同样的事情。我需要声纳扫描仪进入项目 2,进行扫描和分析。为此,我使用新令牌在 SonarQube 中创建了另一个项目。

我需要进行配置,以便在触发项目 1 的管道时扫描项目 1 和项目 2。

为此,我在 Project1 的管道中添加了另一项工作。 如下:

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

我尝试设置下游管道以触发项目 2 中的 yaml 文件。因此,当项目 1 中的管道被触发并且作业 'sonar-scanner-test-repo' 被触发时,项目 2 中的另一个 yaml 文件是运行 作为下游管道。该 YAML 文件如下:

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

然后我在Project2中添加了'sonar-project.properties'文件如下:

声纳-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

问题是,当下游管道为 运行 时,我收到以下错误消息:

我发现下游管道没有找到项目 2 中的 'sonar-scanner.properties'。(第 68 和 74 行)

因为,在项目 1 上搜索此步骤时,它显示:

信息:项目根配置文件:/builds/rmesi/trojanwallg2-testing/sonar-project.properties

但在项目 2 中它不起作用。

有人知道如何解决这个问题吗?

我自己找到了解决方案。

需要添加

"- cd /build/rmesi/test-repo ; 声纳扫描仪"

在 'sonarscanner.gitlab-ci.yml' 文件作业的脚本部分。

这样,运行器会直接映射到所需的目录并在那里执行 'sonar-scanner' 命令。