jenkins + sonar + github整合

时间:2015-08-17 10:01:32

标签: jenkins continuous-integration sonarqube static-analysis

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

我的配置:

  1. 使用github安装Sonar

  2. 已安装jenkins

  3. 在jenkins post-build动作中,我有以下属性

  4. -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
    
    1. 令牌是从我的github帐户生成的。
    2. 在这个link中我已经读过,我必须在运行sonarqube时提供以下属性: enter image description here

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

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

      好消息是,现在使用硬编码拉取请求没有正常工作。但我需要动态的方式来获取拉请求。

      是否有人知道如何获取拉请求。 jenkins内部动态。

      我发现拉请求构建器可以工作,但我的运气没有运气。我无法在声纳属性中使用拉请求构建器环境变量。

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

3 个答案:

答案 0 :(得分:26)

最后,我能够解决这个问题。这是详细信息:

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

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

      1. Global Jenkins系统设置:
    • 转到管理Jenkins - >配置系统
    • 配置jdk enter image description here
    • 安装sonarQube Runner enter image description here
    • 配置声纳 enter image description here
    • Git WebHook设置 enter image description here
    • PullRequest Builder设置 enter image description here

      1. 创建自由风格Jenkins工作 enter image description here
    • 将github网址添加到GitHub项目部分 enter image description here

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

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

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

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

    -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 builder插件触发作业,否则$ {ghprbPullId}将返回空白。如果您手动运行该作业,则无法将此$ {ghprbPullId}属性作为构建参数传递。如果您想查看可用的环境参数,请按照此Git environment variables for use in executing Jenkins shell scripts

    进行操作

    我希望这会有所帮助。

答案 1 :(得分:2)

评论太长了,所以我必须在这里写一下。

在对网络进行一些研究之后,我在尝试设置类似系统{... 3}}的过程中,向前几步发现了一个关于SO的问题。他们试图让这一切都与Maven一起工作,我不确定你是否也是,但无论哪种方式,你都可以看到他们一直用来帮助他们编写脚本的链接: / p>

here

这表明他们需要编写一个脚本,为他们动态检索拉动数。我想您可能需要遵循类似的方法并编写一个脚本来为您检索数字,我相信其中一个github配置文件会跟踪当前的请求编号以用于自己的跟踪目的。

并不像您希望的那样直截了当,但希望这是帮助您实现目标的新信息。

答案 2 :(得分:0)

查看我的项目实现 Jenkins + SONAR + Github,代码位于 https://github.com/lqtruong/ci