您可以在 GitLab CI 配置文件中添加一个阶段,以获取 Sonarqube 的分析结果,并在结果未达到预期标准时阻止提交。
以下是示例 GitLab CI 配置文件:
stages:
- build
- test
build:
stage: build
script:
- your build command here
test:
stage: test
script:
- sonar-scanner # run Sonarqube analysis
- sonar-quality-gate # check if analysis results meet quality gate threshold
rules:
- if: $CI_COMMIT_BRANCH == "master" # only run on master branch
when: always # always run, even if previous stages failed
allow_failure: false # fail pipeline if Sonarqube quality gate not met
该配置文件定义了两个阶段:构建和测试。在测试阶段,它运行了 Sonarqube 分析并使用 sonar-quality-gate 命令检查结果是否符合质量门限。如果结果未达到预期标准,则阻止提交。注意,此配置仅在主分支上运行,失败时不允许继续执行。
您可以根据自己的需求修改配置文件,例如更改质量门限或将其应用于其他分支。




