SonarQube版本5.2构建断路器插件替代方案

时间:2016-01-14 06:33:23

标签: jenkins build sonarqube

建立断路器支持似乎已经停止了5.2起。如果不满足质量指标,是否有任何其他Jenkins插件可以帮助我实现构建失败。

3 个答案:

答案 0 :(得分:1)

如果升级到SonarQube 5.3,则可以使用此新的Quality Web Service API: http://docs.sonarqube.org/display/SONAR/Breaking+the+CI+Build

如果你的质量门失败,

打破你的构建。

答案 1 :(得分:1)

我使用Groovy Postbuild Plugin来获取SonarQube 5.3中的功能。

如果出现任何问题或质量检查失败,以下代码会破坏构建:

import groovy.json.JsonSlurper

// shortcut to make writing to the log easier
def log = manager.listener.logger.&println
// this gets the filepath to the workspace
// note that FilePath can also reference remote files
def workspace = manager.build.getWorkspace()
def reportPath = workspace.withSuffix("/.sonar/report-task.txt")
log("Reading sonar taskId from file: '${reportPath}'")

// if anything goes wrong the build still needs to fail, therefore failure in the catch block
try
{
    if (!reportPath.exists())
    {
        log("[QualityGate] File '${reportPath}' does not exist. Without URL to check the task status the quality gate can not be checked.")
        manager.buildFailure()
    }
    else
    {
        // retrieve the taskUrl to check the task status
        def report = reportPath.read()
        def properties = new Properties()
        properties.load(report)
        def taskUrl = new URL(properties.ceTaskUrl)

        // used for parsing the response from the server
        def jsonParser = new JsonSlurper()

        def status = "PENDING"
        String analysisId = null
        while (status == "PENDING" || status == "IN_PROGRESS")
        {
            // loop until the task on the server is finished
            log("[QualityGate] Requesting task status from URL: " + properties.ceTaskUrl)
            def response = jsonParser.parse(taskUrl.newReader())
            status = response.task.status
            analysisId = response.task.analysisId
            sleep(1000)
        }

        if (status == "FAILED" || status == "CANCELED")
        {
            log("[QualityGate] Task failed or was canceled.")
            manager.buildFailure()
        }
        else if (status == "SUCCESS")
        {
            // once the task is finished on the server we can check the result
            def analysisUrl = new URL("${properties.serverUrl}/api/qualitygates/project_status?analysisId=${analysisId}")
            log("[QualityGate] Task finished, checking the result at ${analysisUrl}")
            def result = jsonParser.parse(analysisUrl.newReader())
            if (result.projectStatus.status == "OK")
            {
                log("[QualityGate] Analysis passed the quality gate.")
                manager.buildSuccess()
            }
            else if (result.projectStatus.status == "ERROR")
            {
                log("[QualityGate] Analysis did not pass the quality gate.")
                manager.buildFailure()
            }
            else
            {
                log("[QualityGate] Unknown quality gate status: '${result.projectStatus.status}'")
                manager.buildFailure()
            }
        }
        else
        {
            log("[QualityGate] Unknown task status '${status}. Aborting.")
            manager.buildFailure()
        }
    }
}
catch(Exception e)
{
    log("[QualityGate] Unexpected Exception: " + e.getMessage())
    manager.buildFailure()
}

答案 2 :(得分:0)

虽然我使用5.1,但逻辑应该相对类似。首先,我使用"资源" API调用I.e。,< ?yourserver:9000 / API /资源资源=&的projectname放大器;度量= quality_gate_details>拉出给定项目的质量门测量(度量,通过/失败,得分等)。我使用' jq'在bash脚本中导入json数据,并(非常容易地)计算出哪些指标通过/失败,它们的分数等等。

似乎您的主要目标是手动失败构建。这可以使用jenkins-cli.jar完成。该命令是" java -jar jenkins-cli.jar set-build-result pass / fail / unstable"

请注意,必须在构建时手动设置此方式的构建状态。在确定是否应将其设置为失败或不稳定之后,应在构建步骤结束时使用它。

我希望有所帮助。祝你好运。