sonar.Qualitygate is Deprecated in Sonar Qube 5.3. What is the alternative?

后端 未结 3 768
日久生厌
日久生厌 2021-01-19 04:09

Context: In Sonar Qube, there exists a custom Quality Gate which is called say abcd. This is NOT the default quality gate. And in Jenkins, I had configured this

相关标签:
3条回答
  • 2021-01-19 04:43

    You can still dynamically create an association gate - project via Sonarqube Web API.

    From your Sonarqube instance, go to its /web_api URL (e.g. http://my-sonarqube/web_api) and check the list of available operations.

    The web_api/api/qualitygates is the set of operations related to quality gates. The web_api/api/qualitygates/select is the operation you need to associate a gate to a project.

    Hence, as replacement of the deprecated sonar.qualitygate, you can use either the manual association via the web UI or a dynamic (and automated) association via web API, recommended. The latter case is the way to go in case of Continous Integration jobs (as you mentioned Jenkins in this context) that would need to create dynamically the link (especially useful for branches management).

    As a mandatory step for this operation to work, you need to pass some permissions, e.g. an user token, as recommended approach from official Sonarqube documentation on Web API.

    An example of what a CI step may look like just before invoking the sonar:sonar step:

    curl -u ${sonar.password.token}: ${sonar.setqualitygate.url} \
    -d "gateId=${sonar.gate.id}&projectKey=${sonar.project.key}:${planRepository.branch}"
    

    Where:

    • sonar.password.token is a token you need to generate from the Sonarqube User management page, for a technical user (e.g. a Jenkins user used to make the connection between the component)
    • sonar.setqualitygate.url the URL of the REST API endpoint (e.g. http://your.sonarqube.domain/api/qualitygates/select)
    • sonar.gate.id is the gate id, you can find it easily on the URL of the concerned gate (e.g. http://your.sonarqube.domain/quality_gates/show/<id>)
    • sonar.project.key and planRepository.branch here we are building dynamically the name of the project for a certain branch as well, you can skip this step if you don't want to handle branches dynamically (e.g. easy to do in Bamboo, a bit more tricky in Jenkins)
    0 讨论(0)
  • 2021-01-19 04:44

    It's indeed no more possible to set the Quality Gate of a project using a parameter when running an analysis. It's only possible from the UI/WS, where you can specify which Quality Gate should be used for which project.

    See the documentation for more information : http://docs.sonarqube.org/display/SONAR/Quality+Gates.

    0 讨论(0)
  • 2021-01-19 04:56

    Using approach, suggested by A_Di-Matteo , I faced an issue: when trying to select a gate for brand new feature branch, Sonar throw an error saying that project does not exist. So one can assign a gate only after project has been created. In this case, i use a hack: manually create a project using Sonar Web API just before assigning a gate, and only then performing mvn sonar:sonar step. Here is creation of dummy new project:

    def createNewProject(def config, def branch) {
        String projectName = new XmlSlurper().parseText(readFile('pom.xml')).name as String
        def url = "${config.sonarHost}/api/projects/create"
        sh "curl -u ${config.sonarToken}: ${url} -d 'name=${projectName}&project=${projectKey()}&branch=${branch}'"
    }
    

    Next step is assigning a Gate for this dummy project:

    def setSonarQualityGate(def config, def projectFullName, def gateId) {
        def url = "${config.sonarHost}/api/qualitygates/select"
        sh "curl -u ${config.sonarToken}: ${url} -d 'gateId=${gateId}&projectKey=${projectFullName}'"
    }
    

    And only after that I execute analysis itself:

    def runSonarAnalysis(def config, def branch) {
        echo "Run Sonar analysis"
        sh "mvn sonar:sonar -Dsonar.host.url=${config.sonarHost} -Dsonar.branch=${branch}"
    }
    
    0 讨论(0)
提交回复
热议问题