متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 238CI/CD · Code quality

How to check code quality with SonarQube

Code reviews catch logic, but people miss the hard-coded password, the SQL built from user input, the 300-line function and the code without tests. SonarQube analyses every branch and pull request for bugs, vulnerabilities, security hotspots, duplication and coverage, and a quality gate decides pass or fail. This how-to creates a project, runs the scanner locally and in CI, and makes the pipeline wait for the gate.

  • 5 الدروس
  • 3 الأوامر

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

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

238.1

The quick answer

sonar-project.properties
sonar.projectKey=shop-api
sonar.projectName=Shop API
sonar.sources=src
sonar.tests=tests
sonar.python.coverage.reportPaths=coverage.xml
sonar.qualitygate.wait=true
export SONAR_HOST_URL=https://sonar.example.com
export SONAR_TOKEN="$(cat ~/.sonar-token)"
sonar-scanner

238.2

Step by step

  1. Create the project in SonarQube (Projects → Create → manually, or import from GitHub/GitLab) and a project analysis token (My Account → Security). Store the token as a CI secret.
  2. Project settings in the repo (quick answer) — key, source and test folders, the coverage report. sonar.qualitygate.wait=true makes the scanner wait for the result and fail if the gate fails.
  3. Coverage — SonarQube does not run tests; your build creates the report first (for Python pytest --cov=src --cov-report=xml; Java: JaCoCo; JS: lcov).
  4. Run the scanner — locally with the CLI, or the scanner container sonarsource/sonar-scanner-cli; Maven/Gradle projects use their plugin (mvn sonar:sonar).
  5. In CI — after the tests, before the deploy:
.github/workflows/ci.yml (part)
      - run: pytest --cov=src --cov-report=xml
      - uses: SonarSource/sonarqube-scan-action@v5
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ vars.SONAR_HOST_URL }}

Pin actions to a commit SHA in real pipelines.

  1. The quality gate on new code — the default "Sonar way" checks new code only: no new bugs/vulnerabilities, reviewed hotspots, ≥ 80 % coverage on new lines, little duplication. Old problems do not block you; new code must be clean.
  2. Pull request decoration — connect SonarQube to GitHub/GitLab (DevOps platform integration), then results appear as a check and comment on the PR.
  3. Security hotspots — code that needs a human decision (a regex, a crypto call). Review them in the UI: "safe" or "fix".

الأوامر في هذا الدرس

238.3

Check that it worked

The scanner ends with ANALYSIS SUCCESSFUL and QUALITY GATE STATUS: PASSED; the project page shows bugs, vulnerabilities, coverage and duplication; a test PR with a hard-coded password fails the gate and the CI job.

238.4

Common problems

ProblemCauseFix
Not authorizedToken missing or wrong scope.Project analysis token in SONAR_TOKEN.
Coverage 0 %Report not created or wrong path.Run tests with coverage first; fix reportPaths.
Gate fails on old codeGate set on overall code.Use the "new code" conditions; set the new-code period.
PR analysis missingCommunity Edition limits, or no platform integration.Check your edition's features; configure the integration.
Scanner very slowAnalyses node_modules, build output, generated code.sonar.exclusions=**/node_modules/**,**/dist/**.
Pipeline passes although the gate failedGate not awaited.sonar.qualitygate.wait=true.

238.5

Go further

1Why does the default quality gate look at new code only?

So old problems do not block every change, while all new code must meet the standard.

2Does SonarQube run the tests to measure coverage?

No — the build runs the tests and writes a coverage report; SonarQube reads that report.

كل الأوامر في هذا الفصل