Java、Python、C++支持Jenkins和SonarQube(三)

简介: Python与Jenkins和SonarQube

5 Python与Jenkins和SonarQube
5.1安装
代码质量分析工具

pip3 install pylint

代码覆盖率分析工具

pip3 install pytest-cov

运行pysonar的时候用

pip install pysonar

运行pylint 报告的时候用

pip install pylint pylint-html

5.2 相关设置

1)在SonarQube建立Python项目,项目名为Python。

2)在SonarQube上建立Python项目,记住token

3)在Jenkins建立Credentials(同前)
image.png
4)在系统设置SonarQube servers中建立Python
image.png
5.3在Jenkins建立pipeline

pipeline {
   
  agent any
  environment {
   
      JAVA_TOOL_OPTIONS = '-Dfile.encoding=UTF-8'
      PYTHONIOENCODING = 'UTF-8'
      PYTHON = 'python'
      PROJECT_DIR = '.'
      ALLURE_RESULTS = 'allure-results'
  }
  stages {
   
    stage('Run Tests') {
   
      steps {
   
         script {
   
             bat """
               ${
   PYTHON} -m pytest -v --alluredir=${
   ALLURE_RESULTS}
             """
            }
        }
    }
    stage('Run Coverage') {
   
       steps {
   
          script {
   
              bat """
                ${
   PYTHON} -m pytest --cov=./ --cov-report=html:site || echo "Coverage completed with warnings"
                  """
              }
         }
    }
    stage('pylint Code Quality') {
   
        steps {
   
            script {
   
                bat "${PYTHON} -m pylint --output-format=json *.py > report.json 2>&1 || exit 0"
                bat "pylint-json2html -o report.html report.json"
            }
        }
    }
    stage('Pylint Score Check') {
   
        steps {
   
            script {
   
                def score = bat(returnStdout: true, script: """
                ${
   PYTHON} -m pylint --exit-zero *.py | findstr "Your code has been rated at"
                """).trim()
                echo "Pylint score: ${score}"
            }
        }
    }
    stage('SonarQube Analysis') {
   
        steps {
   
            script {
   
               withSonarQubeEnv('Python') {
   
                    bat """
                        sonar-scanner -Dsonar.projectKey=Python \
                        -Dsonar.host.url=http://127.0.0.1:9000 \
                        -Dsonar.login=sqp_73a6cc32f8c3e1898cf32c2c1e576b6cc1864369\
                        -Dsonar.java.jdkHome=C:\\Tools\\jdk-17.0.15
                        """
                }
            }
        }
    }
    stage('SonarQube Quality Gate') {
   
        steps {
   
            script {
   
                timeout(time:5,unit:'MINUTES') {
   
                    sleep(5)
                    def qg = waitForQualityGate()
                    if (qg.status != 'OK') {
   
                        echo "Status:${qg.status}"
                        error "Pipeline aborted due to quality gate failure:${qg.status}"
                    }
                }
            }
        }
    }
}
post {
   
    always {
   
        script {
   
            allure([
                includeProperties: false,
                jdk: '',
                properties: [],
                reportBuildPolicy: 'ALWAYS',
                results: [[path: "${ALLURE_RESULTS}"]]
            ])
        }
        script {
   
            publishHTML (target: [
                allowMissing: false,
                alwaysLinkToLastBuild: true,
                keepAll: true,
                reportDir: 'site',
                reportFiles: 'index.html',
                reportName: 'Coverage Reports',
                reportTitles: 'Coverage Report'])
        }
        script {
   
            publishHTML (target: [
                allowMissing: false,
                alwaysLinkToLastBuild: true,
                keepAll: true,
                reportDir: '.\\',
                reportFiles: 'report.html',
                reportName: 'pylint Reports',
                reportTitles: 'pylint Report'])
        }
    }
  }
}

注意

1)命令行可以用

pysonar --sonar-host-url=http://127.0.0.1:9000 --sonar-token=sqp_73a6cc32f8c3e1898cf32c2c1e576b6cc1864369 --sonar-project-key=Python

但是在pipeline中必须用Sonar-Scan

sonar-scanner -Dsonar.projectKey=Python -Dsonar.host.url=http://127.0.0.1:9000 -Dsonar.login=sqp_73a6cc32f8c3e1898cf32c2c1e576b6cc1864369

sonar-scanner 被测对象是64/32位,sonar-scanner也必须为64/32位

withSonarQubeEnv('Python')中的Python为5.2 第5)步设置的项目名。

5.4运行
5.4.1 总体图
image.png
5.4.2 pytest-cov 报告
image.png
5.4.3 单元测试报告
image.png
5.4.4 Pylint报告
image.png

目录
相关文章
|
8月前
|
jenkins Shell 测试技术
|
8月前
|
jenkins Java Shell
Java、Python、C++支持jenkins和SonarQube(全集)
Jenkins 是一个开源的持续集成(CI)和持续交付(CD)工具,用于自动化构建、测试和部署软件项目。它基于 Java 开发,支持跨平台运行,并拥有丰富的插件生态系统,可以灵活地扩展功能
696 1
|
jenkins API 持续交付
PYTHON调用JENKINS的API来进行CI
我查到的相关API有两套,我主要用的是python-jenkins。 https://pypi.python.org/pypi/python-jenkins/ 按语法调用即可。。。 import jenkins server = jenkins.
2886 0
|
8月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1230 102
|
8月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
456 104
|
8月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
355 103
|
8月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
354 82
|
7月前
|
Python
Python编程:运算符详解
本文全面详解Python各类运算符,涵盖算术、比较、逻辑、赋值、位、身份、成员运算符及优先级规则,结合实例代码与运行结果,助你深入掌握Python运算符的使用方法与应用场景。
458 3
|
7月前
|
数据处理 Python
Python编程:类型转换与输入输出
本教程介绍Python中输入输出与类型转换的基础知识,涵盖input()和print()的使用,int()、float()等类型转换方法,并通过综合示例演示数据处理、错误处理及格式化输出,助你掌握核心编程技能。
676 3
|
7月前
|
并行计算 安全 计算机视觉
Python多进程编程:用multiprocessing突破GIL限制
Python中GIL限制多线程性能,尤其在CPU密集型任务中。`multiprocessing`模块通过创建独立进程,绕过GIL,实现真正的并行计算。它支持进程池、队列、管道、共享内存和同步机制,适用于科学计算、图像处理等场景。相比多线程,多进程更适合利用多核优势,虽有较高内存开销,但能显著提升性能。合理使用进程池与通信机制,可最大化效率。
476 3