You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI/CD配置:仅Merge Request触发SonarQube阶段

GitLab CI/CD配置调整方案

针对你的需求,调整后的配置如下:

stages:
  - build-stage
  - sonar-stage

variables:
  SONAR_HOST: "http://asdftestserver01.abc.com:9000"
  SONAR_PROJECT_KEY: "test-api"
  SONAR_LOGIN_TOKEN: "aaabbbbbbcccccccddddddddd"

build_stage:
  stage: build-stage
  only:
    - cicd-setup
    - merge_requests
  environment:
    name: stage
  script:
    - echo "build stage started"
    - mvn clean install
    
api_sonarqube_analysis:
  stage: sonar-stage
  only:
    - merge_requests
  script:
    - pwd
    - echo "sonarqube started"
    # 修正变量引用错误:$PROJECT_KEY 改为 $SONAR_PROJECT_KEY
    - mvn sonar:sonar -Dsonar.projectKey=$SONAR_PROJECT_KEY -Dsonar.host.url=$SONAR_HOST -Dsonar.login=$SONAR_LOGIN_TOKEN 

关键调整说明:

  • build_stage:在only中同时添加cicd-setup和merge_requests,确保推送代码到该分支时、发起MR时都会执行构建
  • api_sonarqube_analysis:仅保留merge_requests在only列表中,这样只有发起MR时才会触发Sonar分析,推送分支时不会执行
  • 修正了Sonar脚本中的变量引用错误:原脚本里的$PROJECT_KEY应该对应定义好的$SONAR_PROJECT_KEY,否则会导致变量未定义的错误

内容的提问来源于stack exchange,提问作者user18665270

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 03:58:19