SonarQube는 코드 품질 및 보안 취약점을 정적 분석으로 검사하는 오픈소스 플랫폼이다. 버그, 코드 스멜, 보안 취약점, 코드 중복을 자동 탐지하고 기술 부채를 측정한다.
주요 분석 항목
| 항목 | 설명 |
|---|
| 버그 | 런타임 오류를 유발하는 코드 |
| 취약점 | 보안 취약점 (OWASP Top 10) |
| 코드 스멜 | 유지보수를 어렵게 하는 코드 |
| 코드 중복 | 복사-붙여넣기 코드 비율 |
| 커버리지 | 테스트 커버리지 수집·표시 |
| 복잡도 | 순환 복잡도 측정 |
프로젝트 분석 설정
properties
# sonar-project.properties
sonar.projectKey=my-project
sonar.projectName=My Project
sonar.sources=src
sonar.tests=src
sonar.test.inclusions=**/*.test.ts,**/*.spec.ts
sonar.typescript.lcov.reportPaths=coverage/lcov.info
sonar.host.url=http://localhost:9000
sonar.token=sqp_xxxxxxxxxxxxx
GitHub Actions 통합
yaml
- name: SonarQube Scan
uses: SonarSource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
with:
args: >
-Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
-Dsonar.pullrequest.branch=${{ github.head_ref }}
-Dsonar.pullrequest.base=${{ github.base_ref }}
- name: Quality Gate 확인
uses: SonarSource/sonarqube-quality-gate-action@master
timeout-minutes: 5
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
Quality Gate 기준 예시
Quality Gate: 신규 코드 기준
- 신규 버그 = 0
- 신규 취약점 = 0
- 신규 코드 스멜 ≤ 5
- 커버리지 ≥ 80%
- 중복 코드 ≤ 3%
관련 문서