Skip to end of metadata
Go to start of metadata

Organizations that want to see their Quality Gates results reflected in the success/failure of their build jobs can use the Quality Gates Web Service to break the build when the project fails the gate. In previous versions of the SonarQube Server, this functionality was available via a plugin.

The import of your raw results (issues, low-level measures) and the computation of aggregated measures is done on the server side in a "Background Task". So once the scanning of your code is finished on the CI server, the analysis step ends and returns control to the job. At this stage, the aggregate measures needed for Quality Gate compliance aren't yet computed; you have to wait for the SonarQube Server to digest the scan results before proceeding.

To break the CI build for a failed Quality Gate, use WS API calls in a loop to wait until processing is finished, and then check Quality Gate web service:

  1. call the analysis's ceTaskUrl and examine the "status" value:
    1. PENDING or IN_PROGRESS - check again later
    2. FAILED or CANCELED - break the build?
    3. SUCCESS - move forward
  2. call Quality Gate web service for status

You can get the ceTaskURL from <work_dir>/report-task.txt, which is generated at the end of the analysis.

Sample report-task.txt
projectKey=test:fake-project-for-tests
serverUrl=http://localhost:9000
dashboardUrl=http://localhost:9000/dashboard/index/test:fake-project-for-tests
ceTaskId=AVF8MXobVNmO-QF545eY
ceTaskUrl=http://localhost:9000/api/ce/task?id=AVF8MXobVNmO-QF545eY

Workflow of WS API Calls

  1. Search in <work_dir>/report-task.txt the values of the CE Task URL (ceTaskUrl) and CE Task Id (ceTaskId)
  2. Call /api/ce/task?id=XXX where XXX is the CE Task Id retrieved from <work_dir>/report-task.txt (save it as analysisId somewhere)
  3. Do the Step 2 until the status is SUCCESS, CANCELED or FAILED
  4. If the status is FAILED => break the build
  5. If the status is SUCCESS
    1. Use the analysisId from the JSON returned by /api/ce/task?id=XXX
    2. Immediately call /api/qualitygates/project_status?analysisId=YYY to check the status of the quality gate

 

  • No labels