-
which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
Using version : Community Edition Version 9.9 (build 65466) -
how is SonarQube deployed: zip, Docker, Helm
It is a centralized deployment in our company so don’t know -
what are you trying to achieve:
We are using Jenkins and a Git event trigger on Jenkins for SonarQube
And that works fine but right now in our script in Jenkins we are just waiting for the report to be finished.
We have put in a 3 minute sleep and that works but would be nice to be able to poll the SONAR API to instead know when it is ready? Maybe with a check every 10 seconds but I can’t find any API request that is suitable?
This is how the script looks like right now:
#!/bin/bash -e
# # Load required software modules (see ~/.modules file for account)
#module add jq/1.6 # <== MAY NOT EXIST ON DESIRED MACHINES
#module add git/2.9.3 # <== VERSION IS TOO OLD
BASH_MODULE_LOCATION="/app/modules/0/init/bash"
MAVEN_MODULE="maven/3.6.0"
MAVEN_PHASE="clean compile"
# Project key for this project.
PROJECT_KEY="nwstjcat"
# Page size is set to maximum during get data from Sonarqube server
PAGE_SIZE=500
# Set of filter rule to filter sonar report got from Sonarqube DB
SONAR_REPORT_BUILDER="ci/sonar-report-builder.jq"
# Output file after filter to pass Sonar Gerrit plugin
JSON_OUTPUT_FILE="target/sonar/sonar-report.json"
# Define Sonarqube server
SONARQUBE_URL="https://sonarqube.lmera.ericsson.se"
# Define Sonarqube API URL
SONARQUBE_API_URL="${SONARQUBE_URL}/api"
# Credential to access Sonarqube
SONARQUBE_CREDENTIAL="25afb75422b4ddf7827692669da83bcf70207c7e"
# Sonar plugin
SONAR_MAVEN_PLUGIN="org.sonarsource.scanner.maven"
# Sonar maven plugin version
SONAR_MAVEN_PLUGIN_VERSION="3.9.0.2155"
# Sonar maven plugin goal
SONAR_MAVEN_PLUGIN_GOAL="sonar"
# Sonar maven plugin id
SONAR_MAVEN_PLUGIN_ID="sonar-maven-plugin"
source ${BASH_MODULE_LOCATION}
module add ${MAVEN_MODULE}
# Trigger a sonar scanner to check the code
mvn -version; mvn ${MAVEN_PHASE} ${SONAR_MAVEN_PLUGIN}:${SONAR_MAVEN_PLUGIN_ID}:${SONAR_MAVEN_PLUGIN_VERSION}:${SONAR_MAVEN_PLUGIN_GOAL} \
-Dsonar.host.url=${SONARQUBE_URL} \
-Dsonar.login=${SONARQUBE_CREDENTIAL} \
-Dsonar.analysis.mode=publish \
-Dsonar.projectKey=${PROJECT_KEY}
# Sleep a certain time to wait for sonar report is ready
# The time was chosen by a 'hacked' number. Not to have any standard
# TODO:
# Choose a reasonable time instead of 'hacked' time OR
# Find a reasonable mechanism to wait
echo -e "\nSleeping 3 minute before get report to 'hope' it's ready.\n"
sleep 180
# Get the date of commit with specific format base on patchset revision
# E.g 2020-08-06T13:07:08+02:00
echo -e "\nGERRIT_PS_REVISION: ${GERRIT_PATCHSET_REVISION}\n"
commit_date=$(git show -s --format=%aI ${GERRIT_PATCHSET_REVISION})
# Reformat the datetime to fit with Sonarqube server API
# E.g 2020-08-06T13:07:08%2b0200
commit_date=$(sed -e "s/\(.*\)[+-]\(.*\):\(.*\)/\1%2b\2\3/" <<< ${commit_date})
# Parameters for search action
search_params="componentKeys=${PROJECT_KEY}&resolved=false&ps=${PAGE_SIZE}&createdAfter=${commit_date}"
echo -e "\nSEARCH_PARAM: ${search_params}\n"
# Create JSON report file and store into target/sonar directory
curl --netrc-file ~/.netrc-sonarqube "${SONARQUBE_API_URL}/issues/search?${search_params}" | jq -f ${SONAR_REPORT_BUILDER} > ${JSON_OUTPUT_FILE}
As I mentioned this works but would be better to have like a poll instead to the REST API of some sort instead of a the sleep for 3 minutes.
I have tried this this instead of the sleep but that was not really the full report that was finished.
# Poll SonarQube API to check analysis status
MAX_POLL_ATTEMPTS=30 # Set your desired number of polling attempts
POLL_INTERVAL_SECONDS=10 # Set your desired polling interval in seconds
poll_attempts=0
analysis_status="PENDING"
while [ $poll_attempts -lt $MAX_POLL_ATTEMPTS ] && [ "$analysis_status" != "SUCCESS" ]; do
sleep $POLL_INTERVAL_SECONDS
poll_attempts=$((poll_attempts + 1))
analysis_status=$(curl -s "${SONARQUBE_API_URL}/ce/component?component=${PROJECT_KEY}" | jq -r '.current.status')
done
if [ "$analysis_status" == "SUCCESS" ]; then
echo "SonarQube analysis is complete. Proceed with further actions."
else
echo "SonarQube analysis did not complete successfully after $MAX_POLL_ATTEMPTS attempts. Check SonarQube server logs for details."
exit 1
fi
Maybe we are doing this wrong
Sorry for long post