I now want to use gitlab-ci to integrate the go project into sonarqube, but the test coverage and static check results displayed by sonar are very different from what I obtain locally.
sonarqube-check:
stage: sonarqube-check
image:
name: registry.cnbita.com:5000/golangci/sonar-scanner-cli-go122:v1
pull_policy: if-not-present
entrypoint: [""]
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
SONAR_HOST_URL: "${SONAR_HOST_URL}"
SONAR_TOKEN: "${SONAR_TOKEN}"
SONAR_PROJECT_KEY: "${SONAR_PROJECT_KEY}"
allow_failure: true
coverage: '/^total:\s*\(statements\)\s*([\d\.]+)%/'
script:
# - bash ./sonar/sonarqube-check.sh
- go mod vendor
- mkdir -p sonar/reports
- golangci-lint run --config=./.golangci.yml ./... --out-format checkstyle > sonar/reports/report.xml
- go test ./... -json > sonar/reports/test-report.out
- go test ./... -v -coverprofile=sonar/reports/coverage.out 2>&1 | go-junit-report > sonar/reports/test-report.xml
- go tool cover -func="sonar/reports/coverage.out"
- |
sonar-scanner \
-Dsonar.host.url=$SONAR_HOST_URL \
-Dsonar.login=$SONAR_TOKEN \
-Dsonar.projectKey=$SONAR_PROJECT_KEY \
-Dsonar.branch.name=$CI_COMMIT_REF_NAME \
-X
sonar-project.properties
sonar.projectKey=hero-os_volume-controller_AZLDEWwhAEg22spS8Gl1
sonar.sourceEncoding=UTF-8
sonar.sources=.
sonar.language=go
sonar.exclusions=**/vendor/**,**/tests/**,**/build/**,**/config/**,**/configs/**,**/deployments/**,**/docs/**,**/hack/**,**/*_test.go
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**
sonar.go.tests.reportPaths=sonar/reports/test-report.out
sonar.go.coverage.reportPaths=sonar/reports/coverage.out
sonar.go.golangci-lint.reportPaths=sonar/reports/report.xml
sonar.qualitygate.wait=true
The picture above shows the unit coverage displayed when gitlabci is executed.The following display shows sonarqube, whether it is unit test coverage or static inspection, it is different. I think sonarqube does not read the report information from the specified file.

