首页 > 解决方案 > 如何为 Sonar Qube 编写 Gitlab 管道作业

问题描述

嗨,就我而言,我使用 Gitlab 社区和 Sonar Qube。Sonar 托管在带有 Docker 容器的 Azure Ubuntu VM 上。在我的 Sonar 在 Windows 机器上之前,这里是 gitlab.yml 的一部分。

    variables:
  SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"  # Defines the location of the analysis task cache
  GIT_DEPTH: "0"  # Tells git to fetch all the branches of the project, required by the analysis task
  
stages:
  - build
  - test

before_script:
 - Set-Variable -Name "time" -Value (Get-Date -Format "%H:%m")
 - echo ${time}
 - echo "started by ${GITLAB_USER_NAME}"



build:
  stage: build
  only:
    - branches
  script:
    - echo "running scripts in the build job"
    - choco feature enable -n=allowGlobalConfirmation
    - choco install netfx-4.6.2-devpack
    - choco install dotnetcore-sdk
    - nuget restore -ConfigFile .\nuget.config 
    - msbuild ".\MyProject\MyProject.csproj" /p:DeployOnBuild=true /p:PublishProfile="Local Publish" /p:WarningLevel=0
    - dotnet build ".\MyProject.Tests\MyProject.Tests.csproj" --configuration Release
  artifacts:
    paths:
      - Publish
      - .\MyProject.Tests\bin\Release\netcoreapp3.1\**
      - .\MyProject
    expire_in: 1 day
  after_script:
    - Remove-Item -Recurse -Force .\packages\
    

    

sonarcloud-check:
  image:
    name: sonarsource/sonar-scanner-cli:latest
    entrypoint: [""]
  cache:
    key: "${CI_JOB_NAME}"
    paths:
      - .sonar/cache
  dependencies:
    - build
  script:
    - choco install sonarqube-scanner.portable
    - SonarScanner.MSBuild.exe begin /k:"myProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="8f6658e7684de225a4f45c7cf3466d462a95c1c7"   
    - nuget restore -ConfigFile .\nuget.config
    - MsBuild.exe ./MyProject /t:Rebuild
    - SonarScanner.MSBuild.exe end /d:sonar.login="8f6658e7684de225a4f45c7cf3466d462a95c1c7"
  only:
    - merge_requests
    - master
    - develop
    - GitLabQualityTool

我知道这个 yml 不是最好的,但它的工作。主要项目在 .net 4.6.2 上。Sonar 在 Ubuntu 上运行,端口为 80:9000。关键是我一周前就开始使用 Azure、Docker 和 Sonar。有人可以帮我配置 yml 吗?

标签: azuredockergitlabsonarqubeubuntu-18.04

解决方案


推荐阅读