هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
SonarQube (self-hosted, port 9000) and SonarQube Cloud analyze code in many languages. The scanner runs in CI, reads sonar-project.properties, and uploads the results. The server applies a quality gate (for example: no new bugs, no new vulnerabilities, coverage on new code ≥ 80 %) and can block the merge. Java/Maven, Gradle and .NET have their own scanners; sonar-scanner is for everything else.
طريقة الكتابة
sonar-scanner [-Dkey=value ...]الأجزاء الرئيسية
Key properties
sonar.projectKey- Unique project ID.
sonar.sources- Source folders.
sonar.tests- Test folders.
sonar.host.url- Server URL.
sonar.token- Analysis token (from env SONAR_TOKEN).
sonar.exclusions- Files to skip.
sonar.python.coverage.reportPaths/sonar.javascript.lcov.reportPaths- Coverage reports.
sonar.qualitygate.wait=true- Wait for the gate and fail the job if red.
الخيارات 4
الأكثر استخدامًا
Flags
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 10
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1Run SonarQube for a lab
2A project file
cat sonar-project.properties sonar.projectKey=s0sta_shop sonar.sources=app sonar.tests=tests sonar.exclusions=**/migrations/** sonar.python.coverage.reportPaths=coverage.xml3Run the scanner and wait for the gate
export SONAR_TOKEN=squ_exampletokenonly0000000000000000000000 sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.qualitygate.wait=true4Run the scanner in a container
docker run --rm -e SONAR_HOST_URL=http://sonar.lab.local:9000 -e SONAR_TOKEN -v "$PWD:/usr/src" sonarsource/sonar-scanner-cli5Make a coverage report first (Python)
pytest --cov=app --cov-report=xml sonar-scanner6GitHub Actions
cat .github/workflows/sonar.yml - uses: actions/checkout@v4 with: { fetch-depth: 0 } - uses: SonarSource/sonarqube-scan-action@v4 env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}7Analyze a pull request (decoration)
sonar-scanner -Dsonar.pullrequest.key=42 -Dsonar.pullrequest.branch=feat/health -Dsonar.pullrequest.base=main8Read the gate status with the API
curl -s -u "$SONAR_TOKEN:" "http://localhost:9000/api/qualitygates/project_status?projectKey=s0sta_shop" | jq '.projectStatus.status'9List open vulnerabilities
curl -s -u "$SONAR_TOKEN:" "http://localhost:9000/api/issues/search?componentKeys=s0sta_shop&types=VULNERABILITY&resolved=false" | jq '.issues[].message'10Debug a failed analysis
sonar-scanner -X 2>&1 | tail -40
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- Use a project analysis token, not your user token, in CI.
fetch-depth: 0is needed so Sonar knows "new code" from Git blame.- The SonarQube container needs
vm.max_map_count=524288on the host (Elasticsearch inside).
الملفات
sonar-project.properties- Project settings.
نصائح للحفظ
- Make the gate strict on new code only, so old projects can start without fixing everything first.
رموز الخروج
0 | Success (gate passed if waited). |
1 | Error or quality gate failed (with qualitygate.wait). |