Skip to content

Commit 379a143

Browse files
committed
feat: reusable sonar workflow
1 parent 1d9b1c0 commit 379a143

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

.github/workflows/sonar.yml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: SonarQube Analysis
2+
on:
3+
workflow_call:
4+
secrets:
5+
SONAR_TOKEN:
6+
required: true
7+
SONAR_HOST:
8+
required: true
9+
GITHUB_TOKEN:
10+
required: true
11+
12+
permissions:
13+
pull-requests: write
14+
contents: read
15+
16+
17+
jobs:
18+
sonar:
19+
name: Run SonarQube
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v4
25+
with:
26+
fetch-depth: 0
27+
28+
- name: Set up JDK
29+
uses: actions/setup-java@v4
30+
with:
31+
distribution: 'temurin'
32+
java-version: '11'
33+
34+
- name: Setup SonarQube Scanner
35+
uses: SonarSource/sonarqube-scan-action@v5.1.0
36+
env:
37+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
39+
SONAR_HOST_URL: ${{ secrets.SONAR_HOST }}
40+
with:
41+
args: >
42+
-Dsonar.projectKey=${{ github.event.repository.name }}
43+
-Dsonar.sources=.
44+
45+
- name: Install jq (for parsing JSON)
46+
run: sudo apt-get update && sudo apt-get install -y jq
47+
48+
- name: Wait for SonarQube Analysis to complete
49+
id: wait-sonar
50+
run: |
51+
PROJECT_KEY="${{ github.event.repository.name }}"
52+
SONAR_URL="${{ secrets.SONAR_HOST }}"
53+
SONAR_TOKEN="${{ secrets.SONAR_TOKEN }}"
54+
55+
MAX_ATTEMPTS=12
56+
ATTEMPT=0
57+
58+
while [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do
59+
echo "Waiting for SonarQube analysis... Attempt $((ATTEMPT+1))"
60+
61+
RESPONSE=$(curl -s -u $SONAR_TOKEN: "$SONAR_URL/api/measures/component?component=$PROJECT_KEY&metricKeys=code_smells,bugs,vulnerabilities,coverage,duplicated_lines_density")
62+
echo "$RESPONSE" > sonar-report.json
63+
64+
ANALYSIS_READY=$(jq -r '.component.measures | length' sonar-report.json)
65+
66+
if [ "$ANALYSIS_READY" != "0" ]; then
67+
echo "Analysis is ready!"
68+
break
69+
fi
70+
71+
ATTEMPT=$((ATTEMPT+1))
72+
sleep 10
73+
done
74+
75+
if [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then
76+
echo "Timeout waiting for SonarQube analysis."
77+
exit 1
78+
fi
79+
80+
- name: Get SonarQube analysis report
81+
id: sonar-report
82+
run: |
83+
# Assumes sonar-report.json is already created
84+
BUGS=$(jq -r '.component.measures[] | select(.metric=="bugs") | .value' sonar-report.json)
85+
CODE_SMELLS=$(jq -r '.component.measures[] | select(.metric=="code_smells") | .value' sonar-report.json)
86+
VULNERABILITIES=$(jq -r '.component.measures[] | select(.metric=="vulnerabilities") | .value' sonar-report.json)
87+
COVERAGE=$(jq -r '.component.measures[] | select(.metric=="coverage") | .value' sonar-report.json)
88+
DUPLICATION=$(jq -r '.component.measures[] | select(.metric=="duplicated_lines_density") | .value' sonar-report.json)
89+
90+
REPORT="🚨 **SonarQube Report**
91+
- 🐞 Bugs: $BUGS
92+
- 💨 Code Smells: $CODE_SMELLS
93+
- 🔐 Vulnerabilities: $VULNERABILITIES
94+
- 🧪 Coverage: $COVERAGE%
95+
- 📦 Duplication: $DUPLICATION%"
96+
97+
echo "$REPORT"
98+
echo "report<<EOF" >> $GITHUB_OUTPUT
99+
echo "$REPORT" >> $GITHUB_OUTPUT
100+
echo "EOF" >> $GITHUB_OUTPUT
101+
102+
- name: Comment on PR
103+
if: github.event_name == 'pull_request'
104+
uses: marocchino/sticky-pull-request-comment@v2
105+
with:
106+
header: sonar-report
107+
message: ${{ steps.sonar-report.outputs.report }}
108+

0 commit comments

Comments
 (0)