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
相关产品推荐
相关产品推荐

