首页 > 解决方案 > GitLab SonarQube CI/CD 变量未传递到管道

问题描述

我试图根据以下文档https://docs.sonarqube.org/latest/analysis/gitlab-cicd/将 GitLab CI/CD 与 SonarQube 8.1 集成。

我尝试将 SonarScanner 用于 Maven 示例配置

image: maven:latest
variables:
  SONAR_TOKEN: "your-sonarqube-token"
  SONAR_HOST_URL: "http://your-sonarqube-url"
  GIT_DEPTH: 0
sonarqube-check:
  script:
    - mvn verify sonar:sonar -Dsonar.qualitygate.wait=true
  allow_failure: true
  only:
    - merge_requests
    - master

问题是它看起来像 SONAR_HOST_URL,并且可能由于不清楚的原因忽略了 SONAR_TOKEN。查看管道日志时,我得到

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project sonar-java-test: Unable to execute SonarQube: Fail to get bootstrap index from server: Failed to connect to localhost/0:0:0:0:0:0:0:1:9000: Connection refused (Connection refused) -> [Help 1]

我试图通过使用 gitlab (12.3.2) CI/CD 变量设置变量来解决这个问题,但它不起作用

在此处输入图像描述

有任何想法吗?

标签: sonarqubegitlab

解决方案


文档似乎不是最新的。

您应该向 maven 命令添加-Dsonar.host.url和参数以覆盖默认设置:-Dsonar.host.url

image: maven:latest
variables:
  SONAR_TOKEN: "your-sonarqube-token"
  SONAR_HOST_URL: "http://your-sonarqube-url"
  GIT_DEPTH: 0
sonarqube-check:
  script:
    - mvn verify sonar:sonar -Dsonar.qualitygate.wait=true -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_TOKEN
  allow_failure: true
  only:
    - merge_requests
    - master

推荐阅读