الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

sonar-scanner

Send code to SonarQube / SonarCloud for analysis: bugs, vulnerabilities, security hotspots, code smells, coverage and a quality gate.

المستوى
خدمة DevSecOps
المجموعة
الفحص الأمني والتقوية
يحتاج root
لا
الحزمة
sonar-scanner-cli

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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

-D key=value
Set any property.
-X
Debug output.
-h
Help.
SONAR_TOKEN / SONAR_HOST_URL
Env vars for token and URL.

أمثلة 10

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Run SonarQube for a lab

    podman run -d --name sonarqube -p 9000:9000 docker.io/sonarqube:lts-community
    curl -s http://localhost:9000/api/system/status
  2. 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.xml
  3. 3Run the scanner and wait for the gate

    export SONAR_TOKEN=squ_exampletokenonly0000000000000000000000
    sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.qualitygate.wait=true
  4. 4Run 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-cli
  5. 5Make a coverage report first (Python)

    pytest --cov=app --cov-report=xml
    sonar-scanner
  6. 6GitHub 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 }}
  7. 7Analyze a pull request (decoration)

    sonar-scanner -Dsonar.pullrequest.key=42 -Dsonar.pullrequest.branch=feat/health -Dsonar.pullrequest.base=main
  8. 8Read the gate status with the API

    curl -s -u "$SONAR_TOKEN:" "http://localhost:9000/api/qualitygates/project_status?projectKey=s0sta_shop" | jq '.projectStatus.status'
  9. 9List open vulnerabilities

    curl -s -u "$SONAR_TOKEN:" "http://localhost:9000/api/issues/search?componentKeys=s0sta_shop&types=VULNERABILITY&resolved=false" | jq '.issues[].message'
  10. 10Debug a failed analysis

    sonar-scanner -X 2>&1 | tail -40

انتبه

  • Use a project analysis token, not your user token, in CI.
  • fetch-depth: 0 is needed so Sonar knows "new code" from Git blame.
  • The SonarQube container needs vm.max_map_count=524288 on 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.

رموز الخروج

0Success (gate passed if waited).
1Error or quality gate failed (with qualitygate.wait).

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man sonar-scanner