GitHub Actions中SonarQube扫描Next.js应用失败求助
解决SonarQube通过GitHub Actions分析时的413 Request Entity Too Large错误
问题场景
使用GitHub Actions作为CI/CD工具,对Next.js项目执行SonarQube代码分析,Workflow配置如下:
on: push: branches: - dev jobs: sonaranalysis: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 with: fetch-depth: 0 - name: SonarQube Analysis uses: sonarsource/sonarqube-scan-action@master env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} - name: Sonarqube Quality Gate Check id: sonarqube-quality-gate-check uses: sonarsource/sonarqube-quality-gate-action@master timeout-minutes: 5 env: SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} - name: "Example show SonarQube Quality Gate Status value" run: echo "The Quality Gate status is ${{ steps.sonarqube-quality-gate-check.outputs.quality-gate-status }}" createpr: needs: sonaranalysis runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 with: ref: staging - name: Reset branch run: | git fetch origin dev:dev git reset --hard dev - name: Create Pull Request uses: peter-evans/create-pull-request@v5 with: branch: stage-promotion delete-branch: true
执行工作流时出现如下错误:
INFO: SCM Publisher 487 source files to be analyzed INFO: SCM Publisher 487/487 source files have been analyzed (done) | time=6530ms INFO: CPD Executor 59 files had no CPD blocks INFO: CPD Executor Calculating CPD for 387 files INFO: CPD Executor CPD calculation finished (done) | time=175ms INFO: Analysis report generated in 141ms, dir size=4.5 MB INFO: Analysis report compressed in 994ms, zip size=2.6 MB INFO: ------------------------------------------------------------------------ INFO: EXECUTION FAILURE INFO: ------------------------------------------------------------------------ INFO: Total time: 1:26.977s INFO: Final Memory: 18M/64M INFO: ------------------------------------------------------------------------ ERROR: Error during SonarScanner execution java.lang.IllegalStateException: Failed to upload report: Error 413 on ***/api/ce/submit?projectKey=astris-fe : <html> <head><title>413 Request Entity Too Large</title></head> <body> <center><h1>413 Request Entity Too Large</h1></center> <hr><center>nginx/1.20.1</center> </body> </html> at org.sonar.scanner.report.ReportPublisher.upload(ReportPublisher.java:226) at org.sonar.scanner.report.ReportPublisher.execute(ReportPublisher.java:154) at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:376) at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188) at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167) at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:137) at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188) at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167) at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72) at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66) at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60) at com.sun.proxy.$Proxy0.execute(Unknown Source) at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189) at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138) at org.sonarsource.scanner.cli.Main.execute(Main.java:126) at org.sonarsource.scanner.cli.Main.execute(Main.java:81) at org.sonarsource.scanner.cli.Main.main(Main.java:62) Caused by: org.sonarqube.ws.client.HttpException: Error 413 on ***/api/ce/submit?projectKey=astris-fe : <html> <head><title>413 Request Entity Too Large</title></head> <body> <center><h1>413 Request Entity Too Large</h1></center> <hr><center>nginx/1.20.1</center> </body> </html> at org.sonarqube.ws.client.BaseResponse.failIfNotSuccessful(BaseResponse.java:36) at org.sonar.scanner.bootstrap.DefaultScannerWsClient.failIfUnauthorized(DefaultScannerWsClient.java:126) at org.sonar.scanner.bootstrap.DefaultScannerWsClient.call(DefaultScannerWsClient.java:89) at org.sonar.scanner.report.ReportPublisher.upload(ReportPublisher.java:224) ... 21 more ERROR: ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.
SonarQube部署在NGINX代理之后,已尝试重装SonarQube、更换CentOS版本,但问题仍存在。
解决方案
这个错误是NGINX默认的请求体大小限制导致的,NGINX默认client_max_body_size为1M,而你的分析报告压缩后有2.6M,超过了限制,所以返回413错误。解决步骤如下:
修改NGINX配置文件
找到SonarQube对应的NGINX server配置块(通常在/etc/nginx/conf.d/或/etc/nginx/sites-available/目录下),添加或修改client_max_body_size参数,设置一个足够大的值,比如10M:server { # 其他现有配置 client_max_body_size 10M; # 其他现有配置 }如果需要全局生效,可以在
http块中设置该参数。重启NGINX服务
执行以下命令使配置生效:sudo systemctl restart nginx验证配置
重新触发GitHub Actions工作流,检查SonarQube分析是否能正常完成。
补充说明
- 不需要修改SonarQube本身的配置,因为问题根源在NGINX的请求大小限制。
- 如果后续报告体积继续增大,可以适当调高
client_max_body_size的值,比如20M或50M。
内容的提问来源于stack exchange,提问作者syog
相关产品推荐
相关产品推荐

