【问题标题】:Adding Sonar to Maven Jenkins Job With Credentials Fails使用凭证将声纳添加到 Maven Jenkins 作业失败
【发布时间】:2019-10-14 20:10:33
【问题描述】:

我正在尝试让 SonarQube 插件在 Jenkins 上运行。我正在使用official guide

  1. 使用来自 Sonar 服务器的秘密文本在 Jenkins 上创建了全局凭据:管理 -> 配置 -> 加密
  2. 配置了 Jenkins 并添加了我们的 SonarQube 服务器和来自 1 的身份验证。
  3. 在作业配置中启用“准备 SonarQube 扫描仪环境”
  4. 在工作目标中添加了$SONAR_MAVEN_GOAL -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_AUTH_TOKEN

所有这些都会导致异常:

Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project com.qualitype.foundation.build: 
Not authorized. Please check the properties sonar.login and sonar.password. 

我试图解决这个问题的方法:

  • sonar.loginsonar.password 添加到pom.xml
  • sonar.loginsonar.password 添加到作业配置的目标中
  • 在作业配置的 MAVEN_OPTS 中添加了 sonar.loginsonar.password
  • 以上所有内容,用户令牌为sonar.login(来自Profile -> Security -> Tokens
  • 将凭证的秘密文本替换为上述令牌
  • 以上所有内容都没有变量(例如,不是$SONAR_AUTH_TOKEN,而是一个固定值)
  • 使用构建后步骤“使用 Maven 进行 SonarQube 分析”

这些工作都没有,都有相同的例外。有效的方法是调用 sonar:sonar 并禁用“准备 SonarQube 扫描仪环境”,因为我们的 pom.xml 是这样设置的。当我再次启用“准备 SonarQube 扫描仪环境”时,构建失败。

如果我使用官方插件而不是仅仅调用sonar:sonar,我曾希望在 Jenkins 作业中显示 Sonar 信息。

如何让 Sonar 插件与 Jenkins 一起工作?

【问题讨论】:

  • 请添加Manage Jenkins → Configure System → SonarQube servers的画面(别忘了点击Advanced...按钮)。
  • @agabrys 哪些信息是相关的?只填写了名称、服务器 URL 和服务器身份验证令牌,我不确定我是否应该在此处填写公司内部信息。

标签: jenkins sonarqube


【解决方案1】:

根据 /configureTools/ 下的 Maven 配置,您必须将以下内容添加到 /configfiles/ 下的 maven settings.xml:

<id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <sonar.host.url>URL to your Sonar-Server</sonar.host.url>
            <sonar.login>the token you generated on Sonaqrqube</sonar.login>             
        </properties>
    </profile>

您可以在 Sonar Plugin Settings 中指定的 Sonartoken 通常不会被 Maven 考虑在内(只要您不通过 -D 参数 script.sh "mvn sonar:sonar -Dsonar.branch=${script.env.getProperty('BRANCH_NAME')} -Dstyle.color=always -Dsonar.login=$token 指定它)。 总的来说,Sonarqube 使用令牌作为用户/密码替换的方式不适合我们的自动实例设置。所以我们最终使用 REST 调用在我们的自定义 Jenkins 管道库中为每个构建生成一个短暂的令牌。

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 2021-08-04
    • 2016-07-02
    • 2016-07-21
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    相关资源
    最近更新 更多