Github Action集成SonarCloud时代码覆盖率显示0%求助
问题:Github Action上传Cypress覆盖率到SonarCloud显示0%,路径解析失败
问题详情
- SonarCloud显示代码覆盖率为0%,流水线输出警告:
WARN: Could not resolve 7 file paths in [/github/workspace/coverage/lcov.info]
WARN: First unresolved path: C:\Users\ferso\OneDrive\Documentos\Faculdade\cypress-test\src\App.jsx (Run in DEBUG mode to get full list of unresolved paths)
- 已尝试用sed工具修正路径,配置
sonar.javascript.lcov.reportPaths=coverage/lcov.info,但均未生效 - 当前配置文件:
sonar-project.properties:
Github工作流片段:sonar.projectKey=fsoupimenta_cypress-test sonar.organization=fsoupimenta sonar.javascript.lcov.reportPaths=coverage/lcov.info sonar.javascript.file.suffixes=.js,.jsx- name: fix code coverage paths working-directory: ./coverage run: | sed -i 's/\/home\/runner\/work\/cypress-test\/cypress-test\//\/github\/workspace\//g' lcov.info sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' lcov.info sed -i 's/\/home\/runner\/work\/cypress-test\/cypress-test\//\/github\/workspace\//g' sonar-cloud-reporter.xml - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
解决步骤
1. 替换lcov.info中的Windows绝对路径
现有sed命令仅处理了Linux格式路径,但警告中出现的是Windows本地路径(C:\Users\ferso\...),需新增替换规则:
在fix code coverage paths步骤中添加:
# 精准替换你的Windows本地路径前缀 sed -i 's/C:\\Users\\ferso\\OneDrive\\Documentos\\Faculdade\\cypress-test\\/\/github\/workspace\//g' lcov.info # 或者用通用规则匹配所有Windows路径前缀(如果路径有变动更灵活) sed -i 's/[A-Z]:\\.*\\cypress-test\\/\/github\/workspace\//g' lcov.info
2. 确保步骤顺序正确
必须在Cypress生成覆盖率报告之后执行路径替换,完整步骤顺序参考:
- name: Run Cypress tests with coverage run: npm run cypress:run -- --env coverage=true - name: fix code coverage paths working-directory: ./coverage run: | # 先处理Windows路径 sed -i 's/C:\\Users\\ferso\\OneDrive\\Documentos\\Faculdade\\cypress-test\\/\/github\/workspace\//g' lcov.info # 再处理Linux路径 sed -i 's/\/home\/runner\/work\/cypress-test\/cypress-test\//\/github\/workspace\//g' lcov.info sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' lcov.info - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3. 验证路径替换结果
添加步骤确认lcov.info中的路径已修正:
- name: Verify coverage paths working-directory: ./coverage run: cat lcov.info | grep "App.jsx"
执行后输出应为/github/workspace/src/App.jsx这类符合容器环境的路径。
4. 完善SonarCloud配置
在sonar-project.properties中添加源码目录指定,确保Sonar能正确找到对应文件:
sonar.projectKey=fsoupimenta_cypress-test sonar.organization=fsoupimenta sonar.sources=src # 指定源码根目录 sonar.javascript.lcov.reportPaths=coverage/lcov.info sonar.javascript.file.suffixes=.js,.jsx
内容的提问来源于stack exchange,提问作者Fernando Pimenta
相关产品推荐
相关产品推荐

