为GitHub上的Objective-C项目配置SonarQube Action遇错求助
问题
我正尝试为托管在GitHub上的Objective-C项目配置SonarQube,SonarQube UI提供的设置无问题,但使用的GitHub Action执行失败。当前使用的Action配置如下:
jobs: sonarcloud: name: SonarCloud runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - 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 }}
执行时出现错误:
ERROR: Error during SonarScanner execution java.lang.UnsupportedOperationException: The only way to get an accurate analysis of C/C++/Objective-C files is by using the SonarSource build-wrapper and setting the property "sonar.cfamily.build-wrapper-output" or by using Clang Compilation Database and setting the property "sonar.cfamily.compile-commands". None of these two options were specified.
请问是否有可运行SonarSource build-wrapper的GitHub Action,或是最佳解决步骤是什么?
最佳解决步骤
方法一:直接在Action中使用SonarSource build-wrapper
SonarSource没有单独的build-wrapper Action,但可以直接在GitHub Action流程中下载、配置并使用它,步骤如下:
- 切换运行环境:Objective-C依赖Xcode工具链,将
runs-on从ubuntu-latest改为macos-latest,避免额外配置编译环境。 - 下载解压build-wrapper:添加步骤获取macOS版本的build-wrapper并解压。
- 用build-wrapper包裹编译命令:执行项目编译时,通过build-wrapper捕获编译信息,输出到指定目录。
- 配置Sonar扫描参数:在SonarCloud Scan步骤中指定build-wrapper的输出目录。
修改后的完整Action配置:
jobs: sonarcloud: name: SonarCloud runs-on: macos-latest steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - name: 安装并解压build-wrapper run: | curl -L https://sonarcloud.io/static/cpp/build-wrapper-macosx-x86.zip -o build-wrapper.zip unzip build-wrapper.zip - name: 通过build-wrapper执行编译 run: | ./build-wrapper-macosx-x86/build-wrapper-macosx-x86 --out-dir build-wrapper-output xcodebuild -scheme YourScheme -configuration Release clean build # 替换YourScheme为项目实际的Scheme名称 - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} with: args: > -Dsonar.cfamily.build-wrapper-output=build-wrapper-output
方法二:使用Clang Compilation Database
如果项目适合生成compile_commands.json,可以通过该文件提供编译信息:
- 安装bear工具:用于生成编译数据库文件。
- 生成compile_commands.json:用bear包裹编译命令,生成对应的数据库文件。
- 配置Sonar扫描参数:指定编译数据库文件路径。
修改后的关键步骤示例:
# ... 其他步骤 ... - name: 安装bear并生成编译数据库 run: | brew install bear bear -- xcodebuild -scheme YourScheme -configuration Release clean build # 替换YourScheme为项目实际的Scheme名称 - name: SonarCloud Scan uses: SonarSource/sonarcloud-github-action@master env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} with: args: > -Dsonar.cfamily.compile-commands=compile_commands.json
内容的提问来源于stack exchange,提问作者queeg
相关产品推荐
相关产品推荐

