-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonarDelay.sh
72 lines (55 loc) · 2.29 KB
/
SonarDelay.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
# this script checks the status of a quality gate for a particular analysisID
# approach taken from https://docs.sonarqube.org/display/SONARQUBE53/Breaking+the+CI+Build
# When SonarScanner executes, the compute engine task is given an id
# The status of this task, and analysisId for the task can be checked at
# /api/ce/task?id=taskid
# When the status is SUCCESS, the quality gate status can be checked at
# /api/qualitygates/project_status?analysisId=analysisId
#set errexit
#set pipefail
#set nounset
# in newer versions of sonar scanner the default report-task.txt location may be different
#REPORT_PATH="./customapi/target/sonar/report-task.txt"
#REPORT_PATH=".sonar/report-task.txt"
CE_TASK_ID_KEY="ceTaskId="
#SONAR_ACCESS_TOKEN="9000"
SLEEP_TIME=5
echo "QG Script --> Using SonarQube instance ${sonarurl}"
# get the compute engine task id
ce_task_id=$(cat $1 | grep $CE_TASK_ID_KEY | cut -d'=' -f2)
echo "QG Script --> Using task id of ${ce_task_id}"
if [ -z "$ce_task_id" ]; then
echo "QG Script --> No task id found"
exit 1
fi
# grab the status of the task
# if CANCELLED or FAILED, fail the Build
# if SUCCESS, stop waiting and grab the analysisId
wait_for_success=true
while [ "${wait_for_success}" = "true" ]
do
ce_status=$(curl --user ${sonartoken}: ${sonarurl}/api/ce/task?id="${ce_task_id}" | jq -r .task.status)
echo "QG Script --> Status of SonarQube task is ${ce_status}"
if [ "${ce_status}" = "CANCELLED" ]; then
echo "QG Script --> SonarQube Compute job has been cancelled - exiting with error"
exit 1
fi
if [ "${ce_status}" = "FAILED" ]; then
echo "QG Script --> SonarQube Compute job has failed - exiting with error"
exit 1
fi
if [ "${ce_status}" = "SUCCESS" ]; then
wait_for_success=false
fi
sleep 10
done
ce_analysis_id=$(curl --user ${sonartoken}: ${sonarurl}/api/ce/task?id=$ce_task_id | jq -r .task.analysisId)
echo "QG Script --> Using analysis id of ${ce_analysis_id}"
# get the status of the quality gate for this analysisId
qg_status=$(curl --user ${sonartoken}: ${sonarurl}/api/qualitygates/project_status?analysisId="${ce_analysis_id}" | jq -r .projectStatus.status)
echo "QG Script --> Quality Gate status is ${qg_status}"
if [ "${qg_status}" != "OK" ]; then
echo "Pipeline aborted due to quality gate failure"
exit 1
fi