2

I want my Jenkins build to fail if the code doesn't have 90% test coverage. For that, I have installed the Quality Gates plugin, which should check the SonarQube analysis.

I have the following configuration in Jenkins, under Quality Gates:

Name: SonarQubeServer
SonarQube Server URL: http://my-server.com:9000
SonarQube account login: admin
SonarQube account password: ****

SonarQube displays: Quality Gate Failed

Jenkins displays: SonarQube analysis completed: SUCCESS and the build passes.

Any idea why Jenkins doesn't get that the quality gate failed?

bsky
  • 19,326
  • 49
  • 155
  • 270

4 Answers4

2

Eventually I realised that I should have added Quality Gates as a Post Build Action for every job I was using it on.

bsky
  • 19,326
  • 49
  • 155
  • 270
2

You can do that with the Shell commands: sharing this info if someone needs it

To mark build as failure when Quality gate is not passed using Sonar Rest api. Add “Execute Shell” after Sonar Step and use below code Tip : Introduce sleep time of 10s before this step , just to ensure that Sonar site is updated with task result status.

Fetching TASKURL from report-task.txt in workspace

url=$(cat $WORKSPACE/.sonar/report-task.txt | grep ceTaskUrl | cut -c11- )

Fetching Task attributes from Sonar Server

curl -u admin:${admin_pwd} -L $url | python -m json.tool

Setting up task status to check if sonar scan is completed successfully.

curl -u admin:${admin_pwd} -L $url -o task.json

status=$(python -m json.tool < task.json | grep -i "status" | cut -c20- | sed 's/.(.)$/\1/'| sed 's/.$//' )

echo ${status}

If SonarScan is completed successfully then set analysis ID & URLS.

if [ $status = SUCCESS ]; then

analysisID=$(python -m json.tool < task.json | grep -i "analysisId" | cut -c24- | sed 's/.(.)$/\1/'| sed 's/.$//')

analysisUrl="https://sonar.net/api/qualitygates/project_status?analysisId=${analysisID}

echo ${analysisID}

echo ${analysisUrl}

else

echo "Sonnar run was not sucess"

exit 1

fi

Fetching SonarGate details using analysis URL

curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool

curl -u admin:$admin_pwd ${analysisUrl} | python -m json.tool | grep -i "status" | cut -c28- | sed 's/.$//' >> tmp.txt

cat tmp.txt

sed -n '/ERROR/p' tmp.txt >> error.txt

cat error.txt

if [ $(cat error.txt | wc -l) -eq 0 ]; then

echo "Quality Gate Passed ! Setting up SonarQube Job Status to Success ! "

else

exit 1

echo "Quality Gate Failed ! Setting up SonarQube Job Status to Failure ! "

fi

Cleaning up the files

unset url

unset status

unset analysisID

unset analysisUrl

task.json

tmp.txt

error.txt

Community
  • 1
  • 1
Sri
  • 51
  • 5
1

In response to Sri who has some type/errors in his solution. This is sonar4.5.5 building using sonar-scanner

if [ -e tmp.txt ];
then
rm tmp.txt
rm error.txt
rm task.json
fi

sleep 5

cat $WORKSPACE/.scannerwork/report-task.txt

url=$(cat $WORKSPACE/.scannerwork/report-task.txt | grep ceTaskUrl | cut -c11- )
echo $url

curl -u admin:pswd -L $url | python -m json.tool
curl -u admin:pswd -L $url -o task.json
status=$(python -m json.tool < task.json | grep -i "status" | cut --delimiter=: --fields=2 | sed 's/"//g' | sed 's/,//g' )
echo ${status}

if [ $status = SUCCESS ]; then
analysisID=$(python -m json.tool < task.json | grep -i "analysisId" | cut -c24- | sed 's/"//g' | sed 's/,//g')
analysisUrl="http://sonarserver/sonarqube/api/qualitygates/project_status?analysisId=${analysisID}"
echo ${analysisID}
echo ${analysisUrl}

else
echo "Sonar run was not success"
exit 1

fi


curl -u admin:pswd ${analysisUrl} | python -m json.tool
curl -u admin:pswd ${analysisUrl} | python -m json.tool | grep -i "status" | cut -c28- | sed 's/.$//' >> tmp.txt
cat tmp.txt
sed -n '/ERROR/p' tmp.txt >> error.txt
cat error.txt
if [ $(cat error.txt | wc -l) -eq 0 ]; then
echo "Quality Gate Passed ! Setting up SonarQube Job Status to Success ! "
else
echo "Quality Gate Failed ! Setting up SonarQube Job Status to Failure ! "
exit 1
fi
Nanotron
  • 554
  • 5
  • 16
0

the plugin quality gates return just status :passed or failed , so you can build other job from jenkins from the result of those two flags . but if you want to make flag passed if the coverage resulat >90 % you have to configure it from sonarqube not jenkins . in this situation you can imagine this scenario :

test coverage <90 -> flag :failed . jenkins don't call other job .

test coverage >90 -> flag :passed. jenkins call other job .

i think this can help you somehow .

Yassine CHABLI
  • 3,459
  • 2
  • 23
  • 43