Gitlab具有生成有关构建状态和覆盖百分比的徽章功能。
是否可以创建自定义徽章来显示Pylint结果?或者只在README.md中显示这些结果?
我已经为Pylint创建了CI作业
Gitlab具有生成有关构建状态和覆盖百分比的徽章功能。
是否可以创建自定义徽章来显示Pylint结果?或者只在README.md中显示这些结果?
我已经为Pylint创建了CI作业
我创建了一个Python徽章生成包,可以生成与主要徽章服务非常相似的可视化徽章。它非常灵活,您可以在Python代码中导入并使用,也可以从命令行运行。
我在GitLab CI中使用它来显示pylint和覆盖率得分。
使用shields.io也有其他方法可以做到这一点(请参见来自kubouch的其他答案),但是当您可能没有外部互联网访问权限(例如在防火墙或代理阻止互联网访问的企业设置中)时,可以使用此方法。
我的CI管道有一个步骤运行pylint,我使用sed
从输出文本中提取分数。然后,我使用anybadge
(详细信息如下)生成一个pylint分数徽章,并将其保存为public/pylint.svg
。
pylint:
stage: test
script:
- pylint --rcfile=.pylintrc --output-format=text <LIST-OF-FILES-TO-RUN-PYLINT-AGAINST> | tee pylint.txt
- score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' pylint.txt)
- echo "Pylint score was $score"
- anybadge --value=$score --file=public/pylint.svg pylint
如果pylint生成非零rc,则GitLab将视为命令错误并且作业将失败,这意味着不会生成徽章,并且在使用徽章的位置将显示缺少图像。
注意:由于pylint使用退出代码来通信lint检查的状态,因此pylint经常会生成非零返回代码。建议在CI管道中使用类似pylint-exit这样的工具来处理pylint返回代码。
我通过将以下内容包含在.gitlab-ci.yml
中,将生成的徽章文件作为CI作业的artifact进行注册:
pylint:
...
- echo "Pylint score was $score"
- anybadge --value=$score --file=public/pylint.svg pylint
artifacts:
paths:
- public/pylint.svg
我包含了一个页面发布步骤,它将public目录中的所有内容部署到GitLab页面:
pages:
stage: deploy
artifacts:
paths:
- public
only:
- master
当项目的主要流程运行时,pylint.svg
文件将被发布到GitLab Pages中。然后我可以从我的项目README.md
引用该图像,以便显示最新的pylint徽章。
如果您正在使用https://gitlab.com进行项目,则svg工件的URL通常类似于以下内容(将NAMESPACE替换为您的用户名或组名(如果您的项目在组下) - 更多详细信息请点击此处):
https://NAMESPACE.gitlab.io/pyling.svg
您可以通过以下方式在README.md中包含一个图像:
![pylint](https://NAMESPACE.gitlab.io/pyling.svg)
如果您想将图片转换为链接,可以使用:
[![pylint](https://NAMESPACE.gitlab.io/pyling.svg)](LINKTARGET)
如果您需要有关任何设置的更多信息,请告诉我。
这里是有关anybadge Python包的更多信息:
您可以设置徽章标签和值,还可以根据阈值设置颜色。 有预先构建的设置用于pylint、覆盖率和管道成功,但您可以创建任何您喜欢的徽章。
这是一个链接到GitHub项目的链接,其中包含更详细的文档:https://github.com/jongracecox/anybadge
使用pip install anybadge
进行安装
示例Python代码:
import anybadge
# Define thresholds: <2=red, <4=orange <8=yellow <10=green
thresholds = {2: 'red',
4: 'orange',
6: 'yellow',
10: 'green'}
badge = anybadge.Badge('pylint', 2.22, thresholds=thresholds)
badge.write_badge('pylint.svg')
示例命令行使用:
anybadge --label pylint --value 2.22 --file pylint.svg 2=red 4=orange 8=yellow 10=green
现在可以直接访问最新的工件,这简化了解决方法。(参考链接)
pylint
工件代替public
,并删除不必要的deploy
步骤(如果已经使用,则进行编辑):pylint:
stage: test
before_script:
- pip install pylint pylint-exit anybadge
script:
- mkdir ./pylint
- pylint --output-format=text . | tee ./pylint/pylint.log || pylint-exit $?
- PYLINT_SCORE=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' ./pylint/pylint.log)
- anybadge --label=Pylint --file=pylint/pylint.svg --value=$PYLINT_SCORE 2=red 4=orange 8=yellow 10=green
- echo "Pylint score is $PYLINT_SCORE"
artifacts:
paths:
- ./pylint/
请注意,这里我复制了文件夹artifact中的Pylint日志文件,以便可以在不查看管道日志的情况下访问它。
然后,徽章图像将可在 https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.svg?job=pylint
找到,而Pylint日志则在 https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.log?job=pylint
。
GitLab现在可以在项目或组中包含徽章,并显示在项目标题中。
前往设置
/ 常规
/ 徽章
,然后通过设置其链接和图像链接创建新徽章,如上所述:
--exit-zero
给 pylint 来强制使用 0 的退出代码。 - Kirk Broadhurst我开发了一种实时作业徽章的解决方案。
虽然不是针对Pylint特定的,但这个方法是通用的,你可以轻松地将其修改为你所需要的。
这个样例仓库(分支badges
)为每个CI作业创建了一个自定义徽章。也有一个完整的演示,这里就不再重复了。
核心思想是(假设您现在正在运行CI作业内部):
Dropbox支持通过HTTP请求调用其API(请参见此处)。因此,所有上述操作都可以使用curl或Python请求等基本工具来完成。您只需要将Dropbox访问令牌传递为密钥变量,并将文件保存为相同名称以覆盖旧的徽章。
然后,您可以直接在需要的地方链接Dropbox徽章。如果要使用它,请确保检查我的样例仓库中的一些技巧。对我来说,它运行得非常好,并且似乎很快。
此方法的优点是,您无需烦恼GitLab页面。而是将其放到Dropbox中。这是通过HTTP请求调用的简单文件传输。没有更多的操作。
Add the below file - .gitlab-ci.yml
to your GitLab repository:
pylint:
stage: test
image: python:3.7-slim
before_script:
- mkdir -p public/badges public/lint
- echo undefined > public/badges/$CI_JOB_NAME.score
- pip install pylint-gitlab
script:
- pylint --exit-zero --output-format=text $(find -type f -name "*.py" ! -path "**/.venv/**") | tee /tmp/pylint.txt
- sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' /tmp/pylint.txt > public/badges/$CI_JOB_NAME.score
- pylint --exit-zero --output-format=pylint_gitlab.GitlabCodeClimateReporter $(find -type f -name "*.py" ! -path "**/.venv/**") > codeclimate.json
- pylint --exit-zero --output-format=pylint_gitlab.GitlabPagesHtmlReporter $(find -type f -name "*.py" ! -path "**/.venv/**") > public/lint/index.html
after_script:
- anybadge --overwrite --label $CI_JOB_NAME --value=$(cat public/badges/$CI_JOB_NAME.score) --file=public/badges/$CI_JOB_NAME.svg 4=red 6=orange 8=yellow 10=green
- |
echo "Your score is: $(cat public/badges/$CI_JOB_NAME.score)"
artifacts:
paths:
- public
reports:
codequality: codeclimate.json
when: always
pages:
stage: deploy
image: alpine:latest
script:
- echo
artifacts:
paths:
- public
only:
refs:
- master
Substitute the below variables accordingly depending upon your project structure.
For example, if your repository is located at company_intern/john/robot_code
and you added the .gitlab-ci.yml
file to your main
branch, then:
$GROUP = company_intern
$SUBGROUP = john
$PROJECT_NAME = robot_code
$BRANCH = main
# Substitute the above variables in the badge
[![pylint](https://gitlab.com/$GROUP/$SUBGROUP/$PROJECT_NAME/-/jobs/artifacts/$BRANCH/raw/public/badges/pylint.svg?job=pylint)](https://gitlab.com/$GROUP/$SUBGROUP/$PROJECT_NAME/-/jobs/artifacts/$BRANCH/browse/public/lint?job=pylint)
Your badge has now been integrated!
To verify the lining process locally before committing it directly:
# To lint all the python files in the directory:
pylint --exit-zero --output-format=text $(find -type f -name "*.py" ! -path "**/.venv/**")
# To lint a specific file, say foo.py:
pylint --exit-zero --output-format=text foo.py