jenkins + Sonar + github 集成

标签 jenkins continuous-integration sonarqube static-analysis

问题:我正在设置jenkins + sonar + github集成,以便通过sonar自动检查pullrequest静态代码。

我的配置:

  1. 通过 github 安装 Sonar

  2. 已安装 Jenkins

  3. 在 Jenkins 构建后操作中,我具有以下属性

-Dsonar.github.login=bhuwang   
-Dsonar.github.repository=company/repo  
-Dsonar.verbose=true 
-Dsonar.analysis.mode=preview 
-Dsonar.issuesReport.console.enable=true 
-Dsonar.forceUpdate=true 
-Dsonar.github.login=gitusername 
-Dsonar.github.oauth=token
  • token 是从我的 github 帐户生成的。
  • 在此link我读到,运行 sonarqube 时必须提供以下属性: enter image description here

    我添加了除 sonar.github.pullRequest 属性之外的所有属性。 我不知道如何动态获取这个属性值。似乎以上四个属性必须正常工作。

    编辑:我找到了添加属性的方法 -Dsonar.github.pullRequest=pullrequestNo

    好消息是它现在工作得很好,硬编码的拉取请求号。但我需要动态方式来获取拉取请求编号。

    有谁知道如何获取拉取请求编号。在 Jenkins 内部动态。?

    我发现拉取请求构建器可以工作,但我最终没有运气。我无法在 Sonar 属性内使用拉取请求构建器环境变量。

    https://issues.jenkins-ci.org/browse/JENKINS-24590

    最佳答案

    终于可以解决这个问题了。详细内容如下:

    1. 使用 GitHub 插件安装 Sonar。
    2. 使用以下插件安装 Jenkins

      • GitHub 拉取请求生成器
      • SonarQube 插件
      • GIT 插件
      • GitHub 插件

    点击此链接设置拉取请求构建器插件:https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin#GitHubpullrequestbuilderplugin-EnvironmentVariables

    • 全局 Jenkins 系统设置:
    • 转到管理 Jenkins -> 配置系统
    • 配置jdk enter image description here
    • 安装 sonarQube Runner enter image description here
    • 配置 Sonar enter image description here
    • Git WebHook 设置 enter image description here
    • PullRequest 构建器设置 enter image description here

    • 创建自由风格的 Jenkins 作业 enter image description here
    • 将 github url 添加到 GitHub 项目部分 enter image description here

    • 设置源代码管理部分 enter image description here

    • 设置构建触发器 enter image description here

    • 为构建后操作设置 Sonar enter image description here

    • 在附加属性部分中提供以下 Sonar 属性

    -Dsonar.sourceEncoding=UTF-8 
    -Dsonar.analysis.mode=preview 
    -Dsonar.github.repository=company/repo
    -Dsonar.github.login=gitusername 
    -Dsonar.github.oauth=oauthtoken
    -Dsonar.host.url=sonarhostedurl
    -Dsonar.login=admin 
    -Dsonar.password=pass
    -Dsonar.github.pullRequest=${ghprbPullId}
    ${ghprbPullId}: this will be provided via github pullrequest builder plugin
    

    注意: 该作业应通过 pullrequest 构建器插件触发,否则 ${ghprbPullId} 将返回空白。如果您手动运行作业,这将不起作用,您必须将此 ${ghprbPullId} 属性作为构建参数传递。如果您想检查可用的环境参数,请按照此 Git environment variables for use in executing Jenkins shell scripts 操作。

    我希望这有帮助。

    关于jenkins + Sonar + github 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32047585/

    相关文章:

    jenkins - 具有相关参数的参数化 Jenkins 作业

    ruby - 重新运行后如何报告 cucumber 测试?

    linux - Sonarqube在Centos7上加载css和js相关内容失败

    java - 自定义插件无法从 jar 复制文件,因为协议(protocol)是 "jar"

    mysql - 连接到 MySQL 时 SonarQube 未启动

    jenkins - 一起使用 withEnv 和 withCredentials

    unit-testing - 如何仅在特定错误退出值(0 除外)上将 Jenkins 构建标记为成功?

    node.js - 如何从 bitbucket-pipelines.yml 进行 git push?

    node.js - Visual Studio Team Services Azure 部署(无法对指定目录执行操作 ("Delete File")

    continuous-integration - 如何从 FinalBuilder 输出将由 TeamCity 捕获的消息?