我正在尝试将 Sonar Scanner 集成到我的 GitLab CI 管道中。 GitLab 运行器与 Sonar 在同一台服务器上运行。使用 docker-compose 创建声纳后,我添加我的 GitLab 令牌,然后从 UI 中选择存储库。它指示添加 SONAR_TOKEN 变量和 SONAR_URL,如图所示。最后添加它们后,我将我的代码推送到 GitLab。
这是我的 docker-compose.yml 声纳文件:
version: "3"
services:
sonarqube:
image: sonarqube:community
depends_on:
- db
environment:
SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
SONAR_JDBC_USERNAME: ${SONAR_JDBC_USERNAME}
SONAR_JDBC_PASSWORD: ${SONAR_JDBC_PASSWORD}
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
ports:
- "9000:9000"
db:
image: postgres:12
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data
volumes:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
postgresql:
postgresql_data:
这是我的gitlab-ci.yml文件:
default:
tags:
- my-custom-tag-1
- my-custom-tag-2
stages:
- test
sonarqube-check:
image:
name: sonarsource/sonar-scanner-cli:latest
entrypoint: [""]
stage: test
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
SONAR_SCANNER_OPTS: "-Dsonar.projectBaseDir=${CI_PROJECT_DIR}/.sonar"
GIT_STRATEGY: "clone" # clone entire repo instead of reusing workspace
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
script:
- sonar-scanner
allow_failure: true
rules:
- if: $CI_COMMIT_BRANCH == 'main'
这是我的 sonar-project.properties 文件位于 .sonar:
sonar.projectKey=${SONAR_PROJECT_KEY}
sonar.qualitygate.wait=true
我不知道默认行为,但在 gitlab runner 日志中我发现了这些行:
INFO: Initialized empty Git repository in /builds/my-project/.git
INFO: 0 files indexed
我看过 SonarQube GitLab Integration 和这个 thread 并关注了这个媒体 post