SonarQube+Gitlab从零搭建自动化持续代码扫描质量平台(下)

104 阅读2分钟

阅读这篇文章前,请先阅读:SonarQube+Gitlab从零搭建自动化持续代码扫描质量平台

四、初始化扫描项目

注意: 这里的token 就是gitlab扫描这个项目的一个登录密码,要记录下来

token: 2a96640cda4a8027ed87cb0b411a16d0e316c433

sonar-scanner \  -Dsonar.projectKey=ci_ci-test_AZKlE63t1GDH1gppbQ0a \  -Dsonar.sources=. \  -Dsonar.host.url=http://gitlab.rubing.top:9000 \  -Dsonar.login=2a96640cda4a8027ed87cb0b411a16d0e316c433

五、安装gitlab-runner并注册runner

这个界面只有管理员有权限

// 先拉取gitlab-runner 镜像   docker pull gitlab-runner// 新建gitlab-runner 容器并进入容器注册runnerdocker exec gitlab-runner gitlab-runner register -n \--url http://gitlab.rubing.top/ \--registration-token glrt-CCNzJPfyBgzinYxpg4aA \--tag-list ci-runner \--executor docker \--docker-image docker \--docker-volumes /var/run/docker.sock:/var/run/docker.sock \--description "ci-runner"

  • 6、编写.yml文件

    stages: - sonarsonar: stage: sonar before_script: - echo "FAIL" > .sonar_job_status image: sonarsource/sonar-scanner-cli:latest script: - sonar-scanner -Dsonar.projectKey=ci_ci-test_AZKlE63t1GDH1gppbQ0a -Dsonar.sources=. -Dsonar.host.url=gitlab.rubing.top:9000 -Dsonar.login=2a96640cda4a8027ed87cb0b411a16d0e316c433

  • 7、提交代码,运行流水线,进行代码扫描

  • 8、设置代码合并进行代码扫描作业

    stages: - sonar sonar: stage: sonar tags: - ci-runner before_script: - echo "FAIL" > .sonar_job_status image: sonarsource/sonar-scanner-cli:latest script: - sonar-scanner -Dsonar.projectKey=ci_ci-test_AZKlE63t1GDH1gppbQ0a -Dsonar.sources=. - Dsonar.host.url=gitlab.rubing.top:9000 -Dsonar.login=2a96640cda4a8027ed87cb0b411a16d0e316c433 -Dsonar.pullrequest.branch=CIMERGEREQUESTSOURCEBRANCHNAMEDsonar.pullrequest.key={CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} -Dsonar.pullrequest.key={CI_MERGE_REQUEST_IID} -Dsonar.pullrequest.base=CIMERGEREQUESTTARGETBRANCHNAMEDsonar.projectDescription={CI_MERGE_REQUEST_TARGET_BRANCH_NAME} -Dsonar.projectDescription={CI_PROJECT_TITLE} -Dsonar.links.homepage=CIPROJECTURLDsonar.gitlab.refname={CI_PROJECT_URL} -Dsonar.gitlab.ref_name={CI_COMMIT_REF_NAME} -Dsonar.gitlab.commit_sha=CICOMMITSHADsonar.gitlab.projectid={CI_COMMIT_SHA} -Dsonar.gitlab.project_id={CI_PROJECT_PATH} -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=2000 only: - merge_requests

  • 9、进行代码合并、跑流水线