What's the best way to get my Sonarqube after invoking a scan via maven?

Which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
Community Edition - Version 7.7 (build 23042)

We are currently invoking Sonar via maven and then parsing the output log to grab the status url. We use that url to get the associated project key which we use to check the quality gate. Is there an easier, more straightforward way to do this?

mvn -s settings.xml --batch-mode org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar > /tmp/sonar.log

SONAR_STATUS_URL=$(grep -o "$SONAR_URL.*" /tmp/sonar.log | tail -1)
if [[ ! "${SONAR_STATUS_URL}" =~ "${SONAR_URL}/api" ]] ; then 
  echo "ERROR: Can't get project URL" 
  echo "Exiting..."
  exit 1
fi

# Get project key
PROJECT_KEY=$(curl -s $SONAR_STATUS_URL | awk -F'"' '{print $18}')

sleep 5 

# Get analysis result
STATUS=$(curl -s $SONAR_URL/api/qualitygates/project_status?projectKey=$PROJECT_KEY | awk -F'"' '{print $6}')
if [ "$STATUS" = "OK" ] ; then
  echo "Quality Gate: $STATUS"
  echo "Results: $SONAR_URL/dashboard/index/$PROJECT_KEY"
else
  echo "Quality Gate: $STATUS"
  echo "Results: $SONAR_URL/dashboard/index/$PROJECT_KEY"
  exit $ENFORCE
fi

The most easy way is to run your mvn builds with Jenkins pipeline.
Sonarqube Jenkins plugin will consume the JSON payload from Sonarqube webhook,
after the analysis report has been computed by Sonarqube server.
So no need to pull manually for quality gate status.

1 Like