要阻止提交,在GitLab CI配置文件中添加一个“阶段”来运行Sonar扫描。如果Sonar扫描失败,就会阻止提交。
以下是示例配置:
stages:
- build
- scan
build:
stage: build
script:
- # build commands here
scan:
stage: scan
script:
- sonar-scanner # run sonar scanner
only:
- master # only run on the master branch
allow_failure: false # fail if the scan fails
在这个示例中,“构建”是默认阶段,用于执行所有必要的构建步骤。在“扫描”阶段中,我们运行Sonar扫描,并指定它只在主分支上运行。allow_failure
设置为false
,表示如果Sonar扫描失败,将防止提交。
请注意,此配置仅适用于主分支。如果您希望在其他分支上阻止提交,可以相应地修改配置。