Ant Quality Gate


Is there a way to apply Quality Gate from within Ant scanner in a similar wat to Jenkins Pipeline?
From the docs it doesn’t seem so.



Your best bet is going to be to apply it in the CI that’s running your Ant build.

If you’re firing this manually instead of using a CI (why???) then it’s going to be complicated. It’s been a long time since I Ant-ed. In pseudo code you need to:

for (status = get status; status==pending; status=get status)
   sleep 30
evaluate status


I’m running this from a CI server. I user Jenkins, but cannot use Jenkins Pipeline for the project unfortunately.
After the scan has started with <sonar:sonar /> from an example here, how do I then query the server for the status of this specific request?


This is not for the faint of heart.

After the analysis is complete IIRC you need to parse one of the files in the analysis work directory to get the analysis id (poke around; you’ll find it). Then take a look at api/project_analyses/search. You’ll find a link to the on-board Web API docs in your page footer.


I couldn’t find analysis id in the files, I looked in .scannerwork directory. There was an HTTP link to the scan at the end of log output though. Would you point me to the file please?

You’re on the right track.
The file should be named report-task.txt and has ceTaskId and ceTaskUrl
Use ceTaskUrl in a loop until the json response has ce task status SUCCESS.
This means the background task computing the quality gate status has finished.
Then use web api api/qualitygates/project_status with ceTaskId and - if using branches - the right branch name to get the quality gate status.
You may use some Groovy snippet via ant groovy task for the rest calls and looping and wrap in
an ant macrodef for reuse.

Ok so with your help I figured this out.
.scannerwork/report-task.txt file is created after the scan is complete. There is ceTaskId in the file that can be used to obtain analysisId by a REST call to https://sonar-url/api/ce/task?id=$ceTaskId. After that query https://sonar-url/api/qualitygates/project_status?analysisId=$analysisId for the Quality Gate status.
It is very unfortunate that I have to do all of this manually, with a custom script. I was kind of expecting this to be a feature of Sonar Ant task.

Thank you all for help!


To be clear, it’s not a feature of any of the scanners (“SonarScanner for Jenkins” is a bit of a misnomer). Their job is to compile the analysis report and submit it to the server. The rest happens asynchronously and must be managed separately.


Even though it happens asynchronously, I see no reason why it cannot be embedded as a scanner option. Seems logical to me.

Maybe I’m not using it the way it was intended.
My use case is that I need to scan a Pull Request, and based on Quality Gate metrics decide if it is allowed to be merged. For that I need to have QG results.


Its sounds like you’re looking for Developer Edition($). That functionality is built-in.


That is what our company is using. Could you point me to a guide on setting this up?
Again, I was able to configure it with Jenkins pipeline with waitForQualityGate(), but in this case I’m using a regular Jenkins job.


I’m not sure there’s good support in a regular Jenkins job. Is it possible two switch to a pipeline & fire your Ant job from it?


In this case the Build Breaker plugin might be an option, see

This is what solved my problem. I used that parameter in Jenkins freestyle job sonar.qualitygate.wait=true from docs

1 Like