Trying to fetch the status of the URL for sonarqube quality gate and check if the status is "OK" the condition should pass or if the status is "ERROR" then it should fail.
quality_gatesstatus=$(curl -u $SONAR_TOKEN:https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY | grep -Po '"status": *\K"[^"]*"')
echo $SONAR_PR_KEY
echo "Checking the Sonar Quality gate status"
if ("$quality_gatesstatus" != "OK") && ("$quality_gatesstatus" != "NONE")
then
echo "check sonar server and fix the issues: $quality_gatesstatus"
exit 1
else
echo "Quality gate succeeded"
fi
But its not working as per the IF statement, its going always to the else condition
The line:
if ("$quality_gatesstatus" != "OK") && ("$quality_gatesstatus" != "NONE")
is evaluated as follows (not precisely, this is a heuristic):
$quality_gatestatus
is expanded to some string, sayS
S
is executed as a command, with the arguments!=
andOK
!=
andNONE
. If that command succeeds then the first block of commands is executed. Otherwise, the commands in theelse
block are executed.The error you are seeing is because the string
S
is not an executable command. Almost certainly what you actually want is:but more likely you want a
case
statement:The syntax of the shell is a bit counter-intuitive. It is not
if condition; then commands; fi
. It isif commands; then commands; fi
. In other words, when you writeif [ 5 = 5 ]
, the[
and]
are not part of the shell syntax. Instead, the command[
is executed with arguments5
,=
, and]
. Although[
is probably the most common command executed in anif
block, it can be any set of commands, and it is common to see constructs likeif grep ...
orif shh ...
orif curl ...
. It is slightly less common to seeif cmd1; cmd2; cmd3; then ...
, but you will see it now and again.