首页 > 解决方案 > Sonarqube 与 Gitlab CI 集成 - 无法访问 SonarQube 服务器

问题描述

我一直在寻找这个问题的答案,但我找不到适合我的解决方案。

我正在尝试将 sonarqube 与 Gitlab CI 一起使用。我对这件事真的很陌生。

到目前为止,我可以安装 sonarqube 的 docker 映像并在本地成功运行它。但是,当我尝试使其与 gitlab CI 一起工作时,我得到“无法访问 SonarQube 服务器 [192.168.10.5:9000]”。192.168.10.5 是我的 IP,9000 是我正在使用的端口。

我在一个 java 8 - maven 项目中工作。

这是我在 pom.xml 中用于 sonarqube 的插件:

...
<plugin>
   <groupId>org.sonarsource.scanner.maven</groupId>
   <artifactId>sonar-maven-plugin</artifactId>
   <version>3.4.0.905</version>
</plugin>
...

在我的 .m2/settings.xml 我有这个配置文件:

<profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <sonar.host.url>http://192.168.10.5:9000</sonar.host.url>
    </properties>
</profile>

这是我的 gitlab-ci.yml:

image: maven:3.6.3-jdk-8

variables:
   SONAR_TOKEN: "myUserToken"
   SONAR_HOST_URL: "192.168.10.5:9000"
   GIT_DEPTH: 0

cache:
  paths:
    - .m2/repository

stages:
  - quality_test
  - build

quality_test:
  stage: quality_test
  script:
      - mvn pmd:check 
      - mvn verify sonar:sonar -Dsonar.qualitygate.wait=true -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_TOKEN -DskipTests
  allow_failure: true

build:
  stage: build
  script:
      - mvn package -DskipTests
  artifacts:
    paths:
      - target/com.jar

当我推送到 gitlab 时,管道“通过”并带有“警告”:

[INFO] User cache: /root/.sonar/cache
[ERROR] SonarQube server [192.168.10.5:9000] can not be reached
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.785 s
[INFO] Finished at: 2020-06-16T20:16:23Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.4.0.905:sonar (default-cli) on project project: Unable to execute SonarQube: Fail to get bootstrap index from server: unexpected url: 192.168.10.5:9000/batch/index -> [Help 1]

我知道它无法访问我用“192.168.10.5:9000”指定的服务器,其中 192.168.10.5 是我的 IP。我尝试使用 localhost:9000 得到相同的结果。

gitlab ci 运行器未在本地执行。

我真的坚持这一点。任何帮助将非常感激。

提前道歉,这是我的第一篇文章,英语不是我的母语。

标签: javamavensonarqubegitlabgitlab-ci

解决方案


推荐阅读