简体   繁体   English

在 gitlab-ci 中运行声纳扫描仪

[英]Running Sonar Scanner in gitlab-ci

I'm trying to build a pipeline that runs sonarqube for a python application.我正在尝试为 python 应用程序构建一个运行 sonarqube 的管道。 This is my first time using a gitlab-ci so I was hoping to get some direction.这是我第一次使用 gitlab-ci,所以我希望能得到一些指导。 I know to run sonarqube, you have to somehow call sonar scanner within the yml.我知道要运行 sonarqube,您必须以某种方式在 yml 中调用声纳扫描仪。 That is where I'm stuck.那就是我卡住的地方。 How do I set up/install a sonar-scanner in my docker?如何在我的 docker 中设置/安装声纳扫描仪? I'm running a docker with a python image.我正在运行带有 python 图像的 docker。 Any help would be greatly appreciated.任何帮助将不胜感激。 I honestly am not understanding how locally downloading the sonar scanner package makes sense.老实说,我不明白在本地下载声纳扫描仪 package 有什么意义。 Does the git pipeline default to looking at the packages installed on my local computer? git 管道是否默认查看安装在我本地计算机上的包?

There are two solutions available:有两种可用的解决方案:

  1. You can install sonar-scanner on your local machine using SonarScanner Documentaion As sonar-scanner requires Java-11 to be installed on the machine you will have to install openjdk-11-jdk-headless with您可以使用SonarScanner Documentaion在本地计算机上安装 sonar-scanner 由于 sonar-scanner 需要在计算机上安装 Java-11,因此您必须安装 openjdk-11-jdk-headless
   sudo apt install openjdk-11-jdk-headless

And install the sonar scanner on your machine using the steps provided in the documentation.并使用文档中提供的步骤在您的机器上安装声纳扫描仪。 2. You can use a docker image that has sonar-scanner installed in it as base image and use that image to build your own docker image with the required python versions and required softwares. 2. 您可以使用安装了声纳扫描仪的 docker 映像作为基础映像,并使用该映像构建您自己的 docker 映像,其中包含所需的 python 版本和所需的软件。

For example you can use following docker image as a reference image for your docker image例如,您可以使用以下 docker 图像作为 docker 图像的参考图像

   bluelabseu/sonar-scanner:4.3.0-7879

Sample docker file with using a sonar scanner image to create a image for python project.示例 docker 文件,使用声纳扫描仪图像为 python 项目创建图像。

    FROM bluelabseu/sonar-scanner:4.3.0-7248`
    RUN apt-get update && apt-get install python2.7 -y && apt-get install python3.6 -y

Once you have a docker file use this to create a docker image and upload this to your gitlab-container registry of your project repository.一旦你有一个 docker 文件,使用它来创建一个 docker 图像并将它上传到你的项目存储库的 gitlab-container 注册表。

Using Sonar-scanner for python project:对 python 项目使用声纳扫描仪:

  1. If you want to publish your code coverage as well as code quality to sonarqube you will have to generate test coverage file which can be generated by any of the build tool.如果您想将代码覆盖率和代码质量发布到 sonarqube,您将必须生成可以由任何构建工具生成的测试覆盖率文件。

  2. Add coverage report path for python profile in sonarqube administration.在 sonarqube 管理中添加 python 配置文件的覆盖率报告路径。 It can be set as follows:可以如下设置:

    a.一个。 Login to sonarqube server with admin user.使用管理员用户登录 sonarqube 服务器。

    b.湾。 Go to Administration-> configuration -> python -> Test and Coverage -> Path to coverage reports. Go 到管理 -> 配置 -> python -> 测试和覆盖率 -> 覆盖率报告的路径。 Set coverage-reports/*coverage-*.xml as coverage path for sonar.python.coverage.reportPaths key.coverage-reports/*coverage-*.xmlsonar.python.coverage.reportPaths键的覆盖路径。

  3. Generate test coverage report for example using poetry build tool poetry run py.test tests/ --cov=<PROJECT_PACKAGE_NAME>/ --cov-report xml:coverage-reports/coverage-result.xml生成测试覆盖率报告,例如使用诗歌构建工具poetry run py.test tests/ --cov=<PROJECT_PACKAGE_NAME>/ --cov-report xml:coverage-reports/coverage-result.xml

  4. Execute the following command on your在您的设备上执行以下命令

    sonar-scanner -Dsonar.coverage.exclusions=tests/** -Dsonar.python.coverage.reportPaths=coverage-reports/coverage-reports.xml  -Dsonar.projectKey=<PROJECT_NAME> -Dsonar.projectName=<PROJECT_NAME> -Dsonar.projectVersion=<<PROJECT_VERSION>  -Dsonar.sources=<PROJECT_PACKAGE_NAME> -Dsonar.tests=tests/  -Dsonar.sourceEncoding=UTF-8 -Dsonar.host.url=<SONAR_HOST_SERVER_URL> -Dsonar.login=<SONAR_AUTH_TOKEN>

Note: It will be better if you will not set any configurations in sonar-scanner.properties file and provide configurations in the command itself with -D parameter注意:最好不要在 sonar-scanner.properties 文件中设置任何配置,并在命令本身中使用 -D 参数提供配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM