Home · RSS · E-Mail · GitHub · GitLab · Mastodon · Twitter · LinkedIn

Get the SonarQube Quality Gate status for the current commit

first published:

Update 2021-03-05: Simplified Code

Update 2021-01-11: Added authentication

When you use SonarQube and want to fail a pipeline if the Quality Gate result for the current git commit is not “OK”, this script might be helpful for you.

You can always find the latest version in my git repository. Open an issue or a pull request for questions and enhancements.

 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
#!/usr/bin/env bash

# Description
# ===========
# Get the SonarQube Quality Gate status of a specific project for the current git commit.
# When the Quality Gate analysis is "OK", return 0, otherwise, return 1.
# Well suitable for usage in a build pipeline.
#
# Requirements
# ============
# git, curl, jq
#
# Usage
# =====
# Execute this script `./sonar-status.sh <host> <auth_token> <project_key>` and check for the return code.
# Example: `./sonar-status.sh http://localhost:9000 d7981b9c1333bef3ff83502cea6f6cd6ce4d70f6 test`

set -euf -o pipefail

SONAR_HOST=$1
AUTH_TOKEN=$2 # project key token or user token (see https://docs.sonarqube.org/latest/user-guide/user-token/)
PROJECT_KEY=$3

GIT_COMMIT=$(git rev-parse HEAD)


retry=0
while [  $retry -lt 6 ]; do
    # get latest analysis id to corresponding git commit hash
    analysis_id=$(curl -s -u "$AUTH_TOKEN": "$SONAR_HOST"/api/project_analyses/search?project="$PROJECT_KEY" | jq '.analyses[] | select(.revision == "'"$GIT_COMMIT"'") | .key' | head -n 1)

    # remove double quotes from analysis id (e.g. "AWv6wb07Y5FuS8wxa-xk" -> AWv6wb07Y5FuS8wxa-xk)
    analysis_id=$(echo "$analysis_id" | tr -d "\"\`'")

    # get quality gate status of this anlysis
    analysis_status=$(curl -s -u "$AUTH_TOKEN": "$SONAR_HOST"/api/qualitygates/project_status\?analysisId="$analysis_id" | jq '.projectStatus.status')

    # break this loop when analysis was found,
    # otherwise, try again (next loop iteration)
    if [ "$analysis_status" != null ]
        then
        break
    fi

    echo "Retry $retry: $PROJECT_KEY"
    (( retry=retry+1 ))
    
    sleep 10s
done

# print and return status depending on result
printf "%s \t %-35s \t $SONAR_HOST/dashboard?id=%s\n" "$analysis_status" "$PROJECT_KEY" "$PROJECT_KEY"
if [ "$analysis_status" != \""OK\"" ]
    then
        exit 1
fi



Home · RSS · E-Mail · GitHub · GitLab · Mastodon · Twitter · LinkedIn