Skip to content

Instantly share code, notes, and snippets.

@kumbasar
Created December 4, 2018 12:15
Show Gist options
  • Select an option

  • Save kumbasar/6892c8a3d35cd2e42355550fc832f3de to your computer and use it in GitHub Desktop.

Select an option

Save kumbasar/6892c8a3d35cd2e42355550fc832f3de to your computer and use it in GitHub Desktop.
Set SonarQube Quality Gate Status to Jenkins Build Result
def scannerHome = tool 'SonarQube Scanner 3';
withSonarQubeEnv(config.sonarqube_id) {
sh "${scannerHome}/bin/sonar-scanner"
}
def props = readProperties file: '.scannerwork/report-task.txt'
echo "properties=${props}"
def sonarServerUrl=props['serverUrl']
def ceTaskUrl= props['ceTaskUrl']
def projectKey = props['projectKey']
def ceTask
timeout(time: 1, unit: 'MINUTES') {
waitUntil {
def response = httpRequest url: ceTaskUrl, authentication: 'jenkins_scanner'
ceTask = readJSON text: response.content
echo ceTask.toString()
return "SUCCESS".equals(ceTask["task"]["status"])
}
}
qgateRequest = sonarServerUrl + '/api/measures/component?componentKey=' + projectKey + '&metricKeys=alert_status'
response = httpRequest url: qgateRequest, authentication: 'jenkins_scanner'
qgate = readJSON text: response.content
echo qgate.toString()
if ("ERROR".equals(qgate["component"]["measures"][0]["value"].toString()) && (currentBuild.result.equals("SUCCESS"))) {
currentBuild.result = "UNSTABLE"
echo "Quality Gates Status: " + qgate["component"]["measures"][0]["value"].toString()
echo "Set build status to " + currentBuild.result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment