Gitlab has functionality to generade badges about build status and coverage percentage.
Is it possible to create custom badge to display Pylint results?
Or just display this results in README.md?
I already have CI job for Pylint
相关问题
- Gitlab-runner without Docker
- How to get Gitlab merge request description in Git
- How do i give parameters to SonarQube.Scanner.MSBu
- What is the result of mounting `/var/run/docker.so
- 请问 GitLab 如何连接一个已有的 Kubernetes 集群
相关文章
- 请问 GitLab 如何连接一个已有的 Kubernetes 集群
- gitlab升级失败
- gitlab-ci docker 部署后 找不到容器
- ssh: Could not resolve hostname git: Name or servi
- How do you center text in Gitlab markdown?
- How do I disable a Pylint specific error message g
- .gitlab-ci.yml after_script section: how can I tel
- Gitlab CI / Docker: Use custom image for job
I have written a python badge generation package that produces badges very visually similar to the main badge services. It is highly flexible, you can import and use in your python code, or run from the command line.
I use this in GitLab CI to display pylint and coverage scores.
There are other ways to do this using shields.io (see other answer from kubouch), but this approach can be used in situations where you may not have external internet access, such as in a corporate / enterprise setting where firewalls or proxies are blocking internet access.
GitLab CI Setup
1. Generate the badge
My CI pipeline has a step that runs pylint, and I used
sed
to extract the score from the output text. I then useanybadge
(details below) to generate a pylint score badge, and save it aspublic/pylint.svg
.If pylint generates a non-zero rc then GitLab will see that as a command error and the job will fail, meaning no badge is generated, and missing image will show where the badge is used.
NOTE: pylint WILL OFTEN generate non-zero return codes since it uses the exit code to communicate the status of the lint check. I suggest using something like pylint-exit to handle pylint return codes in CI pipelines.
2. Register badge as pipeline artifact
I register the generated badge file as an artifact in the CI job by including this in the
.gitlab-ci.yml
:3. Publish badge to GitLab Pages
I include a pages publish step, which deploys everything in the public directory to GitLab pages:
4. Include badge in README.md
When the master pipeline runs for the project, the
pylint.svg
file is published to GitLab Pages, and I can then reference the image from my projectREADME.md
so that the latest pylint badge is displayed.If you are using https://gitlab.com for your project then the URL for the svg artifact will usually be something like this (replace NAMESPACE with your username, or group name if your project is under a group - more details here):
https://NAMESPACE.gitlab.io/pyling.svg
In your README.md you can include an image with:
If you want to make the image into a link you can use:
Let me know if you need more information on any of the setup.
Anybadge Python Package
Here's some more info on the anybadge Python package:
You can set the badge label and value, and you can set the color based on thresholds. There are pre-built settings for pylint, coverage, and pipeline success, but you can create any badge you like.
Here is a link to the github project with more detailed documentation: https://github.com/jongracecox/anybadge
Install with
pip install anybadge
Example python code:
Example command line use:
Update 2019
Using GitLab Pages is no longer required
It is now possible to directly access to the latest articfact, which simplify the workaround.
pylint
artifact instead ofpublic
, and remove the unnecessarydeploy
step (or edit it if already used):Note that here I copy the Pylint log file in the folder artifact, in this way it will be accessible without looking at the pipeline logs.
The badge image will then be available at
https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.svg?job=pylint
, and the Pylint log athttps://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.log?job=pylint
.2. You can use GitLab's builtin badges instead of images in README
GitLab can now include badges in a projet or group, that will be displayed in the project header.
Got to
Settings
/General
/Badges
, then create a new badge by setting its link and image link, as described above:If you don't want to use the README, gitlab pages, anybadge or dropbox you can use https://img.shields.io/badge/lint%20score-$score-blue.svg to 'create' a badge (which is just an URL) and change the badge image URL via the gitlab API.
Details and the lint stage of my
.gitlab-ci.yml
I developed a workaround solution to real-time per-job badges. It is not Pylint specific but the approach is general and you can easily modify it into what you need.
This example repo (branch
badges
) creates a custom badge for each CI job. There is also a complete walkthrough so I won't copy-paste it here.The core idea is (assuming you're now inside a running CI job):
Dropbox supports calling its API via HTTP requests (see this). Thus, all the above can be done using e.g. curl or Python requests - basic tools. You just need to pass the Dropbox access token as secret variable and save the file under the same name to overwrite the old badge.
Then, you can then directly link the Dropbox badge wherever you need. There are some tricks to it so be sure to check my example repo if you want to use it. For me it works quite well and seems to be fast.
The advantage of this method is that you don't have to mess with GitLab Pages. Instead of publishing on Pages you put it to Dropbox. That is a simple file transfer called by HTTP request. No more to that.