2
votes

I had configured sonarqube server in jenkins. In sonarqube I had a different quality gates, each quality gates has different conditions. I had a multiple jenkins pipeline job. How to select the sonarqube quality gate from jenkins pipeline.

For a separate job I can select the particular quality gate from jenkins pipeline code to check and analyse my code.

Instead of selecting the quality gate from sonarqube, how can I select my sonarqube quality gate from jenkins pipeline code.

Any Help

Thanks

1

1 Answers

1
votes

SonarQube provides a REST api that can be used to extract information from the SonarQube project, but you can also use it to modify properties in the SonarQube project, like the quality gate (or quality profile).

In the process I designed, there is typically a "base" SonarQube project that has a quality gate and quality profile I want to set in "derived" projects (we're not using new-style branches yet). So, I first read the properties from the base project and then set them into the derived project.

Code like this is used to get the properties from the base project:

def sonarQubeProjectName = sonarProps['sonar.motsid'] + ':' + SONAR_PROJECT_NAME
def authString = "${sonarProps['sonar.login']}:${sonarProps['sonar.password']}"

def qualityProfileResult =
    sh(returnStdout: true,
       script: "curl -s -X GET -u ${authString} \'${sonarProps['sonar.host.url']}/api/qualityprofiles/search?project=${sonarQubeProjectName}&language=java\'")
echo "qualityProfileResult[${qualityProfileResult}]"
def qualityProfileName = new JsonSlurper().parseText(qualityProfileResult).profiles[0].name
echo "qualityProfileName[${qualityProfileName}]"

def qualityGateResult   =
    sh(returnStdout: true,
       script: "curl -s -X GET -u ${authString} \'${sonarProps['sonar.host.url']}/api/qualitygates/get_by_project?project=${sonarQubeProjectName}\'")
echo "qualityGateResult[${qualityGateResult}]"
def qualityGateId   = new JsonSlurper().parseText(qualityGateResult).qualityGate.id
echo "qualityGateId[${qualityGateId}]"

And then to set them into the derived project:

// Now associate the project with the quality gate.
def selectQualityGateResult =
    sh(returnStdout: true,
       script: "curl -s -X POST -u ${authString} \'${sonarProps['sonar.host.url']}/api/qualitygates/select?gateId=${qualityGateId}&projectKey=${projectAndBranch}\'")
echo "selectQualityGateResult[${selectQualityGateResult}]"

// Now associate the project with the quality profile.
def selectQualityProfileResult =
    sh(returnStdout: true,
       script: "curl -s -X POST -u ${authString} \'${sonarProps['sonar.host.url']}/api/qualityprofiles/add_project?language=java&qualityProfile=${qualityProfileName}&project=${projectAndBranch}\'")
echo "selectQualityProfileResult[${selectQualityProfileResult}]"