Bitbucket CI配置中SonarQube扫描无法匹配PHPUnit覆盖率报告文件路径的问题求助
Looks like you're hitting a common path mapping issue between your Dockerized PHPUnit tests and the SonarQube scan step. The coverage report uses absolute paths from inside the test container (/usr/src/app/src/...), but SonarQube is looking for files in your pipeline's working directory structure. Let's walk through two solid solutions to fix this:
Method 1: Adjust SonarQube Parameters to Map Paths (No File Moves Needed)
This approach keeps your file structure intact and tells SonarQube how to translate the container paths to your pipeline's actual file locations.
Update your bitbucket-pipelines.yml SonarQube step to add path mapping and source directory variables:
- step: name: Analyse SonarQube image: maven:3.8.1 caches: - sonar script: # Remove the mv commands that move src/tests to /usr/src/app/ if you use this method - pipe: sonarsource/sonarqube-scan:1.0.0 variables: SONAR_HOST_URL: ${SONAR_HOST_URL} SONAR_TOKEN: ${SONAR_TOKEN} SONAR_SOURCES: "back/src" # Tell Sonar where your source files live in the pipeline SONAR_PHP_COVERAGE_REPORT_PATHS: "back/test-reports/result-coverage.xml" # Path to your coverage report SONAR_PHP_COVERAGE_PATHS: "/usr/src/app/src=back/src" # Map container path to pipeline path
What each variable does:
SONAR_SOURCES: Defines the root directory of your source code in the pipeline's working directory.SONAR_PHP_COVERAGE_REPORT_PATHS: Points SonarQube directly to your generated coverage report.SONAR_PHP_COVERAGE_PATHS: This is the magic line—it replaces the container's absolute path prefix (/usr/src/app/src) with your pipeline's actual source directory (back/src), so Sonar can resolve the files correctly.
Method 2: Align Your Pipeline's Working Directory with the Container Path
If you prefer to keep moving the source files to /usr/src/app/ (as you're doing now), you just need to tell SonarQube to use that directory as its project base.
Modify your SonarQube step script and variables like this:
- step: name: Analyse SonarQube image: maven:3.8.1 caches: - sonar script: - mkdir -p /usr/src/app/ - mv back/src/ /usr/src/app/ - mv back/tests/ /usr/src/app/ - mv back/test-reports/ /usr/src/app/ # Move the coverage report to the same directory too - pipe: sonarsource/sonarqube-scan:1.0.0 variables: SONAR_HOST_URL: ${SONAR_HOST_URL} SONAR_TOKEN: ${SONAR_TOKEN} SONAR_PROJECT_BASEDIR: "/usr/src/app" # Set Sonar's project root to the directory you moved files to SONAR_SOURCES: "src" # Sources are now in /usr/src/app/src SONAR_PHP_COVERAGE_REPORT_PATHS: "test-reports/result-coverage.xml" # Report is in /usr/src/app/test-reports
Why this works:
By setting SONAR_PROJECT_BASEDIR to /usr/src/app, SonarQube will look for files relative to that directory. Since your coverage report uses paths like /usr/src/app/src/..., Sonar will match those directly to the files you moved into /usr/src/app/src.
Bonus: Fix the Coverage Report at the Source (Optional)
If you want to avoid path mapping entirely, you can configure PHPUnit to generate coverage reports with relative paths instead of absolute paths. Edit your phpunit.xml file to set the coverage-clover output path relative to your project root, and ensure PHPUnit runs from the project root inside the Docker container. For example:
<phpunit ...> <logging> <log type="coverage-clover" target="./back/test-reports/result-coverage.xml"/> </logging> </phpunit>
This way, the report will use paths like back/src/Controller/TempController.php which SonarQube can resolve directly without extra mapping.
内容的提问来源于stack exchange,提问作者FindL

