代码质量分析工具
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(同前)
4)在系统设置SonarQube servers中建立Python
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)步设置的项目名。
顾翔凡言:人工智能未来的发展瓶颈在于对知识的更新。唯一不变的是变化,知识发生了变化,人工智能软件能否及时跟进变化,可能阻碍人工智能的使用。